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

@curgoal = external global i32, align 4
@curwp = external global ptr, align 8
@curbp = external global ptr, align 8
@lastflag = external global i32, align 4
@curcol = external global i32, align 4
@thisflag = external global i32, align 4

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @forwline(i32 noundef %0, i32 noundef %1) #0 {
  %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
  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) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %2
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %88

12:                                               ; preds = %2
  %13 = load ptr, ptr @curwp, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.WINDOW, ptr %13, i32 0, i32 9
  %15 = load i32, ptr %14, align 4, !tbaa !11
  store i32 %15, ptr %7, align 4, !tbaa !5
  %16 = load i32, ptr @lastflag, align 4, !tbaa !5
  %17 = and i32 %16, 1
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %30

19:                                               ; preds = %12
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %27

22:                                               ; preds = %19
  %23 = load i32, ptr @curcol, align 4, !tbaa !5
  %24 = sub nsw i32 %23, 1
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = add nsw i32 %24, %25
  store i32 %26, ptr @curgoal, align 4, !tbaa !5
  br label %29

27:                                               ; preds = %19
  %28 = load i32, ptr @curcol, align 4, !tbaa !5
  store i32 %28, ptr @curgoal, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %27, %22
  br label %30

30:                                               ; preds = %29, %12
  %31 = load i32, ptr @thisflag, align 4, !tbaa !5
  %32 = or i32 %31, 1
  store i32 %32, ptr @thisflag, align 4, !tbaa !5
  %33 = load ptr, ptr @curwp, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.WINDOW, ptr %33, i32 0, i32 3
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  store ptr %35, ptr %6, align 8, !tbaa !9
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = icmp sgt i32 %36, 0
  br i1 %37, label %38, label %56

38:                                               ; preds = %30
  br label %39

39:                                               ; preds = %51, %38
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = add nsw i32 %40, -1
  store i32 %41, ptr %5, align 4, !tbaa !5
  %42 = icmp ne i32 %40, 0
  br i1 %42, label %43, label %49

43:                                               ; preds = %39
  %44 = load ptr, ptr %6, align 8, !tbaa !9
  %45 = load ptr, ptr @curbp, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.BUFFER, ptr %45, i32 0, i32 5
  %47 = load ptr, ptr %46, align 8, !tbaa !14
  %48 = icmp ne ptr %44, %47
  br label %49

49:                                               ; preds = %43, %39
  %50 = phi i1 [ false, %39 ], [ %48, %43 ]
  br i1 %50, label %51, label %55

51:                                               ; preds = %49
  %52 = load ptr, ptr %6, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.LINE, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !16
  store ptr %54, ptr %6, align 8, !tbaa !9
  br label %39, !llvm.loop !19

55:                                               ; preds = %49
  br label %76

56:                                               ; preds = %30
  br label %57

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

61:                                               ; preds = %57
  %62 = load ptr, ptr %6, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.LINE, ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %63, align 8, !tbaa !21
  %65 = load ptr, ptr @curbp, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.BUFFER, ptr %65, i32 0, i32 5
  %67 = load ptr, ptr %66, align 8, !tbaa !14
  %68 = icmp ne ptr %64, %67
  br label %69

69:                                               ; preds = %61, %57
  %70 = phi i1 [ false, %57 ], [ %68, %61 ]
  br i1 %70, label %71, label %75

71:                                               ; preds = %69
  %72 = load ptr, ptr %6, align 8, !tbaa !9
  %73 = getelementptr inbounds %struct.LINE, ptr %72, i32 0, i32 1
  %74 = load ptr, ptr %73, align 8, !tbaa !21
  store ptr %74, ptr %6, align 8, !tbaa !9
  br label %57, !llvm.loop !22

75:                                               ; preds = %69
  br label %76

76:                                               ; preds = %75, %55
  %77 = load ptr, ptr %6, align 8, !tbaa !9
  %78 = load ptr, ptr @curwp, align 8, !tbaa !9
  %79 = getelementptr inbounds %struct.WINDOW, ptr %78, i32 0, i32 3
  store ptr %77, ptr %79, align 8, !tbaa !13
  %80 = load ptr, ptr %6, align 8, !tbaa !9
  %81 = call i32 @getgoal(ptr noundef %80)
  %82 = load ptr, ptr @curwp, align 8, !tbaa !9
  %83 = getelementptr inbounds %struct.WINDOW, ptr %82, i32 0, i32 4
  store i32 %81, ptr %83, align 8, !tbaa !23
  %84 = load ptr, ptr @curwp, align 8, !tbaa !9
  %85 = getelementptr inbounds %struct.WINDOW, ptr %84, i32 0, i32 11
  %86 = load i32, ptr %85, align 4, !tbaa !24
  %87 = or i32 %86, 2
  store i32 %87, ptr %85, align 4, !tbaa !24
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %88

88:                                               ; preds = %76, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %89 = load i32, ptr %3, align 4
  ret i32 %89
}

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!12, !6, i64 60}
!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 = !{!12, !10, i64 24}
!14 = !{!15, !10, i64 40}
!15 = !{!"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}
!16 = !{!17, !10, i64 0}
!17 = !{!"LINE", !10, i64 0, !10, i64 8, !18, i64 16, !18, i64 18, !7, i64 20}
!18 = !{!"short", !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!17, !10, i64 8}
!22 = distinct !{!22, !20}
!23 = !{!12, !6, i64 32}
!24 = !{!12, !6, i64 68}


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

@curgoal = external local_unnamed_addr global i32, align 4
@curwp = external local_unnamed_addr global ptr, align 8
@curbp = external local_unnamed_addr global ptr, align 8
@lastflag = external local_unnamed_addr global i32, align 4
@curcol = external local_unnamed_addr global i32, align 4
@thisflag = external local_unnamed_addr global i32, align 4

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

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

4:                                                ; preds = %2
  %5 = load ptr, ptr @curwp, align 8, !tbaa !5
  %6 = load i32, ptr @lastflag, align 4, !tbaa !9
  %7 = and i32 %6, 1
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %.sink.split, label %13

.sink.split:                                      ; preds = %4
  %9 = getelementptr inbounds i8, ptr %5, i64 60
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = load i32, ptr @curcol, align 4, !tbaa !9
  %12 = tail call i32 @llvm.usub.sat.i32(i32 %10, i32 1)
  %.sink = add i32 %11, %12
  store i32 %.sink, ptr @curgoal, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %.sink.split, %4
  %14 = load i32, ptr @thisflag, align 4, !tbaa !9
  %15 = or i32 %14, 1
  store i32 %15, ptr @thisflag, align 4, !tbaa !9
  %16 = getelementptr inbounds i8, ptr %5, i64 24
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = icmp sgt i32 %1, 0
  %19 = load ptr, ptr @curbp, align 8
  %20 = getelementptr inbounds i8, ptr %19, i64 40
  br i1 %18, label %.preheader, label %.preheader23

.preheader:                                       ; preds = %13
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  br label %22

22:                                               ; preds = %.preheader, %23
  %.in = phi i32 [ %1, %.preheader ], [ %24, %23 ]
  %.01425 = phi ptr [ %17, %.preheader ], [ %25, %23 ]
  %.not22 = icmp eq ptr %.01425, %21
  br i1 %.not22, label %.critedge, label %23

23:                                               ; preds = %22
  %24 = add nsw i32 %.in, -1
  %25 = load ptr, ptr %.01425, align 8, !tbaa !16
  %.not21 = icmp eq i32 %24, 0
  br i1 %.not21, label %.critedge, label %22, !llvm.loop !19

.preheader23:                                     ; preds = %13, %26
  %.116 = phi i32 [ %27, %26 ], [ %1, %13 ]
  %.1 = phi ptr [ %29, %26 ], [ %17, %13 ]
  %.not19 = icmp eq i32 %.116, 0
  br i1 %.not19, label %.critedge, label %26

26:                                               ; preds = %.preheader23
  %27 = add nsw i32 %.116, 1
  %28 = getelementptr inbounds i8, ptr %.1, i64 8
  %29 = load ptr, ptr %28, align 8, !tbaa !21
  %30 = load ptr, ptr %20, align 8, !tbaa !14
  %.not20 = icmp eq ptr %29, %30
  br i1 %.not20, label %.critedge, label %.preheader23, !llvm.loop !22

.critedge:                                        ; preds = %26, %.preheader23, %22, %23
  %.2 = phi ptr [ %25, %23 ], [ %.01425, %22 ], [ %.1, %.preheader23 ], [ %.1, %26 ]
  store ptr %.2, ptr %16, align 8, !tbaa !13
  %31 = tail call i32 @getgoal(ptr noundef %.2)
  %32 = load ptr, ptr @curwp, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 32
  store i32 %31, ptr %33, align 8, !tbaa !23
  %34 = getelementptr inbounds i8, ptr %32, i64 68
  %35 = load i32, ptr %34, align 4, !tbaa !24
  %36 = or i32 %35, 2
  store i32 %36, ptr %34, align 4, !tbaa !24
  br label %37

37:                                               ; preds = %2, %.critedge
  ret i32 1
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.usub.sat.i32(i32, i32) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 60}
!12 = !{!"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}
!13 = !{!12, !6, i64 24}
!14 = !{!15, !6, i64 40}
!15 = !{!"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}
!16 = !{!17, !6, i64 0}
!17 = !{!"LINE", !6, i64 0, !6, i64 8, !18, i64 16, !18, i64 18, !7, i64 20}
!18 = !{!"short", !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!17, !6, i64 8}
!22 = distinct !{!22, !20}
!23 = !{!12, !10, i64 32}
!24 = !{!12, !10, i64 68}


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