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

; 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] }
%struct.WINDOW = type { ptr, ptr, ptr, ptr, i32, ptr, i32, i32, i32, i32, i32, i32 }
%struct.LINE = type { ptr, ptr, i16, i16, [32768 x i8] }

@wheadp = external global ptr, align 8
@.str.10 = external hidden unnamed_addr constant [29 x i8], align 1

; 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

declare void @mlwrite(ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @make_popup(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %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 ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = call i32 (ptr, ptr, ...) %12(ptr noundef %13, ptr noundef %14)
  store i32 %15, ptr %10, align 4, !tbaa !9
  %16 = icmp ne i32 %15, 1
  br i1 %16, label %17, label %19

17:                                               ; preds = %3
  %18 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %18, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %109

19:                                               ; preds = %3
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.BUFFER, ptr %20, i32 0, i32 6
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %67

24:                                               ; preds = %19
  %25 = call ptr (...) @wpopup()
  store ptr %25, ptr %8, align 8, !tbaa !5
  %26 = icmp eq ptr %25, null
  br i1 %26, label %27, label %28

27:                                               ; preds = %24
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %109

28:                                               ; preds = %24
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.WINDOW, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !13
  store ptr %31, ptr %9, align 8, !tbaa !5
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.BUFFER, ptr %32, i32 0, i32 6
  %34 = load i32, ptr %33, align 8, !tbaa !11
  %35 = add nsw i32 %34, -1
  store i32 %35, ptr %33, align 8, !tbaa !11
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %58

37:                                               ; preds = %28
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.WINDOW, ptr %38, i32 0, i32 3
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.BUFFER, ptr %41, i32 0, i32 1
  store ptr %40, ptr %42, align 8, !tbaa !16
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.WINDOW, ptr %43, i32 0, i32 4
  %45 = load i32, ptr %44, align 8, !tbaa !17
  %46 = load ptr, ptr %9, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.BUFFER, ptr %46, i32 0, i32 2
  store i32 %45, ptr %47, align 8, !tbaa !18
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.WINDOW, ptr %48, i32 0, i32 5
  %50 = load ptr, ptr %49, align 8, !tbaa !19
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.BUFFER, ptr %51, i32 0, i32 3
  store ptr %50, ptr %52, align 8, !tbaa !20
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.WINDOW, ptr %53, i32 0, i32 6
  %55 = load i32, ptr %54, align 8, !tbaa !21
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.BUFFER, ptr %56, i32 0, i32 4
  store i32 %55, ptr %57, align 8, !tbaa !22
  br label %58

58:                                               ; preds = %37, %28
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %59, ptr %9, align 8, !tbaa !5
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.WINDOW, ptr %61, i32 0, i32 1
  store ptr %60, ptr %62, align 8, !tbaa !13
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.BUFFER, ptr %63, i32 0, i32 6
  %65 = load i32, ptr %64, align 8, !tbaa !11
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %64, align 8, !tbaa !11
  br label %67

67:                                               ; preds = %58, %19
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %68, ptr %9, align 8, !tbaa !5
  %69 = load ptr, ptr @wheadp, align 8, !tbaa !5
  store ptr %69, ptr %8, align 8, !tbaa !5
  br label %70

70:                                               ; preds = %104, %67
  %71 = load ptr, ptr %8, align 8, !tbaa !5
  %72 = icmp ne ptr %71, null
  br i1 %72, label %73, label %108

73:                                               ; preds = %70
  %74 = load ptr, ptr %8, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.WINDOW, ptr %74, i32 0, i32 1
  %76 = load ptr, ptr %75, align 8, !tbaa !13
  %77 = load ptr, ptr %9, align 8, !tbaa !5
  %78 = icmp eq ptr %76, %77
  br i1 %78, label %79, label %104

79:                                               ; preds = %73
  %80 = load ptr, ptr %9, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.BUFFER, ptr %80, i32 0, i32 5
  %82 = load ptr, ptr %81, align 8, !tbaa !23
  %83 = getelementptr inbounds %struct.LINE, ptr %82, i32 0, i32 0
  %84 = load ptr, ptr %83, align 8, !tbaa !24
  %85 = load ptr, ptr %8, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.WINDOW, ptr %85, i32 0, i32 2
  store ptr %84, ptr %86, align 8, !tbaa !27
  %87 = load ptr, ptr %9, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.BUFFER, ptr %87, i32 0, i32 5
  %89 = load ptr, ptr %88, align 8, !tbaa !23
  %90 = getelementptr inbounds %struct.LINE, ptr %89, i32 0, i32 0
  %91 = load ptr, ptr %90, align 8, !tbaa !24
  %92 = load ptr, ptr %8, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.WINDOW, ptr %92, i32 0, i32 3
  store ptr %91, ptr %93, align 8, !tbaa !15
  %94 = load ptr, ptr %8, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.WINDOW, ptr %94, i32 0, i32 4
  store i32 0, ptr %95, align 8, !tbaa !17
  %96 = load ptr, ptr %8, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.WINDOW, ptr %96, i32 0, i32 5
  store ptr null, ptr %97, align 8, !tbaa !19
  %98 = load ptr, ptr %8, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.WINDOW, ptr %98, i32 0, i32 6
  store i32 0, ptr %99, align 8, !tbaa !21
  %100 = load ptr, ptr %8, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.WINDOW, ptr %100, i32 0, i32 11
  %102 = load i32, ptr %101, align 4, !tbaa !28
  %103 = or i32 %102, 25
  store i32 %103, ptr %101, align 4, !tbaa !28
  br label %104

104:                                              ; preds = %79, %73
  %105 = load ptr, ptr %8, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.WINDOW, ptr %105, i32 0, i32 0
  %107 = load ptr, ptr %106, align 8, !tbaa !29
  store ptr %107, ptr %8, align 8, !tbaa !5
  br label %70, !llvm.loop !30

108:                                              ; preds = %70
  call void (ptr, ...) @mlwrite(ptr noundef @.str.10)
  store i32 1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %109

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

declare ptr @wpopup(...) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!12, !10, i64 48}
!12 = !{!"BUFFER", !6, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !10, i64 32, !6, i64 40, !10, i64 48, !10, i64 52, !7, i64 56, !7, i64 184}
!13 = !{!14, !6, i64 8}
!14 = !{!"WINDOW", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32, !6, i64 40, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68}
!15 = !{!14, !6, i64 24}
!16 = !{!12, !6, i64 8}
!17 = !{!14, !10, i64 32}
!18 = !{!12, !10, i64 16}
!19 = !{!14, !6, i64 40}
!20 = !{!12, !6, i64 24}
!21 = !{!14, !10, i64 48}
!22 = !{!12, !10, i64 32}
!23 = !{!12, !6, i64 40}
!24 = !{!25, !6, i64 0}
!25 = !{!"LINE", !6, i64 0, !6, i64 8, !26, i64 16, !26, i64 18, !7, i64 20}
!26 = !{!"short", !7, i64 0}
!27 = !{!14, !6, i64 16}
!28 = !{!14, !10, i64 68}
!29 = !{!14, !6, i64 0}
!30 = distinct !{!30, !31}
!31 = !{!"llvm.loop.mustprogress"}


; 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"

@wheadp = external local_unnamed_addr global ptr, align 8
@.str.10 = external hidden unnamed_addr constant [29 x i8], align 1

declare void @mlwrite(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @make_popup(ptr noundef %0, ptr nocapture noundef readonly %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = tail call i32 (ptr, ptr, ...) %1(ptr noundef %0, ptr noundef %2) #2
  %.not = icmp eq i32 %4, 1
  br i1 %.not, label %5, label %53

5:                                                ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 48
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %35

9:                                                ; preds = %5
  %10 = tail call ptr (...) @wpopup() #2
  %11 = icmp eq ptr %10, null
  br i1 %11, label %53, label %12

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %10, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %14, i64 48
  %16 = load i32, ptr %15, align 8, !tbaa !5
  %17 = add nsw i32 %16, -1
  store i32 %17, ptr %15, align 8, !tbaa !5
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %32

19:                                               ; preds = %12
  %20 = getelementptr inbounds i8, ptr %10, i64 24
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %14, i64 8
  store ptr %21, ptr %22, align 8, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %10, i64 32
  %24 = load i32, ptr %23, align 8, !tbaa !15
  %25 = getelementptr inbounds i8, ptr %14, i64 16
  store i32 %24, ptr %25, align 8, !tbaa !16
  %26 = getelementptr inbounds i8, ptr %10, i64 40
  %27 = load ptr, ptr %26, align 8, !tbaa !17
  %28 = getelementptr inbounds i8, ptr %14, i64 24
  store ptr %27, ptr %28, align 8, !tbaa !18
  %29 = getelementptr inbounds i8, ptr %10, i64 48
  %30 = load i32, ptr %29, align 8, !tbaa !19
  %31 = getelementptr inbounds i8, ptr %14, i64 32
  store i32 %30, ptr %31, align 8, !tbaa !20
  br label %32

32:                                               ; preds = %19, %12
  store ptr %0, ptr %13, align 8, !tbaa !11
  %33 = load i32, ptr %6, align 8, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %6, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %32, %5
  %.03237 = load ptr, ptr @wheadp, align 8, !tbaa !21
  %.not3638 = icmp eq ptr %.03237, null
  br i1 %.not3638, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %35
  %36 = getelementptr inbounds i8, ptr %0, i64 40
  br label %37

37:                                               ; preds = %.lr.ph, %52
  %.03239 = phi ptr [ %.03237, %.lr.ph ], [ %.032, %52 ]
  %38 = getelementptr inbounds i8, ptr %.03239, i64 8
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  %40 = icmp eq ptr %39, %0
  br i1 %40, label %41, label %52

41:                                               ; preds = %37
  %42 = load ptr, ptr %36, align 8, !tbaa !22
  %43 = load ptr, ptr %42, align 8, !tbaa !23
  %44 = getelementptr inbounds i8, ptr %.03239, i64 16
  store ptr %43, ptr %44, align 8, !tbaa !26
  %45 = getelementptr inbounds i8, ptr %.03239, i64 24
  store ptr %43, ptr %45, align 8, !tbaa !13
  %46 = getelementptr inbounds i8, ptr %.03239, i64 32
  store i32 0, ptr %46, align 8, !tbaa !15
  %47 = getelementptr inbounds i8, ptr %.03239, i64 40
  store ptr null, ptr %47, align 8, !tbaa !17
  %48 = getelementptr inbounds i8, ptr %.03239, i64 48
  store i32 0, ptr %48, align 8, !tbaa !19
  %49 = getelementptr inbounds i8, ptr %.03239, i64 68
  %50 = load i32, ptr %49, align 4, !tbaa !27
  %51 = or i32 %50, 25
  store i32 %51, ptr %49, align 4, !tbaa !27
  br label %52

52:                                               ; preds = %41, %37
  %.032 = load ptr, ptr %.03239, align 8, !tbaa !21
  %.not36 = icmp eq ptr %.032, null
  br i1 %.not36, label %._crit_edge, label %37, !llvm.loop !28

._crit_edge:                                      ; preds = %52, %35
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.10) #2
  br label %53

53:                                               ; preds = %9, %3, %._crit_edge
  %.0 = phi i32 [ 1, %._crit_edge ], [ %4, %3 ], [ 0, %9 ]
  ret i32 %.0
}

declare ptr @wpopup(...) local_unnamed_addr #0

attributes #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 = { 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 = { 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, !10, i64 48}
!6 = !{!"BUFFER", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24, !10, i64 32, !7, i64 40, !10, i64 48, !10, i64 52, !8, i64 56, !8, i64 184}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !7, i64 8}
!12 = !{!"WINDOW", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !7, i64 40, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68}
!13 = !{!12, !7, i64 24}
!14 = !{!6, !7, i64 8}
!15 = !{!12, !10, i64 32}
!16 = !{!6, !10, i64 16}
!17 = !{!12, !7, i64 40}
!18 = !{!6, !7, i64 24}
!19 = !{!12, !10, i64 48}
!20 = !{!6, !10, i64 32}
!21 = !{!7, !7, i64 0}
!22 = !{!6, !7, i64 40}
!23 = !{!24, !7, i64 0}
!24 = !{!"LINE", !7, i64 0, !7, i64 8, !25, i64 16, !25, i64 18, !8, i64 20}
!25 = !{!"short", !8, i64 0}
!26 = !{!12, !7, i64 16}
!27 = !{!12, !10, i64 68}
!28 = distinct !{!28, !29}
!29 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass