; ModuleID = 'LFortran'
source_filename = "LFortran"

@0 = private unnamed_addr constant [39 x i8] c"tests/errors/runtime_stacktrace_01.f90\00", align 1
@1 = private unnamed_addr constant [5 x i8] c"STOP\00", align 1
@2 = private unnamed_addr constant [2 x i8] c" \00", align 1
@3 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@4 = private unnamed_addr constant [9 x i8] c"%s%s%d%s\00", align 1
@5 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@6 = private unnamed_addr constant [5 x i8] c"%s%s\00", align 1
@7 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@8 = private unnamed_addr constant [5 x i8] c"%s%s\00", align 1

define i32 @main(i32 %0, i8** %1) !dbg !3 {
.entry:
  call void @_lpython_call_initial_functions(i32 %0, i8** %1), !dbg !7
  %2 = call i32 @_xx_lcompilers_changed_main_xx(), !dbg !9
  %3 = sext i32 %2 to i64, !dbg !9
  %4 = call i8* (i32, i8*, ...) @_lcompilers_string_format_fortran(i32 2, i8* null, i32 2, i64 %3), !dbg !9
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @8, i32 0, i32 0), i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @7, i32 0, i32 0)), !dbg !9
  call void @_lpython_free_argv(), !dbg !9
  br label %return, !dbg !9

return:                                           ; preds = %.entry
  ret i32 0, !dbg !9
}

define void @f() !dbg !10 {
.entry:
  br i1 true, label %then, label %else, !dbg !13

then:                                             ; preds = %.entry
  call void (i8*, i1, ...) @print_stacktrace_addresses(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @0, i32 0, i32 0), i1 false), !dbg !13
  br label %ifcont, !dbg !13

else:                                             ; preds = %.entry
  br label %ifcont, !dbg !13

ifcont:                                           ; preds = %else, %then
  call void (i8*, ...) @_lcompilers_print_error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @4, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @1, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @2, i32 0, i32 0), i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @3, i32 0, i32 0)), !dbg !13
  call void @exit(i32 1), !dbg !13
  br label %return, !dbg !13

return:                                           ; preds = %ifcont
  ret void, !dbg !13
}

define i32 @g() !dbg !14 {
.entry:
  %g = alloca i32, align 4, !dbg !15
  call void @llvm.dbg.declare(metadata i32* %g, metadata !16, metadata !DIExpression()), !dbg !17
  store i32 1, i32* %g, align 4, !dbg !18
  call void @f(), !dbg !19
  br label %return, !dbg !19

return:                                           ; preds = %.entry
  %0 = load i32, i32* %g, align 4, !dbg !19
  ret i32 %0, !dbg !19
}

define i32 @_xx_lcompilers_changed_main_xx() !dbg !8 {
.entry:
  %main = alloca i32, align 4, !dbg !20
  call void @llvm.dbg.declare(metadata i32* %main, metadata !21, metadata !DIExpression()), !dbg !22
  store i32 1, i32* %main, align 4, !dbg !23
  %0 = call i32 @g(), !dbg !24
  %1 = sext i32 %0 to i64, !dbg !24
  %2 = call i8* (i32, i8*, ...) @_lcompilers_string_format_fortran(i32 2, i8* null, i32 2, i64 %1), !dbg !24
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @6, i32 0, i32 0), i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @5, i32 0, i32 0)), !dbg !24
  br label %return, !dbg !24

return:                                           ; preds = %.entry
  %3 = load i32, i32* %main, align 4, !dbg !24
  ret i32 %3, !dbg !24
}

declare void @print_stacktrace_addresses(i8*, i1, ...)

declare void @_lcompilers_print_error(i8*, ...)

declare void @exit(i32)

; Function Attrs: nounwind readnone speculatable willreturn
declare void @llvm.dbg.declare(metadata, metadata, metadata) #0

declare i8* @_lcompilers_string_format_fortran(i32, i8*, ...)

declare void @_lfortran_printf(i8*, ...)

declare void @_lpython_call_initial_functions(i32, i8**)

declare void @_lpython_free_argv()

attributes #0 = { nounwind readnone speculatable willreturn }

!llvm.dbg.cu = !{!0}

!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "LPython Compiler", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "tests/errors/runtime_stacktrace_01.f90", directory: ".")
!2 = !{}
!3 = distinct !DISubprogram(name: "expr2", scope: !1, file: !1, line: 1, type: !4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!4 = !DISubroutineType(types: !5)
!5 = !{!6}
!6 = !DIBasicType(name: "integer", size: 32, encoding: DW_ATE_signed)
!7 = !DILocation(line: 1, column: 1, scope: !8)
!8 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 7, type: !4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!9 = !DILocation(line: 3, column: 14, scope: !8)
!10 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 13, type: !11, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!11 = !DISubroutineType(types: !12)
!12 = !{null}
!13 = !DILocation(line: 14, column: 9, scope: !10)
!14 = distinct !DISubprogram(name: "g", scope: !1, file: !1, line: 17, type: !4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!15 = !DILocation(line: 17, column: 5, scope: !14)
!16 = !DILocalVariable(name: "g", arg: 1, scope: !14, file: !1, line: 18, type: !6)
!17 = !DILocation(line: 18, scope: !14)
!18 = !DILocation(line: 19, column: 9, scope: !14)
!19 = !DILocation(line: 20, column: 9, scope: !14)
!20 = !DILocation(line: 7, column: 5, scope: !8)
!21 = !DILocalVariable(name: "main", arg: 1, scope: !8, file: !1, line: 8, type: !6)
!22 = !DILocation(line: 8, scope: !8)
!23 = !DILocation(line: 9, column: 9, scope: !8)
!24 = !DILocation(line: 10, column: 18, scope: !8)
asr_to_llvm: module failed verification. Error:
!dbg attachment points at wrong subprogram for function
!3 = distinct !DISubprogram(name: "expr2", scope: !1, file: !1, line: 1, type: !4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
i32 (i32, i8**)* @main
  call void @_lpython_call_initial_functions(i32 %0, i8** %1), !dbg !7
!7 = !DILocation(line: 1, column: 1, scope: !8)
!8 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 7, type: !4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!8 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 7, type: !4, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)

