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

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

@curbp = external global ptr, align 8
@curwp = external global ptr, align 8
@.str.12 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [9 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

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

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

declare i32 @ldelete(i32 noundef, i32 noundef) #1

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

12:                                               ; preds = %2
  %13 = load ptr, ptr @curwp, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.WINDOW, ptr %13, i32 0, i32 3
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  %16 = getelementptr inbounds %struct.LINE, ptr %15, i32 0, i32 3
  %17 = load i16, ptr %16, align 2, !tbaa !13
  %18 = sext i16 %17 to i32
  %19 = and i32 65535, %18
  %20 = load ptr, ptr @curwp, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.WINDOW, ptr %20, i32 0, i32 4
  %22 = load i32, ptr %21, align 8, !tbaa !16
  %23 = sub nsw i32 %19, %22
  store i32 %23, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = icmp sle i32 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %12
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %26, %12
  br label %85

28:                                               ; preds = %2
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %37

31:                                               ; preds = %28
  %32 = load ptr, ptr @curwp, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.WINDOW, ptr %32, i32 0, i32 4
  %34 = load i32, ptr %33, align 8, !tbaa !16
  store i32 %34, ptr %6, align 4, !tbaa !5
  %35 = load ptr, ptr @curwp, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.WINDOW, ptr %35, i32 0, i32 4
  store i32 0, ptr %36, align 8, !tbaa !16
  br label %84

37:                                               ; preds = %28
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = icmp sgt i32 %38, 0
  br i1 %39, label %40, label %82

40:                                               ; preds = %37
  %41 = load ptr, ptr @curwp, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.WINDOW, ptr %41, i32 0, i32 3
  %43 = load ptr, ptr %42, align 8, !tbaa !11
  %44 = getelementptr inbounds %struct.LINE, ptr %43, i32 0, i32 3
  %45 = load i16, ptr %44, align 2, !tbaa !13
  %46 = sext i16 %45 to i32
  %47 = and i32 65535, %46
  %48 = load ptr, ptr @curwp, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.WINDOW, ptr %48, i32 0, i32 4
  %50 = load i32, ptr %49, align 8, !tbaa !16
  %51 = sub nsw i32 %47, %50
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %6, align 4, !tbaa !5
  %53 = load ptr, ptr @curwp, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.WINDOW, ptr %53, i32 0, i32 3
  %55 = load ptr, ptr %54, align 8, !tbaa !11
  %56 = getelementptr inbounds %struct.LINE, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !17
  store ptr %57, ptr %8, align 8, !tbaa !9
  br label %58

58:                                               ; preds = %69, %40
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = add nsw i32 %59, -1
  store i32 %60, ptr %5, align 4, !tbaa !5
  %61 = icmp ne i32 %60, 0
  br i1 %61, label %62, label %81

62:                                               ; preds = %58
  %63 = load ptr, ptr %8, align 8, !tbaa !9
  %64 = load ptr, ptr @curbp, align 8, !tbaa !9
  %65 = getelementptr inbounds %struct.BUFFER, ptr %64, i32 0, i32 5
  %66 = load ptr, ptr %65, align 8, !tbaa !18
  %67 = icmp eq ptr %63, %66
  br i1 %67, label %68, label %69

68:                                               ; preds = %62
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %96

69:                                               ; preds = %62
  %70 = load ptr, ptr %8, align 8, !tbaa !9
  %71 = getelementptr inbounds %struct.LINE, ptr %70, i32 0, i32 3
  %72 = load i16, ptr %71, align 2, !tbaa !13
  %73 = sext i16 %72 to i32
  %74 = and i32 65535, %73
  %75 = add nsw i32 %74, 1
  %76 = load i32, ptr %6, align 4, !tbaa !5
  %77 = add nsw i32 %76, %75
  store i32 %77, ptr %6, align 4, !tbaa !5
  %78 = load ptr, ptr %8, align 8, !tbaa !9
  %79 = getelementptr inbounds %struct.LINE, ptr %78, i32 0, i32 0
  %80 = load ptr, ptr %79, align 8, !tbaa !17
  store ptr %80, ptr %8, align 8, !tbaa !9
  br label %58, !llvm.loop !20

81:                                               ; preds = %58
  br label %83

82:                                               ; preds = %37
  call void (ptr, ...) @mlwrite(ptr noundef @.str.12)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %96

83:                                               ; preds = %81
  br label %84

84:                                               ; preds = %83, %31
  br label %85

85:                                               ; preds = %84, %27
  %86 = load i32, ptr %6, align 4, !tbaa !5
  %87 = call i32 @ldelete(i32 noundef %86, i32 noundef 1)
  store i32 %87, ptr %7, align 4, !tbaa !5
  %88 = load i32, ptr %4, align 4, !tbaa !5
  %89 = icmp eq i32 %88, 1
  br i1 %89, label %90, label %94

90:                                               ; preds = %85
  %91 = load i32, ptr %7, align 4, !tbaa !5
  %92 = icmp eq i32 %91, 1
  br i1 %92, label %93, label %94

93:                                               ; preds = %90
  call void (ptr, ...) @mlwrite(ptr noundef @.str.13)
  br label %94

94:                                               ; preds = %93, %90, %85
  %95 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %95, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %96

96:                                               ; preds = %94, %82, %68
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %97 = load i32, ptr %3, align 4
  ret i32 %97
}

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 = !{!"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 = !{!12, !10, i64 24}
!12 = !{!"WINDOW", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !10, i64 40, !6, i64 48, !6, i64 52, !6, i64 56, !6, i64 60, !6, i64 64, !6, i64 68}
!13 = !{!14, !15, i64 18}
!14 = !{!"LINE", !10, i64 0, !10, i64 8, !15, i64 16, !15, i64 18, !7, i64 20}
!15 = !{!"short", !7, i64 0}
!16 = !{!12, !6, i64 32}
!17 = !{!14, !10, i64 0}
!18 = !{!19, !10, i64 40}
!19 = !{!"BUFFER", !10, i64 0, !10, i64 8, !6, i64 16, !10, i64 24, !6, i64 32, !10, i64 40, !6, i64 48, !6, i64 52, !7, i64 56, !7, i64 184}
!20 = distinct !{!20, !21}
!21 = !{!"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"

@curbp = external local_unnamed_addr global ptr, align 8
@curwp = external local_unnamed_addr global ptr, align 8
@.str.12 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [9 x i8], align 1

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

declare i32 @ldelete(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @killtxt(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq i32 %0, 0
  br i1 %3, label %4, label %14

4:                                                ; preds = %2
  %5 = load ptr, ptr @curwp, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %7, i64 18
  %9 = load i16, ptr %8, align 2, !tbaa !12
  %10 = zext i16 %9 to i32
  %11 = getelementptr inbounds i8, ptr %5, i64 32
  %12 = load i32, ptr %11, align 8, !tbaa !15
  %13 = sub nsw i32 %10, %12
  %spec.store.select = tail call i32 @llvm.smax.i32(i32 %13, i32 1)
  br label %.loopexit

14:                                               ; preds = %2
  %15 = icmp eq i32 %1, 0
  br i1 %15, label %16, label %20

16:                                               ; preds = %14
  %17 = load ptr, ptr @curwp, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 32
  %19 = load i32, ptr %18, align 8, !tbaa !15
  store i32 0, ptr %18, align 8, !tbaa !15
  br label %.loopexit

20:                                               ; preds = %14
  %21 = icmp sgt i32 %1, 0
  br i1 %21, label %22, label %.loopexit19.sink.split

22:                                               ; preds = %20
  %23 = load ptr, ptr @curwp, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %23, i64 24
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds i8, ptr %25, i64 18
  %27 = load i16, ptr %26, align 2, !tbaa !12
  %28 = zext i16 %27 to i32
  %29 = getelementptr inbounds i8, ptr %23, i64 32
  %30 = load i32, ptr %29, align 8, !tbaa !15
  %reass.sub = sub i32 %28, %30
  %31 = add i32 %reass.sub, 1
  %32 = add nsw i32 %1, -1
  %.not22 = icmp eq i32 %32, 0
  br i1 %.not22, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %22
  %33 = load ptr, ptr @curbp, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i64 40
  %35 = load ptr, ptr %34, align 8, !tbaa !16
  br label %36

36:                                               ; preds = %.lr.ph, %39
  %37 = phi i32 [ %32, %.lr.ph ], [ %45, %39 ]
  %.024.in = phi ptr [ %25, %.lr.ph ], [ %.024, %39 ]
  %.01523 = phi i32 [ %31, %.lr.ph ], [ %44, %39 ]
  %.024 = load ptr, ptr %.024.in, align 8, !tbaa !18
  %38 = icmp eq ptr %.024, %35
  br i1 %38, label %.loopexit19, label %39

39:                                               ; preds = %36
  %40 = getelementptr inbounds i8, ptr %.024, i64 18
  %41 = load i16, ptr %40, align 2, !tbaa !12
  %42 = zext i16 %41 to i32
  %43 = add i32 %.01523, 1
  %44 = add i32 %43, %42
  %45 = add nsw i32 %37, -1
  %.not = icmp eq i32 %45, 0
  br i1 %.not, label %.loopexit, label %36, !llvm.loop !19

.loopexit:                                        ; preds = %39, %22, %16, %4
  %.1 = phi i32 [ %spec.store.select, %4 ], [ %19, %16 ], [ %31, %22 ], [ %44, %39 ]
  %46 = tail call i32 @ldelete(i32 noundef %.1, i32 noundef 1) #3
  %47 = icmp eq i32 %0, 1
  %48 = icmp eq i32 %46, 1
  %or.cond = select i1 %47, i1 %48, i1 false
  br i1 %or.cond, label %.loopexit19.sink.split, label %.loopexit19

.loopexit19.sink.split:                           ; preds = %.loopexit, %20
  %.str.13.sink = phi ptr [ @.str.12, %20 ], [ @.str.13, %.loopexit ]
  %.014.ph = phi i32 [ 0, %20 ], [ 1, %.loopexit ]
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull %.str.13.sink) #3
  br label %.loopexit19

.loopexit19:                                      ; preds = %36, %.loopexit19.sink.split, %.loopexit
  %.014 = phi i32 [ %46, %.loopexit ], [ %.014.ph, %.loopexit19.sink.split ], [ 0, %36 ]
  ret i32 %.014
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !6, i64 24}
!10 = !{!"WINDOW", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !14, i64 18}
!13 = !{!"LINE", !6, i64 0, !6, i64 8, !14, i64 16, !14, i64 18, !7, i64 20}
!14 = !{!"short", !7, i64 0}
!15 = !{!10, !11, i64 32}
!16 = !{!17, !6, i64 40}
!17 = !{!"BUFFER", !6, i64 0, !6, i64 8, !11, i64 16, !6, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !7, i64 56, !7, i64 184}
!18 = !{!13, !6, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass