; LLVM IR from freakout42/macrocalc :: mc/mex (files)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.BUFFER = type { ptr, ptr, i32, ptr, i32, ptr, i32, i32, [128 x i8], [128 x i8] }

@nfiles = external global i32, align 4
@clfn = external global ptr, align 8
@curbp = external global ptr, align 8
@oldbp = external global ptr, align 8
@fileindex = external global i32, align 4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @addfile(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @choosefile(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 1, ptr %10, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %92, %41, %3
  %13 = load i32, ptr %10, align 4, !tbaa !9
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %44

15:                                               ; preds = %12
  store i32 0, ptr %10, align 4, !tbaa !9
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %34

18:                                               ; preds = %15
  %19 = load ptr, ptr @curbp, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.BUFFER, ptr %19, i32 0, i32 8
  %21 = getelementptr inbounds [128 x i8], ptr %20, i64 0, i64 0
  %22 = load i8, ptr %21, align 8, !tbaa !11
  %23 = sext i8 %22 to i32
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %29

25:                                               ; preds = %18
  %26 = load ptr, ptr @curbp, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.BUFFER, ptr %26, i32 0, i32 8
  %28 = getelementptr inbounds [128 x i8], ptr %27, i64 0, i64 0
  store ptr %28, ptr %8, align 8, !tbaa !5
  br label %33

29:                                               ; preds = %18
  %30 = load ptr, ptr @curbp, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.BUFFER, ptr %30, i32 0, i32 9
  %32 = getelementptr inbounds [128 x i8], ptr %31, i64 0, i64 0
  store ptr %32, ptr %8, align 8, !tbaa !5
  br label %33

33:                                               ; preds = %29, %25
  br label %43

34:                                               ; preds = %15
  %35 = load ptr, ptr @oldbp, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %41

37:                                               ; preds = %34
  %38 = load ptr, ptr @oldbp, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.BUFFER, ptr %38, i32 0, i32 8
  %40 = getelementptr inbounds [128 x i8], ptr %39, i64 0, i64 0
  store ptr %40, ptr %8, align 8, !tbaa !5
  br label %42

41:                                               ; preds = %34
  br label %12

42:                                               ; preds = %37
  br label %43

43:                                               ; preds = %42, %33
  br label %65

44:                                               ; preds = %12
  %45 = load i32, ptr @nfiles, align 4, !tbaa !9
  %46 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %48

47:                                               ; preds = %44
  store ptr null, ptr %8, align 8, !tbaa !5
  br label %64

48:                                               ; preds = %44
  %49 = load i32, ptr @fileindex, align 4, !tbaa !9
  %50 = load i32, ptr @nfiles, align 4, !tbaa !9
  %51 = icmp slt i32 %49, %50
  br i1 %51, label %52, label %59

52:                                               ; preds = %48
  %53 = load ptr, ptr @clfn, align 8, !tbaa !5
  %54 = load i32, ptr @fileindex, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr @fileindex, align 4, !tbaa !9
  %56 = sext i32 %54 to i64
  %57 = getelementptr inbounds ptr, ptr %53, i64 %56
  %58 = load ptr, ptr %57, align 8, !tbaa !5
  store ptr %58, ptr %8, align 8, !tbaa !5
  br label %63

59:                                               ; preds = %48
  %60 = load ptr, ptr @clfn, align 8, !tbaa !5
  %61 = getelementptr inbounds ptr, ptr %60, i64 0
  %62 = load ptr, ptr %61, align 8, !tbaa !5
  store ptr %62, ptr %8, align 8, !tbaa !5
  store i32 1, ptr @fileindex, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %59, %52
  br label %64

64:                                               ; preds = %63, %47
  br label %65

65:                                               ; preds = %64, %43
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = call i32 @mlreply(ptr noundef %66, ptr noundef %67, ptr noundef %68, i32 noundef 128, i32 noundef 0)
  store i32 %69, ptr %9, align 4, !tbaa !9
  %70 = load i32, ptr %9, align 4, !tbaa !9
  %71 = icmp eq i32 %70, 336
  br i1 %71, label %72, label %86

72:                                               ; preds = %65
  %73 = load i32, ptr @fileindex, align 4, !tbaa !9
  %74 = icmp sgt i32 %73, 1
  br i1 %74, label %75, label %78

75:                                               ; preds = %72
  %76 = load i32, ptr @fileindex, align 4, !tbaa !9
  %77 = sub nsw i32 %76, 2
  store i32 %77, ptr @fileindex, align 4, !tbaa !9
  br label %85

78:                                               ; preds = %72
  %79 = load i32, ptr @fileindex, align 4, !tbaa !9
  %80 = icmp eq i32 %79, 1
  br i1 %80, label %81, label %84

81:                                               ; preds = %78
  %82 = load i32, ptr @nfiles, align 4, !tbaa !9
  %83 = sub nsw i32 %82, 1
  store i32 %83, ptr @fileindex, align 4, !tbaa !9
  br label %84

84:                                               ; preds = %81, %78
  br label %85

85:                                               ; preds = %84, %75
  br label %86

86:                                               ; preds = %85, %65
  %87 = load i32, ptr %9, align 4, !tbaa !9
  %88 = icmp eq i32 %87, 336
  br i1 %88, label %92, label %89

89:                                               ; preds = %86
  %90 = load i32, ptr %9, align 4, !tbaa !9
  %91 = icmp eq i32 %90, 334
  br i1 %91, label %92, label %93

92:                                               ; preds = %89, %86
  br label %12

93:                                               ; preds = %89
  %94 = load i32, ptr %9, align 4, !tbaa !9
  %95 = icmp ne i32 %94, 1
  br i1 %95, label %96, label %100

96:                                               ; preds = %93
  %97 = load i32, ptr %9, align 4, !tbaa !9
  %98 = icmp ne i32 %97, 0
  br i1 %98, label %99, label %100

99:                                               ; preds = %96
  store i32 2, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %107

100:                                              ; preds = %96, %93
  %101 = load i32, ptr %9, align 4, !tbaa !9
  %102 = icmp eq i32 %101, 1
  br i1 %102, label %103, label %106

103:                                              ; preds = %100
  %104 = load ptr, ptr %6, align 8, !tbaa !5
  %105 = call i32 @addfile(ptr noundef %104)
  br label %106

106:                                              ; preds = %103, %100
  store i32 1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %107

107:                                              ; preds = %106, %99
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %108 = load i32, ptr %4, align 4
  ret i32 %108
}

declare i32 @mlreply(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@nfiles = external local_unnamed_addr global i32, align 4
@clfn = external local_unnamed_addr global ptr, align 8
@curbp = external local_unnamed_addr global ptr, align 8
@oldbp = external local_unnamed_addr global ptr, align 8
@fileindex = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local i32 @addfile(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 1, 3) i32 @choosefile(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
.critedge:
  %.not23 = icmp eq i32 %2, 0
  br i1 %.not23, label %11, label %6

3:                                                ; preds = %33, %.sink.split, %26, %11
  %4 = load i32, ptr @nfiles, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %26, label %15

6:                                                ; preds = %.critedge
  %7 = load ptr, ptr @curbp, align 8, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %7, i64 56
  %9 = load i8, ptr %8, align 8, !tbaa !11
  %.not25 = icmp eq i8 %9, 0
  %10 = getelementptr inbounds i8, ptr %7, i64 184
  %spec.select = select i1 %.not25, ptr %10, ptr %8
  br label %26

11:                                               ; preds = %.critedge
  %12 = load ptr, ptr @oldbp, align 8, !tbaa !9
  %.not24 = icmp eq ptr %12, null
  br i1 %.not24, label %3, label %13

13:                                               ; preds = %11
  %14 = getelementptr inbounds i8, ptr %12, i64 56
  br label %26

15:                                               ; preds = %3
  %16 = load i32, ptr @fileindex, align 4, !tbaa !5
  %17 = icmp slt i32 %16, %4
  %18 = load ptr, ptr @clfn, align 8, !tbaa !9
  br i1 %17, label %19, label %24

19:                                               ; preds = %15
  %20 = add nsw i32 %16, 1
  store i32 %20, ptr @fileindex, align 4, !tbaa !5
  %21 = sext i32 %16 to i64
  %22 = getelementptr inbounds ptr, ptr %18, i64 %21
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  br label %26

24:                                               ; preds = %15
  %25 = load ptr, ptr %18, align 8, !tbaa !9
  store i32 1, ptr @fileindex, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %6, %3, %24, %19, %13
  %.016 = phi ptr [ %14, %13 ], [ %23, %19 ], [ %25, %24 ], [ null, %3 ], [ %spec.select, %6 ]
  %27 = tail call i32 @mlreply(ptr noundef %0, ptr noundef %.016, ptr noundef %1, i32 noundef 128, i32 noundef 0) #2
  switch i32 %27, label %38 [
    i32 336, label %28
    i32 334, label %3
  ]

28:                                               ; preds = %26
  %29 = load i32, ptr @fileindex, align 4, !tbaa !5
  %30 = icmp sgt i32 %29, 1
  br i1 %30, label %31, label %33

31:                                               ; preds = %28
  %32 = add nsw i32 %29, -2
  br label %.sink.split

33:                                               ; preds = %28
  %34 = icmp eq i32 %29, 1
  br i1 %34, label %35, label %3

35:                                               ; preds = %33
  %36 = load i32, ptr @nfiles, align 4, !tbaa !5
  %37 = add nsw i32 %36, -1
  br label %.sink.split

.sink.split:                                      ; preds = %35, %31
  %.sink = phi i32 [ %32, %31 ], [ %37, %35 ]
  store i32 %.sink, ptr @fileindex, align 4, !tbaa !5
  br label %3

38:                                               ; preds = %26
  %or.cond3 = icmp ugt i32 %27, 1
  br i1 %or.cond3, label %43, label %39

39:                                               ; preds = %38
  %40 = icmp eq i32 %27, 1
  br i1 %40, label %41, label %43

41:                                               ; preds = %39
  %42 = tail call i32 @addfile(ptr noundef %1)
  br label %43

43:                                               ; preds = %39, %41, %38
  %.015 = phi i32 [ 2, %38 ], [ 1, %41 ], [ 1, %39 ]
  ret i32 %.015
}

declare i32 @mlreply(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass