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

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

@kbufp = external dso_local global ptr, align 8
@kused = external dso_local global i32, align 4
@ksize = external dso_local global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local i32 @kinsert(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %4 = load i32, ptr @kused, align 4, !tbaa !5
  %5 = load i32, ptr @ksize, align 4, !tbaa !5
  %6 = icmp sge i32 %4, %5
  br i1 %6, label %7, label %12

7:                                                ; preds = %1
  %8 = call i32 @enlrg_kbuf(i32 noundef 1)
  %9 = icmp ne i32 %8, 1
  br i1 %9, label %10, label %11

10:                                               ; preds = %7
  store i32 0, ptr %2, align 4
  br label %20

11:                                               ; preds = %7
  br label %12

12:                                               ; preds = %11, %1
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = trunc i32 %13 to i8
  %15 = load ptr, ptr @kbufp, align 8, !tbaa !9
  %16 = load i32, ptr @kused, align 4, !tbaa !5
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr @kused, align 4, !tbaa !5
  %18 = sext i32 %16 to i64
  %19 = getelementptr inbounds i8, ptr %15, i64 %18
  store i8 %14, ptr %19, align 1, !tbaa !11
  store i32 1, ptr %2, align 4
  br label %20

20:                                               ; preds = %12, %10
  %21 = load i32, ptr %2, align 4
  ret i32 %21
}

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 = !{!7, !7, i64 0}


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

@kbufp = external dso_local local_unnamed_addr global ptr, align 8
@kused = external dso_local local_unnamed_addr global i32, align 4
@ksize = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @kinsert(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @kused, align 4, !tbaa !5
  %3 = load i32, ptr @ksize, align 4, !tbaa !5
  %.not = icmp slt i32 %2, %3
  br i1 %.not, label %6, label %4

4:                                                ; preds = %1
  %5 = tail call i32 @enlrg_kbuf(i32 noundef 1)
  %.not2 = icmp eq i32 %5, 1
  br i1 %.not2, label %._crit_edge, label %13

._crit_edge:                                      ; preds = %4
  %.pre = load i32, ptr @kused, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %._crit_edge, %1
  %7 = phi i32 [ %.pre, %._crit_edge ], [ %2, %1 ]
  %8 = trunc i32 %0 to i8
  %9 = load ptr, ptr @kbufp, align 8, !tbaa !9
  %10 = add nsw i32 %7, 1
  store i32 %10, ptr @kused, align 4, !tbaa !5
  %11 = sext i32 %7 to i64
  %12 = getelementptr inbounds i8, ptr %9, i64 %11
  store i8 %8, ptr %12, align 1, !tbaa !11
  br label %13

13:                                               ; preds = %4, %6
  %.0 = phi i32 [ 1, %6 ], [ 0, %4 ]
  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 = !{!"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 = !{!7, !7, i64 0}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass