; LLVM IR from kostya/crystal-benchmarks-game :: pidigits (pidigits)

; 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.__mpz_struct = type { i32, i32, ptr }

@tmp1 = external dso_local global [1 x %struct.__mpz_struct], align 16
@num = external dso_local global [1 x %struct.__mpz_struct], align 16
@tmp2 = external dso_local global [1 x %struct.__mpz_struct], align 16
@acc = external dso_local global [1 x %struct.__mpz_struct], align 16
@den = external dso_local global [1 x %struct.__mpz_struct], align 16
@.str = external hidden unnamed_addr constant [6 x i8], align 1

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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 0, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  %10 = load ptr, ptr %5, align 8, !tbaa !9
  %11 = getelementptr inbounds ptr, ptr %10, i64 1
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = call i32 @atoi(ptr noundef %12) #7
  store i32 %13, ptr %9, align 4, !tbaa !5
  call void @__gmpz_init(ptr noundef @tmp1)
  call void @__gmpz_init(ptr noundef @tmp2)
  call void @__gmpz_init_set_ui(ptr noundef @acc, i64 noundef 0)
  call void @__gmpz_init_set_ui(ptr noundef @den, i64 noundef 1)
  call void @__gmpz_init_set_ui(ptr noundef @num, i64 noundef 1)
  store i32 0, ptr %7, align 4, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %41, %29, %23, %2
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = load i32, ptr %9, align 4, !tbaa !5
  %17 = icmp ult i32 %15, %16
  br i1 %17, label %18, label %43

18:                                               ; preds = %14
  %19 = load i32, ptr %7, align 4, !tbaa !5
  %20 = add i32 %19, 1
  store i32 %20, ptr %7, align 4, !tbaa !5
  call void @next_term(i32 noundef %20)
  %21 = call i32 @__gmpz_cmp(ptr noundef @num, ptr noundef @acc) #7
  %22 = icmp sgt i32 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %18
  br label %14, !llvm.loop !11

24:                                               ; preds = %18
  %25 = call i32 @extract_digit(i32 noundef 3)
  store i32 %25, ptr %6, align 4, !tbaa !5
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = call i32 @extract_digit(i32 noundef 4)
  %28 = icmp ne i32 %26, %27
  br i1 %28, label %29, label %30

29:                                               ; preds = %24
  br label %14, !llvm.loop !11

30:                                               ; preds = %24
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = add i32 48, %31
  %33 = call i32 @putchar(i32 noundef %32)
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = add i32 %34, 1
  store i32 %35, ptr %8, align 4, !tbaa !5
  %36 = urem i32 %35, 10
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %41

38:                                               ; preds = %30
  %39 = load i32, ptr %8, align 4, !tbaa !5
  %40 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %39)
  br label %41

41:                                               ; preds = %38, %30
  %42 = load i32, ptr %6, align 4, !tbaa !5
  call void @eliminate_digit(i32 noundef %42)
  br label %14, !llvm.loop !11

43:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  ret i32 0
}

; Function Attrs: inlinehint nounwind willreturn memory(read) uwtable
declare i32 @atoi(ptr noundef nonnull) #2

declare void @__gmpz_init(ptr noundef) #3

declare void @__gmpz_init_set_ui(ptr noundef, i64 noundef) #3

; Function Attrs: nounwind willreturn memory(read)
declare i32 @__gmpz_cmp(ptr noundef, ptr noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare i32 @putchar(i32 noundef) #5

declare i32 @printf(ptr noundef, ...) #3

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint nounwind willreturn memory(read) 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 = { "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 #4 = { nounwind willreturn memory(read) "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 #5 = { inlinehint 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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(read) }

!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 = distinct !{!11, !12}
!12 = !{!"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"

%struct.__mpz_struct = type { i32, i32, ptr }

@tmp1 = external dso_local global [1 x %struct.__mpz_struct], align 16
@num = external dso_local global [1 x %struct.__mpz_struct], align 16
@tmp2 = external dso_local global [1 x %struct.__mpz_struct], align 16
@acc = external dso_local global [1 x %struct.__mpz_struct], align 16
@den = external dso_local global [1 x %struct.__mpz_struct], align 16
@.str = external hidden unnamed_addr constant [6 x i8], align 1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %1, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = tail call i32 @atoi(ptr nocapture noundef %4) #6
  tail call void @__gmpz_init(ptr noundef nonnull @tmp1) #7
  tail call void @__gmpz_init(ptr noundef nonnull @tmp2) #7
  tail call void @__gmpz_init_set_ui(ptr noundef nonnull @acc, i64 noundef 0) #7
  tail call void @__gmpz_init_set_ui(ptr noundef nonnull @den, i64 noundef 1) #7
  tail call void @__gmpz_init_set_ui(ptr noundef nonnull @num, i64 noundef 1) #7
  %.not16 = icmp eq i32 %5, 0
  br i1 %.not16, label %.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.outer
  %6 = phi i1 [ %26, %.outer ], [ true, %2 ]
  %.0.ph14 = phi i32 [ %.us-phi12, %.outer ], [ 0, %2 ]
  %.08.ph13 = phi i32 [ %21, %.outer ], [ 0, %2 ]
  br i1 %6, label %.lr.ph.split.us, label %.lr.ph.split, !llvm.loop !9

.lr.ph.split.us:                                  ; preds = %.lr.ph, %.lr.ph.split.us.backedge
  %.011.us = phi i32 [ %7, %.lr.ph.split.us.backedge ], [ %.0.ph14, %.lr.ph ]
  %7 = add i32 %.011.us, 1
  tail call void @next_term(i32 noundef %7)
  %8 = tail call i32 @__gmpz_cmp(ptr noundef nonnull @num, ptr noundef nonnull @acc) #6
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %.lr.ph.split.us.backedge, label %10

10:                                               ; preds = %.lr.ph.split.us
  %11 = tail call i32 @extract_digit(i32 noundef 3)
  %12 = tail call i32 @extract_digit(i32 noundef 4)
  %.not.us = icmp eq i32 %11, %12
  br i1 %.not.us, label %.split.us, label %.lr.ph.split.us.backedge

.lr.ph.split.us.backedge:                         ; preds = %10, %.lr.ph.split.us
  br label %.lr.ph.split.us

.lr.ph.split:                                     ; preds = %.lr.ph
  %13 = add i32 %.0.ph14, 1
  tail call void @next_term(i32 noundef %13)
  %14 = tail call i32 @__gmpz_cmp(ptr noundef nonnull @num, ptr noundef nonnull @acc) #6
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.outer._crit_edge, label %16

16:                                               ; preds = %.lr.ph.split
  %17 = tail call i32 @extract_digit(i32 noundef 3)
  %18 = tail call i32 @extract_digit(i32 noundef 4)
  %.not = icmp eq i32 %17, %18
  br i1 %.not, label %.split.us, label %.outer._crit_edge

.split.us:                                        ; preds = %10, %16
  %.us-phi = phi i32 [ %17, %16 ], [ %11, %10 ]
  %.us-phi12 = phi i32 [ %13, %16 ], [ %7, %10 ]
  %19 = add i32 %.us-phi, 48
  %20 = tail call i32 @putchar(i32 noundef %19)
  %21 = add nuw i32 %.08.ph13, 1
  %22 = urem i32 %21, 10
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %.outer

24:                                               ; preds = %.split.us
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %21)
  br label %.outer

.outer:                                           ; preds = %24, %.split.us
  tail call void @eliminate_digit(i32 noundef %.us-phi)
  %26 = icmp ult i32 %21, %5
  %exitcond.not = icmp eq i32 %21, %5
  br i1 %exitcond.not, label %.outer._crit_edge, label %.lr.ph, !llvm.loop !9

.outer._crit_edge:                                ; preds = %.lr.ph.split, %16, %.outer, %2
  ret i32 0
}

; Function Attrs: inlinehint mustprogress nofree nounwind willreturn memory(read) uwtable
declare i32 @atoi(ptr nocapture noundef nonnull) local_unnamed_addr #1

declare void @__gmpz_init(ptr noundef) local_unnamed_addr #2

declare void @__gmpz_init_set_ui(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(read)
declare i32 @__gmpz_cmp(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nofree nounwind uwtable
declare noundef i32 @putchar(i32 noundef) local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #5

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" }
attributes #1 = { inlinehint mustprogress nofree nounwind willreturn memory(read) 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 = { "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 = { mustprogress nofree nounwind willreturn memory(read) "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 #4 = { inlinehint nofree 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 #5 = { nofree nounwind "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 #6 = { nounwind willreturn memory(read) }
attributes #7 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass