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

@curwp = external global ptr, align 8

; 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 @forwline(i32 noundef, i32 noundef) #1

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

9:                                                ; preds = %2
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %12, label %24

12:                                               ; preds = %9, %2
  %13 = load ptr, ptr @curwp, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.WINDOW, ptr %13, i32 0, i32 8
  %15 = load i32, ptr %14, align 8, !tbaa !11
  %16 = sub nsw i32 %15, 2
  store i32 %16, ptr %6, align 4, !tbaa !5
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = icmp sle i32 %17, 0
  br i1 %18, label %19, label %20

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

20:                                               ; preds = %19, %12
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = mul nsw i32 %22, %21
  store i32 %23, ptr %4, align 4, !tbaa !5
  br label %30

24:                                               ; preds = %9
  %25 = load ptr, ptr @curwp, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.WINDOW, ptr %25, i32 0, i32 8
  %27 = load i32, ptr %26, align 8, !tbaa !11
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = mul nsw i32 %28, %27
  store i32 %29, ptr %4, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %24, %20
  %31 = load ptr, ptr @curwp, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.WINDOW, ptr %31, i32 0, i32 2
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  store ptr %33, ptr %5, align 8, !tbaa !9
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %44, %30
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = load ptr, ptr @curwp, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.WINDOW, ptr %36, i32 0, i32 3
  %38 = load ptr, ptr %37, align 8, !tbaa !14
  %39 = icmp ne ptr %35, %38
  br i1 %39, label %40, label %47

40:                                               ; preds = %34
  %41 = load ptr, ptr %5, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.LINE, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !15
  store ptr %43, ptr %5, align 8, !tbaa !9
  br label %44

44:                                               ; preds = %40
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %6, align 4, !tbaa !5
  br label %34, !llvm.loop !18

47:                                               ; preds = %34
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = load ptr, ptr @curwp, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.WINDOW, ptr %49, i32 0, i32 10
  store i32 %48, ptr %50, align 8, !tbaa !20
  %51 = load ptr, ptr @curwp, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.WINDOW, ptr %51, i32 0, i32 11
  %53 = load i32, ptr %52, align 4, !tbaa !21
  %54 = or i32 %53, 1
  store i32 %54, ptr %52, align 4, !tbaa !21
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = call i32 @forwline(i32 noundef 0, i32 noundef %55)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i32 %56
}

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

@curwp = external local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
define dso_local i32 @forwpage(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr @curwp, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 56
  %5 = load i32, ptr %4, align 8, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %3, i64 16
  %7 = getelementptr inbounds i8, ptr %3, i64 24
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  br label %9

9:                                                ; preds = %9, %2
  %.011.in = phi ptr [ %6, %2 ], [ %.011, %9 ]
  %.0 = phi i32 [ 1, %2 ], [ %10, %9 ]
  %.011 = load ptr, ptr %.011.in, align 8, !tbaa !5
  %.not = icmp eq ptr %.011, %8
  %10 = add nuw nsw i32 %.0, 1
  br i1 %.not, label %11, label %9, !llvm.loop !13

11:                                               ; preds = %9
  %12 = icmp eq i32 %0, 0
  %13 = icmp eq i32 %1, -1
  %or.cond = or i1 %12, %13
  %14 = tail call i32 @llvm.smax.i32(i32 %5, i32 3)
  %spec.store.select = add nsw i32 %14, -2
  %spec.store.select.pn = select i1 %or.cond, i32 %spec.store.select, i32 %5
  %.012 = mul nsw i32 %spec.store.select.pn, %1
  %15 = getelementptr inbounds i8, ptr %3, i64 64
  store i32 %.0, ptr %15, align 8, !tbaa !15
  %16 = getelementptr inbounds i8, ptr %3, i64 68
  %17 = load i32, ptr %16, align 4, !tbaa !16
  %18 = or i32 %17, 1
  store i32 %18, ptr %16, align 4, !tbaa !16
  %19 = tail call i32 @forwline(i32 noundef 0, i32 noundef %.012)
  ret i32 %19
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.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, !11, i64 56}
!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 = !{!10, !6, i64 24}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!10, !11, i64 64}
!16 = !{!10, !11, i64 68}


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