; LLVM IR from freakout42/macrocalc :: mc/mex (random)

; 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] }

@curbp = external global ptr, align 8
@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

declare i32 @lnewline(...) #1

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

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

12:                                               ; preds = %2
  br label %13

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

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

30:                                               ; preds = %17
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  %32 = load ptr, ptr @curbp, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.BUFFER, ptr %32, i32 0, i32 5
  %34 = load ptr, ptr %33, align 8, !tbaa !17
  %35 = icmp ne ptr %31, %34
  br i1 %35, label %36, label %51

36:                                               ; preds = %30
  %37 = load ptr, ptr %6, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.LINE, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !19
  %40 = getelementptr inbounds %struct.LINE, ptr %39, i32 0, i32 3
  %41 = load i16, ptr %40, align 2, !tbaa !13
  %42 = sext i16 %41 to i32
  %43 = and i32 65535, %42
  %44 = icmp eq i32 %43, 0
  br i1 %44, label %45, label %51

45:                                               ; preds = %36
  %46 = call i32 @forwchar(i32 noundef 0, i32 noundef 1)
  store i32 %46, ptr %7, align 4, !tbaa !5
  %47 = icmp ne i32 %46, 1
  br i1 %47, label %48, label %50

48:                                               ; preds = %45
  %49 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %49, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %59

50:                                               ; preds = %45
  br label %57

51:                                               ; preds = %36, %30, %17
  %52 = call i32 (...) @lnewline()
  store i32 %52, ptr %7, align 4, !tbaa !5
  %53 = icmp ne i32 %52, 1
  br i1 %53, label %54, label %56

54:                                               ; preds = %51
  %55 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %55, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %59

56:                                               ; preds = %51
  br label %57

57:                                               ; preds = %56, %50
  br label %13, !llvm.loop !20

58:                                               ; preds = %13
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %59

59:                                               ; preds = %58, %54, %48, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %60 = load i32, ptr %3, align 4
  ret i32 %60
}

declare i32 @forwchar(i32 noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #3 = { 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, !10, i64 24}
!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 = !{!14, !15, i64 18}
!14 = !{!"LINE", !10, i64 0, !10, i64 8, !15, i64 16, !15, i64 18, !7, i64 20}
!15 = !{!"short", !7, i64 0}
!16 = !{!12, !6, i64 32}
!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 = !{!14, !10, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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"

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

declare i32 @lnewline(...) local_unnamed_addr #0

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

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

.lr.ph:                                           ; preds = %.preheader, %27
  %.in = phi i32 [ %4, %27 ], [ %1, %.preheader ]
  %4 = add nsw i32 %.in, -1
  %5 = load ptr, ptr @curwp, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %7, i64 18
  %9 = load i16, ptr %8, align 2, !tbaa !12
  %10 = zext i16 %9 to i32
  %11 = getelementptr inbounds i8, ptr %5, i64 32
  %12 = load i32, ptr %11, align 8, !tbaa !15
  %13 = icmp eq i32 %12, %10
  br i1 %13, label %14, label %25

14:                                               ; preds = %.lr.ph
  %15 = load ptr, ptr @curbp, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 40
  %17 = load ptr, ptr %16, align 8, !tbaa !16
  %.not12 = icmp eq ptr %7, %17
  br i1 %.not12, label %25, label %18

18:                                               ; preds = %14
  %19 = load ptr, ptr %7, align 8, !tbaa !18
  %20 = getelementptr inbounds i8, ptr %19, i64 18
  %21 = load i16, ptr %20, align 2, !tbaa !12
  %22 = icmp eq i16 %21, 0
  br i1 %22, label %23, label %25

23:                                               ; preds = %18
  %24 = tail call i32 @forwchar(i32 noundef 0, i32 noundef 1) #2
  %.not14 = icmp eq i32 %24, 1
  br i1 %.not14, label %27, label %.loopexit

25:                                               ; preds = %18, %14, %.lr.ph
  %26 = tail call i32 (...) @lnewline() #2
  %.not13 = icmp eq i32 %26, 1
  br i1 %.not13, label %27, label %.loopexit

27:                                               ; preds = %25, %23
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !19

.loopexit:                                        ; preds = %23, %25, %27, %.preheader, %2
  %.0 = phi i32 [ 0, %2 ], [ 1, %.preheader ], [ %24, %23 ], [ %26, %25 ], [ 1, %27 ]
  ret i32 %.0
}

declare i32 @forwchar(i32 noundef, i32 noundef) local_unnamed_addr #0

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


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