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

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

@lastflag = external global i32, align 4
@thisflag = external global i32, align 4
@nem = external hidden global [40 x i8], align 16
@.str.4 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.5 = 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

; 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 @getregion(ptr noundef) #1

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

declare void @kdelete(...) #2

declare i32 @enlrg_kbuf(...) #2

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

13:                                               ; preds = %2
  %14 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %14, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %70

15:                                               ; preds = %2
  %16 = getelementptr inbounds %struct.REGION, ptr %9, i32 0, i32 2
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = icmp sgt i32 %17, 512
  br i1 %18, label %19, label %20

19:                                               ; preds = %15
  call void (ptr, ...) @mlwrite(ptr noundef @.str.4)
  br label %20

20:                                               ; preds = %19, %15
  %21 = load i32, ptr @lastflag, align 4, !tbaa !5
  %22 = and i32 %21, 2
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %20
  call void (...) @kdelete()
  br label %25

25:                                               ; preds = %24, %20
  %26 = load i32, ptr @thisflag, align 4, !tbaa !5
  %27 = or i32 %26, 2
  store i32 %27, ptr @thisflag, align 4, !tbaa !5
  %28 = getelementptr inbounds %struct.REGION, ptr %9, i32 0, i32 2
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = call i32 (i32, ...) @enlrg_kbuf(i32 noundef %29)
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %25
  call void (ptr, ...) @mlwrite(ptr noundef @nem)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %70

33:                                               ; preds = %25
  %34 = getelementptr inbounds %struct.REGION, ptr %9, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !12
  store ptr %35, ptr %6, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct.REGION, ptr %9, i32 0, i32 1
  %37 = load i32, ptr %36, align 8, !tbaa !14
  store i32 %37, ptr %7, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %68, %33
  %39 = getelementptr inbounds %struct.REGION, ptr %9, i32 0, i32 2
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = add nsw i32 %40, -1
  store i32 %41, ptr %39, align 4, !tbaa !9
  %42 = icmp sgt i32 %40, 0
  br i1 %42, label %43, label %69

43:                                               ; preds = %38
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = load ptr, ptr %6, align 8, !tbaa !13
  %46 = getelementptr inbounds %struct.LINE, ptr %45, i32 0, i32 3
  %47 = load i16, ptr %46, align 2, !tbaa !15
  %48 = sext i16 %47 to i32
  %49 = and i32 65535, %48
  %50 = icmp eq i32 %44, %49
  br i1 %50, label %51, label %56

51:                                               ; preds = %43
  %52 = call i32 @kinsert(i32 noundef 10)
  %53 = load ptr, ptr %6, align 8, !tbaa !13
  %54 = getelementptr inbounds %struct.LINE, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !18
  store ptr %55, ptr %6, align 8, !tbaa !13
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %68

56:                                               ; preds = %43
  %57 = load ptr, ptr %6, align 8, !tbaa !13
  %58 = getelementptr inbounds %struct.LINE, ptr %57, i32 0, i32 4
  %59 = load i32, ptr %7, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [32768 x i8], ptr %58, i64 0, i64 %60
  %62 = load i8, ptr %61, align 1, !tbaa !19
  %63 = sext i8 %62 to i32
  %64 = and i32 %63, 255
  %65 = call i32 @kinsert(i32 noundef %64)
  %66 = load i32, ptr %7, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %7, align 4, !tbaa !5
  br label %68

68:                                               ; preds = %56, %51
  br label %38, !llvm.loop !20

69:                                               ; preds = %38
  call void (ptr, ...) @mlwrite(ptr noundef @.str.5)
  store i32 1, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %70

70:                                               ; preds = %69, %32, %13
  call void @llvm.lifetime.end.p0(i64 16, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %71 = load i32, ptr %3, align 4
  ret i32 %71
}

declare i32 @kinsert(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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 12}
!10 = !{!"", !11, i64 0, !6, i64 8, !6, i64 12}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 8}
!15 = !{!16, !17, i64 18}
!16 = !{!"LINE", !11, i64 0, !11, i64 8, !17, i64 16, !17, i64 18, !7, i64 20}
!17 = !{!"short", !7, i64 0}
!18 = !{!16, !11, i64 0}
!19 = !{!7, !7, i64 0}
!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"

%struct.REGION = type { ptr, i32, i32 }

