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

@curwp = external global ptr, align 8
@curbp = external global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @forwchar(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 0
  br i1 %7, label %8, label %13

8:                                                ; preds = %2
  %9 = load i32, ptr %4, align 4, !tbaa !5
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = sub nsw i32 0, %10
  %12 = call i32 @backchar(i32 noundef %9, i32 noundef %11)
  store i32 %12, ptr %3, align 4
  br label %60

13:                                               ; preds = %2
  br label %14

14:                                               ; preds = %58, %13
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = add nsw i32 %15, -1
  store i32 %16, ptr %5, align 4, !tbaa !5
  %17 = icmp ne i32 %15, 0
  br i1 %17, label %18, label %59

18:                                               ; preds = %14
  %19 = load ptr, ptr @curwp, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.WINDOW, ptr %19, i32 0, i32 4
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = load ptr, ptr @curwp, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.WINDOW, ptr %22, i32 0, i32 3
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  %25 = getelementptr inbounds %struct.LINE, ptr %24, i32 0, i32 3
  %26 = load i16, ptr %25, align 2, !tbaa !14
  %27 = sext i16 %26 to i32
  %28 = and i32 65535, %27
  %29 = icmp eq i32 %21, %28
  br i1 %29, label %30, label %53

30:                                               ; preds = %18
  %31 = load ptr, ptr @curwp, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.WINDOW, ptr %31, i32 0, i32 3
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = load ptr, ptr @curbp, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.BUFFER, ptr %34, i32 0, i32 5
  %36 = load ptr, ptr %35, align 8, !tbaa !17
  %37 = icmp eq ptr %33, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %30
  store i32 0, ptr %3, align 4
  br label %60

39:                                               ; preds = %30
  %40 = load ptr, ptr @curwp, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.WINDOW, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %41, align 8, !tbaa !13
  %43 = getelementptr inbounds %struct.LINE, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !19
  %45 = load ptr, ptr @curwp, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.WINDOW, ptr %45, i32 0, i32 3
  store ptr %44, ptr %46, align 8, !tbaa !13
  %47 = load ptr, ptr @curwp, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.WINDOW, ptr %47, i32 0, i32 4
  store i32 0, ptr %48, align 8, !tbaa !11
  %49 = load ptr, ptr @curwp, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.WINDOW, ptr %49, i32 0, i32 11
  %51 = load i32, ptr %50, align 4, !tbaa !20
  %52 = or i32 %51, 2
  store i32 %52, ptr %50, align 4, !tbaa !20
  br label %58

53:                                               ; preds = %18
  %54 = load ptr, ptr @curwp, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.WINDOW, ptr %54, i32 0, i32 4
  %56 = load i32, ptr %55, align 8, !tbaa !11
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %55, align 8, !tbaa !11
  br label %58

58:                                               ; preds = %53, %39
  br label %14, !llvm.loop !21

59:                                               ; preds = %14
  store i32 1, ptr %3, align 4
  br label %60

60:                                               ; preds = %59, %38, %8
  %61 = load i32, ptr %3, align 4
  ret i32 %61
}

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

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

!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 32}
!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, !16, i64 18}
!15 = !{!"LINE", !10, i64 0, !10, i64 8, !16, i64 16, !16, i64 18, !7, i64 20}
!16 = !{!"short", !7, i64 0}
!17 = !{!18, !10, i64 40}
!18 = !{!"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}
!19 = !{!15, !10, i64 0}
!20 = !{!12, !6, i64 68}
!21 = distinct !{!21, !22}
!22 = !{!"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"

@curwp = external local_unnamed_addr global ptr, align 8
@curbp = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @forwchar(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %1, 0
  br i1 %3, label %10, label %.preheader

.preheader:                                       ; preds = %2
  %.not9 = icmp eq i32 %1, 0
  br i1 %.not9, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %4 = load ptr, ptr @curwp, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 32
  %6 = getelementptr inbounds i8, ptr %4, i64 24
  %7 = load ptr, ptr @curbp, align 8
  %8 = getelementptr inbounds i8, ptr %7, i64 40
  %9 = getelementptr inbounds i8, ptr %4, i64 68
  %.promoted = load i32, ptr %5, align 8, !tbaa !9
  %.promoted12 = load ptr, ptr %6, align 8, !tbaa !12
  br label %13

10:                                               ; preds = %2
  %11 = sub nsw i32 0, %1
  %12 = tail call i32 @backchar(i32 noundef %0, i32 noundef %11)
  br label %.loopexit

13:                                               ; preds = %.lr.ph, %30
  %14 = phi ptr [ %.promoted12, %.lr.ph ], [ %31, %30 ]
  %15 = phi i32 [ %.promoted, %.lr.ph ], [ %32, %30 ]
  %.in = phi i32 [ %1, %.lr.ph ], [ %16, %30 ]
  %16 = add nsw i32 %.in, -1
  %17 = getelementptr inbounds i8, ptr %14, i64 18
  %18 = load i16, ptr %17, align 2, !tbaa !13
  %19 = zext i16 %18 to i32
  %20 = icmp eq i32 %15, %19
  br i1 %20, label %21, label %28

21:                                               ; preds = %13
  %22 = load ptr, ptr %8, align 8, !tbaa !16
  %23 = icmp eq ptr %14, %22
  br i1 %23, label %.loopexit, label %24

24:                                               ; preds = %21
  %25 = load ptr, ptr %14, align 8, !tbaa !18
  store ptr %25, ptr %6, align 8, !tbaa !12
  store i32 0, ptr %5, align 8, !tbaa !9
  %26 = load i32, ptr %9, align 4, !tbaa !19
  %27 = or i32 %26, 2
  store i32 %27, ptr %9, align 4, !tbaa !19
  br label %30

28:                                               ; preds = %13
  %29 = add nsw i32 %15, 1
  store i32 %29, ptr %5, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %28, %24
  %31 = phi ptr [ %14, %28 ], [ %25, %24 ]
  %32 = phi i32 [ %29, %28 ], [ 0, %24 ]
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %.loopexit, label %13, !llvm.loop !20

.loopexit:                                        ; preds = %21, %30, %.preheader, %10
  %.05 = phi i32 [ %12, %10 ], [ 1, %.preheader ], [ 0, %21 ], [ 1, %30 ]
  ret i32 %.05
}

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

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

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


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