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

@curwp = external global ptr, align 8
@kused = external global i32, align 4
@.str.14 = external hidden unnamed_addr constant [13 x i8], align 1
@thisflag = external global i32, align 4
@.str.15 = external hidden unnamed_addr constant [9 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare void @mlwrite(ptr noundef, ...) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @tnewline(i32 noundef, i32 noundef) #2

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

12:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %65

13:                                               ; preds = %2
  %14 = load i32, ptr @kused, align 4, !tbaa !5
  %15 = icmp sgt i32 %14, 512
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  call void (ptr, ...) @mlwrite(ptr noundef @.str.14)
  br label %17

17:                                               ; preds = %16, %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
  %21 = load ptr, ptr @curwp, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.WINDOW, ptr %21, i32 0, i32 2
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  %24 = icmp eq ptr %20, %23
  %25 = zext i1 %24 to i32
  store i32 %25, ptr %8, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %51, %17
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = add nsw i32 %27, -1
  store i32 %28, ptr %5, align 4, !tbaa !5
  %29 = icmp ne i32 %27, 0
  br i1 %29, label %30, label %52

30:                                               ; preds = %26
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %50, %30
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %7, align 4, !tbaa !5
  %34 = call i32 @kremove(i32 noundef %32)
  store i32 %34, ptr %6, align 4, !tbaa !5
  %35 = icmp sge i32 %34, 0
  br i1 %35, label %36, label %51

36:                                               ; preds = %31
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = icmp eq i32 %37, 10
  br i1 %38, label %39, label %44

39:                                               ; preds = %36
  %40 = call i32 @tnewline(i32 noundef 0, i32 noundef 1)
  %41 = icmp ne i32 %40, 1
  br i1 %41, label %42, label %43

42:                                               ; preds = %39
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %65

43:                                               ; preds = %39
  br label %50

44:                                               ; preds = %36
  %45 = load i32, ptr %6, align 4, !tbaa !5
  %46 = call i32 @linsert(i32 noundef 1, i32 noundef %45, i32 noundef 0)
  %47 = icmp ne i32 %46, 1
  br i1 %47, label %48, label %49

48:                                               ; preds = %44
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %65

49:                                               ; preds = %44
  br label %50

50:                                               ; preds = %49, %43
  br label %31, !llvm.loop !14

51:                                               ; preds = %31
  br label %26, !llvm.loop !16

52:                                               ; preds = %26
  %53 = load i32, ptr @thisflag, align 4, !tbaa !5
  %54 = or i32 %53, 4
  store i32 %54, ptr @thisflag, align 4, !tbaa !5
  %55 = load i32, ptr %8, align 4, !tbaa !5
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %57, label %64

57:                                               ; preds = %52
  %58 = load ptr, ptr @curwp, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.WINDOW, ptr %58, i32 0, i32 11
  %60 = load i32, ptr %59, align 4, !tbaa !17
  %61 = or i32 %60, 1
  store i32 %61, ptr %59, align 4, !tbaa !17
  %62 = load ptr, ptr @curwp, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.WINDOW, ptr %62, i32 0, i32 10
  store i32 0, ptr %63, align 8, !tbaa !18
  br label %64

64:                                               ; preds = %57, %52
  call void (ptr, ...) @mlwrite(ptr noundef @.str.15)
  store i32 1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %65

65:                                               ; preds = %64, %48, %42, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %66 = load i32, ptr %3, align 4
  ret i32 %66
}

declare i32 @kremove(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 = !{!12, !10, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!12, !6, i64 68}
!18 = !{!12, !6, i64 64}


; 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
@kused = external local_unnamed_addr global i32, align 4
@.str.14 = external hidden unnamed_addr constant [13 x i8], align 1
@thisflag = external local_unnamed_addr global i32, align 4
@.str.15 = external hidden unnamed_addr constant [9 x i8], align 1

declare void @mlwrite(ptr noundef, ...) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @yank(i32 noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = icmp slt i32 %1, 1
  br i1 %3, label %.loopexit13, label %4

4:                                                ; preds = %2
  %5 = load i32, ptr @kused, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 512
  br i1 %6, label %7, label %8

7:                                                ; preds = %4
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.14) #2
  br label %8

8:                                                ; preds = %7, %4
  %9 = load ptr, ptr @curwp, align 8, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %9, i64 24
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %9, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = icmp eq ptr %11, %13
  br label %.preheader

.loopexit:                                        ; preds = %25, %.preheader
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %29, label %.preheader, !llvm.loop !14

.preheader:                                       ; preds = %8, %.loopexit
  %.in = phi i32 [ %1, %8 ], [ %15, %.loopexit ]
  %15 = add nsw i32 %.in, -1
  %16 = tail call i32 @kremove(i32 noundef 0) #2
  %17 = icmp sgt i32 %16, -1
  br i1 %17, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader, %25
  %18 = phi i32 [ %27, %25 ], [ %16, %.preheader ]
  %19 = phi i32 [ %26, %25 ], [ 1, %.preheader ]
  %20 = icmp eq i32 %18, 10
  br i1 %20, label %21, label %23

21:                                               ; preds = %.lr.ph
  %22 = tail call i32 @tnewline(i32 noundef 0, i32 noundef 1)
  %.not12 = icmp eq i32 %22, 1
  br i1 %.not12, label %25, label %.loopexit13

23:                                               ; preds = %.lr.ph
  %24 = tail call i32 @linsert(i32 noundef 1, i32 noundef %18, i32 noundef 0) #2
  %.not11 = icmp eq i32 %24, 1
  br i1 %.not11, label %25, label %.loopexit13

25:                                               ; preds = %23, %21
  %26 = add nuw nsw i32 %19, 1
  %27 = tail call i32 @kremove(i32 noundef %19) #2
  %28 = icmp sgt i32 %27, -1
  br i1 %28, label %.lr.ph, label %.loopexit, !llvm.loop !16

29:                                               ; preds = %.loopexit
  %30 = load i32, ptr @thisflag, align 4, !tbaa !5
  %31 = or i32 %30, 4
  store i32 %31, ptr @thisflag, align 4, !tbaa !5
  br i1 %14, label %32, label %38

32:                                               ; preds = %29
  %33 = load ptr, ptr @curwp, align 8, !tbaa !9
  %34 = getelementptr inbounds i8, ptr %33, i64 68
  %35 = load i32, ptr %34, align 4, !tbaa !17
  %36 = or i32 %35, 1
  store i32 %36, ptr %34, align 4, !tbaa !17
  %37 = getelementptr inbounds i8, ptr %33, i64 64
  store i32 0, ptr %37, align 8, !tbaa !18
  br label %38

38:                                               ; preds = %32, %29
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.15) #2
  br label %.loopexit13

.loopexit13:                                      ; preds = %23, %21, %2, %38
  %.0 = phi i32 [ 1, %38 ], [ 0, %2 ], [ 0, %21 ], [ 0, %23 ]
  ret i32 %.0
}

declare i32 @kremove(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 = !{!"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 = !{!12, !10, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!12, !6, i64 68}
!18 = !{!12, !6, i64 64}


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