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

; Function Attrs: nounwind uwtable
define dso_local i32 @backchar(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
  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
  %8 = load i32, ptr %5, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 0
  br i1 %9, label %10, label %15

10:                                               ; preds = %2
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = sub nsw i32 0, %12
  %14 = call i32 @forwchar(i32 noundef %11, i32 noundef %13)
  store i32 %14, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %58

15:                                               ; preds = %2
  br label %16

16:                                               ; preds = %56, %15
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = add nsw i32 %17, -1
  store i32 %18, ptr %5, align 4, !tbaa !5
  %19 = icmp ne i32 %17, 0
  br i1 %19, label %20, label %57

20:                                               ; preds = %16
  %21 = load ptr, ptr @curwp, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.WINDOW, ptr %21, i32 0, i32 4
  %23 = load i32, ptr %22, align 8, !tbaa !11
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %51

25:                                               ; preds = %20
  %26 = load ptr, ptr @curwp, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.WINDOW, ptr %26, i32 0, i32 3
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  %29 = getelementptr inbounds %struct.LINE, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !14
  store ptr %30, ptr %6, align 8, !tbaa !9
  %31 = load ptr, ptr @curbp, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.BUFFER, ptr %31, i32 0, i32 5
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  %34 = icmp eq ptr %30, %33
  br i1 %34, label %35, label %36

35:                                               ; preds = %25
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %58

36:                                               ; preds = %25
  %37 = load ptr, ptr %6, align 8, !tbaa !9
  %38 = load ptr, ptr @curwp, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.WINDOW, ptr %38, i32 0, i32 3
  store ptr %37, ptr %39, align 8, !tbaa !13
  %40 = load ptr, ptr %6, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.LINE, ptr %40, i32 0, i32 3
  %42 = load i16, ptr %41, align 2, !tbaa !19
  %43 = sext i16 %42 to i32
  %44 = and i32 65535, %43
  %45 = load ptr, ptr @curwp, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.WINDOW, ptr %45, i32 0, i32 4
  store i32 %44, ptr %46, align 8, !tbaa !11
  %47 = load ptr, ptr @curwp, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.WINDOW, ptr %47, i32 0, i32 11
  %49 = load i32, ptr %48, align 4, !tbaa !20
  %50 = or i32 %49, 2
  store i32 %50, ptr %48, align 4, !tbaa !20
  br label %56

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

56:                                               ; preds = %51, %36
  br label %16, !llvm.loop !21

57:                                               ; preds = %16
  store i32 1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %58

58:                                               ; preds = %57, %35, %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %59 = load i32, ptr %3, align 4
  ret i32 %59
}

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 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, !10, i64 8}
!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, !16, i64 18}
!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
declare dso_local i32 @forwchar(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @backchar(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
  %.not10 = icmp eq i32 %1, 0
  br i1 %.not10, 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
  br label %13

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

13:                                               ; preds = %.lr.ph, %31
  %14 = phi i32 [ %.promoted, %.lr.ph ], [ %32, %31 ]
  %.in = phi i32 [ %1, %.lr.ph ], [ %15, %31 ]
  %15 = add nsw i32 %.in, -1
  %16 = icmp eq i32 %14, 0
  br i1 %16, label %17, label %29

17:                                               ; preds = %13
  %18 = load ptr, ptr %6, align 8, !tbaa !12
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = load ptr, ptr %8, align 8, !tbaa !16
  %22 = icmp eq ptr %20, %21
  br i1 %22, label %.loopexit, label %23

23:                                               ; preds = %17
  store ptr %20, ptr %6, align 8, !tbaa !12
  %24 = getelementptr inbounds i8, ptr %20, i64 18
  %25 = load i16, ptr %24, align 2, !tbaa !18
  %26 = zext i16 %25 to i32
  store i32 %26, ptr %5, align 8, !tbaa !9
  %27 = load i32, ptr %9, align 4, !tbaa !19
  %28 = or i32 %27, 2
  store i32 %28, ptr %9, align 4, !tbaa !19
  br label %31

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

31:                                               ; preds = %29, %23
  %32 = phi i32 [ %30, %29 ], [ %26, %23 ]
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %.loopexit, label %13, !llvm.loop !20

.loopexit:                                        ; preds = %17, %31, %.preheader, %10
  %.0 = phi i32 [ %12, %10 ], [ 1, %.preheader ], [ 0, %17 ], [ 1, %31 ]
  ret i32 %.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, !6, i64 8}
!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, !15, i64 18}
!19 = !{!10, !11, i64 68}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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