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

@tabsize = external global i32, align 4
@curgoal = external global i32, align 4

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

7:                                                ; preds = %53, %1
  %8 = load i32, ptr %6, align 4, !tbaa !9
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.LINE, ptr %9, i32 0, i32 3
  %11 = load i16, ptr %10, align 2, !tbaa !11
  %12 = sext i16 %11 to i32
  %13 = and i32 65535, %12
  %14 = icmp slt i32 %8, %13
  br i1 %14, label %15, label %57

15:                                               ; preds = %7
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.LINE, ptr %16, i32 0, i32 4
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [32768 x i8], ptr %17, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !14
  %22 = sext i8 %21 to i32
  %23 = and i32 %22, 255
  store i32 %23, ptr %3, align 4, !tbaa !9
  %24 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %24, ptr %5, align 4, !tbaa !9
  %25 = load i32, ptr %3, align 4, !tbaa !9
  %26 = icmp eq i32 %25, 9
  br i1 %26, label %27, label %36

27:                                               ; preds = %15
  %28 = load i32, ptr @tabsize, align 4, !tbaa !9
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = load i32, ptr @tabsize, align 4, !tbaa !9
  %31 = srem i32 %29, %30
  %32 = sub nsw i32 %28, %31
  %33 = sub nsw i32 %32, 1
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = add nsw i32 %34, %33
  store i32 %35, ptr %5, align 4, !tbaa !9
  br label %46

36:                                               ; preds = %15
  %37 = load i32, ptr %3, align 4, !tbaa !9
  %38 = icmp slt i32 %37, 32
  br i1 %38, label %42, label %39

39:                                               ; preds = %36
  %40 = load i32, ptr %3, align 4, !tbaa !9
  %41 = icmp eq i32 %40, 127
  br i1 %41, label %42, label %45

42:                                               ; preds = %39, %36
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %5, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %42, %39
  br label %46

46:                                               ; preds = %45, %27
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %5, align 4, !tbaa !9
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = load i32, ptr @curgoal, align 4, !tbaa !9
  %51 = icmp sgt i32 %49, %50
  br i1 %51, label %52, label %53

52:                                               ; preds = %46
  br label %57

53:                                               ; preds = %46
  %54 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %54, ptr %4, align 4, !tbaa !9
  %55 = load i32, ptr %6, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %6, align 4, !tbaa !9
  br label %7, !llvm.loop !15

57:                                               ; preds = %52, %7
  %58 = load i32, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %58
}

; 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

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 = !{!"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, !13, i64 18}
!12 = !{!"LINE", !6, i64 0, !6, i64 8, !13, i64 16, !13, i64 18, !7, i64 20}
!13 = !{!"short", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

@tabsize = external local_unnamed_addr global i32, align 4
@curgoal = external local_unnamed_addr global i32, align 4

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @getgoal(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 18
  %3 = load i16, ptr %2, align 2, !tbaa !5
  %4 = zext i16 %3 to i32
  %.not24 = icmp eq i16 %3, 0
  br i1 %.not24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 20
  %6 = load i32, ptr @tabsize, align 4
  %7 = load i32, ptr @curgoal, align 4, !tbaa !11
  %wide.trip.count = zext i16 %3 to i64
  br label %8

8:                                                ; preds = %.lr.ph, %21
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %21 ]
  %.01720 = phi i32 [ 0, %.lr.ph ], [ %22, %21 ]
  %9 = getelementptr inbounds [32768 x i8], ptr %5, i64 0, i64 %indvars.iv
  %10 = load i8, ptr %9, align 1, !tbaa !13
  %11 = icmp eq i8 %10, 9
  br i1 %11, label %12, label %16

12:                                               ; preds = %8
  %13 = srem i32 %.01720, %6
  %14 = xor i32 %13, -1
  %15 = add i32 %6, %14
  br label %20

16:                                               ; preds = %8
  %17 = icmp ult i8 %10, 32
  %18 = icmp eq i8 %10, 127
  %or.cond = or i1 %17, %18
  %19 = zext i1 %or.cond to i32
  br label %20

20:                                               ; preds = %16, %12
  %.pn = phi i32 [ %15, %12 ], [ %19, %16 ]
  %.016 = add nsw i32 %.pn, %.01720
  %.not = icmp slt i32 %.016, %7
  br i1 %.not, label %21, label %._crit_edge.loopexit.split.loop.exit

21:                                               ; preds = %20
  %22 = add nsw i32 %.016, 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %8, !llvm.loop !14

._crit_edge.loopexit.split.loop.exit:             ; preds = %20
  %23 = trunc nuw nsw i64 %indvars.iv to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %21, %._crit_edge.loopexit.split.loop.exit, %1
  %.0.lcssa = phi i32 [ 0, %1 ], [ %23, %._crit_edge.loopexit.split.loop.exit ], [ %4, %21 ]
  ret i32 %.0.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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" }

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


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