@lastflag = external local_unnamed_addr global i32, align 4
@thisflag = external local_unnamed_addr global i32, align 4
@nem = external hidden global [40 x i8], align 16
@.str.4 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [9 x i8], align 1

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

; Function Attrs: mustprogress 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 @getregion(ptr noundef) local_unnamed_addr #1

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

declare void @kdelete(...) local_unnamed_addr #2

declare i32 @enlrg_kbuf(...) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @copyregion(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = alloca %struct.REGION, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  %4 = call i32 @getregion(ptr noundef nonnull %3)
  %.not = icmp eq i32 %4, 1
  br i1 %.not, label %5, label %47

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %3, i64 12
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 512
  br i1 %8, label %9, label %10

9:                                                ; preds = %5
  call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.4) #3
  br label %10

10:                                               ; preds = %9, %5
  %11 = load i32, ptr @lastflag, align 4, !tbaa !11
  %12 = and i32 %11, 2
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %10
  call void (...) @kdelete() #3
  br label %15

15:                                               ; preds = %14, %10
  %16 = load i32, ptr @thisflag, align 4, !tbaa !11
  %17 = or i32 %16, 2
  store i32 %17, ptr @thisflag, align 4, !tbaa !11
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = call i32 (i32, ...) @enlrg_kbuf(i32 noundef %18) #3
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %.sink.split, label %21

21:                                               ; preds = %15
  %22 = load ptr, ptr %3, align 8, !tbaa !12
  %23 = getelementptr inbounds i8, ptr %3, i64 8
  %24 = load i32, ptr %23, align 8, !tbaa !13
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = add nsw i32 %25, -1
  store i32 %26, ptr %6, align 4, !tbaa !5
  %27 = icmp sgt i32 %25, 0
  br i1 %27, label %.lr.ph, label %.sink.split

.lr.ph:                                           ; preds = %21, %43
  %.0712 = phi i32 [ %.1, %43 ], [ %24, %21 ]
  %.0811 = phi ptr [ %.19, %43 ], [ %22, %21 ]
  %28 = getelementptr inbounds i8, ptr %.0811, i64 18
  %29 = load i16, ptr %28, align 2, !tbaa !14
  %30 = zext i16 %29 to i32
  %31 = icmp eq i32 %.0712, %30
  br i1 %31, label %32, label %35

32:                                               ; preds = %.lr.ph
  %33 = call i32 @kinsert(i32 noundef 10) #3
  %34 = load ptr, ptr %.0811, align 8, !tbaa !17
  br label %43

35:                                               ; preds = %.lr.ph
  %36 = getelementptr inbounds i8, ptr %.0811, i64 20
  %37 = sext i32 %.0712 to i64
  %38 = getelementptr inbounds [32768 x i8], ptr %36, i64 0, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !18
  %40 = zext i8 %39 to i32
  %41 = call i32 @kinsert(i32 noundef %40) #3
  %42 = add nsw i32 %.0712, 1
  br label %43

43:                                               ; preds = %35, %32
  %.19 = phi ptr [ %34, %32 ], [ %.0811, %35 ]
  %.1 = phi i32 [ 0, %32 ], [ %42, %35 ]
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = add nsw i32 %44, -1
  store i32 %45, ptr %6, align 4, !tbaa !5
  %46 = icmp sgt i32 %44, 0
  br i1 %46, label %.lr.ph, label %.sink.split, !llvm.loop !19

.sink.split:                                      ; preds = %43, %21, %15
  %.str.5.sink = phi ptr [ @nem, %15 ], [ @.str.5, %21 ], [ @.str.5, %43 ]
  %.0.ph = phi i32 [ 0, %15 ], [ 1, %21 ], [ 1, %43 ]
  call void (ptr, ...) @mlwrite(ptr noundef nonnull %.str.5.sink) #3
  br label %47

47:                                               ; preds = %.sink.split, %2
  %.0 = phi i32 [ %4, %2 ], [ %.0.ph, %.sink.split ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  ret i32 %.0
}

declare i32 @kinsert(i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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, !10, i64 12}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!15, !16, i64 18}
!15 = !{!"LINE", !7, i64 0, !7, i64 8, !16, i64 16, !16, i64 18, !8, i64 20}
!16 = !{!"short", !8, i64 0}
!17 = !{!15, !7, i64 0}
!18 = !{!8, !8, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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