; LLVM IR from louk78/crypto_project :: bignum (bignum_tostring)

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

@NUMS = external dso_local global [11 x %struct.bignum], align 16

; Function Attrs: nounwind uwtable
declare dso_local ptr @bignum_alloc() #0

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; 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
declare dso_local void @bignum_free(ptr noundef) #0

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

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local void @bignum_copy(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @bignum_idivider(ptr noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @bignum_tostring(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 100, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %10 = load i32, ptr %3, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = mul i64 %11, 1
  %13 = call noalias ptr @malloc(i64 noundef %12) #5
  store ptr %13, ptr %6, align 8, !tbaa !5
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = call i32 @bignum_iszero(ptr noundef %14)
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %22

17:                                               ; preds = %1
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 0
  store i8 48, ptr %19, align 1, !tbaa !11
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 1
  store i8 0, ptr %21, align 1, !tbaa !11
  br label %103

22:                                               ; preds = %1
  %23 = call ptr @bignum_alloc()
  store ptr %23, ptr %7, align 8, !tbaa !5
  %24 = call ptr @bignum_alloc()
  store ptr %24, ptr %8, align 8, !tbaa !5
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  call void @bignum_copy(ptr noundef %25, ptr noundef %26)
  br label %27

27:                                               ; preds = %58, %22
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = call i32 @bignum_iszero(ptr noundef %28)
  %30 = icmp ne i32 %29, 0
  %31 = xor i1 %30, true
  br i1 %31, label %32, label %59

32:                                               ; preds = %27
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  call void @bignum_idivider(ptr noundef %33, ptr noundef getelementptr inbounds ([11 x %struct.bignum], ptr @NUMS, i64 0, i64 10), ptr noundef %34)
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.bignum, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !12
  %38 = getelementptr inbounds i32, ptr %37, i64 0
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = add i32 %39, 48
  %41 = trunc i32 %40 to i8
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load i32, ptr %4, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %4, align 4, !tbaa !9
  %45 = sext i32 %43 to i64
  %46 = getelementptr inbounds i8, ptr %42, i64 %45
  store i8 %41, ptr %46, align 1, !tbaa !11
  %47 = load i32, ptr %4, align 4, !tbaa !9
  %48 = load i32, ptr %3, align 4, !tbaa !9
  %49 = icmp sge i32 %47, %48
  br i1 %49, label %50, label %58

50:                                               ; preds = %32
  %51 = load i32, ptr %3, align 4, !tbaa !9
  %52 = mul nsw i32 %51, 2
  store i32 %52, ptr %3, align 4, !tbaa !9
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i32, ptr %3, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = mul i64 %55, 1
  %57 = call ptr @realloc(ptr noundef %53, i64 noundef %56) #6
  store ptr %57, ptr %6, align 8, !tbaa !5
  br label %58

58:                                               ; preds = %50, %32
  br label %27, !llvm.loop !14

59:                                               ; preds = %27
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %60

60:                                               ; preds = %93, %59
  %61 = load i32, ptr %5, align 4, !tbaa !9
  %62 = load i32, ptr %4, align 4, !tbaa !9
  %63 = sdiv i32 %62, 2
  %64 = icmp slt i32 %61, %63
  br i1 %64, label %65, label %96

65:                                               ; preds = %60
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = load i32, ptr %5, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds i8, ptr %66, i64 %68
  %70 = load i8, ptr %69, align 1, !tbaa !11
  %71 = sext i8 %70 to i32
  store i32 %71, ptr %9, align 4, !tbaa !9
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = load i32, ptr %4, align 4, !tbaa !9
  %74 = load i32, ptr %5, align 4, !tbaa !9
  %75 = sub nsw i32 %73, %74
  %76 = sub nsw i32 %75, 1
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i8, ptr %72, i64 %77
  %79 = load i8, ptr %78, align 1, !tbaa !11
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = load i32, ptr %5, align 4, !tbaa !9
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds i8, ptr %80, i64 %82
  store i8 %79, ptr %83, align 1, !tbaa !11
  %84 = load i32, ptr %9, align 4, !tbaa !9
  %85 = trunc i32 %84 to i8
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  %87 = load i32, ptr %4, align 4, !tbaa !9
  %88 = load i32, ptr %5, align 4, !tbaa !9
  %89 = sub nsw i32 %87, %88
  %90 = sub nsw i32 %89, 1
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds i8, ptr %86, i64 %91
  store i8 %85, ptr %92, align 1, !tbaa !11
  br label %93

93:                                               ; preds = %65
  %94 = load i32, ptr %5, align 4, !tbaa !9
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %5, align 4, !tbaa !9
  br label %60, !llvm.loop !16

96:                                               ; preds = %60
  %97 = load ptr, ptr %6, align 8, !tbaa !5
  %98 = load i32, ptr %4, align 4, !tbaa !9
  %99 = sext i32 %98 to i64
  %100 = getelementptr inbounds i8, ptr %97, i64 %99
  store i8 0, ptr %100, align 1, !tbaa !11
  %101 = load ptr, ptr %7, align 8, !tbaa !5
  call void @bignum_free(ptr noundef %101)
  %102 = load ptr, ptr %8, align 8, !tbaa !5
  call void @bignum_free(ptr noundef %102)
  br label %103

103:                                              ; preds = %96, %17
  %104 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret ptr %104
}

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 = { nounwind allocsize(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 allocsize(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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { nounwind allocsize(1) }

!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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@NUMS = external dso_local global [11 x %struct.bignum], align 16

; Function Attrs: nounwind uwtable
declare dso_local ptr @bignum_alloc() local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

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

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

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @bignum_copy(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @bignum_idivider(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noalias ptr @bignum_tostring(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call noalias dereferenceable_or_null(100) ptr @malloc(i64 noundef 100) #3
  %3 = tail call i32 @bignum_iszero(ptr noundef %0)
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %6, label %4

4:                                                ; preds = %1
  store i8 48, ptr %2, align 1, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %2, i64 1
  store i8 0, ptr %5, align 1, !tbaa !5
  br label %47

6:                                                ; preds = %1
  %7 = tail call ptr @bignum_alloc()
  %8 = tail call ptr @bignum_alloc()
  tail call void @bignum_copy(ptr noundef %0, ptr noundef %7)
  %9 = tail call i32 @bignum_iszero(ptr noundef %7)
  %.not3941 = icmp eq i32 %9, 0
  br i1 %.not3941, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %6
  %10 = getelementptr inbounds i8, ptr %8, i64 8
  br label %14

.preheader:                                       ; preds = %25
  %.not48 = icmp eq i64 %indvars.iv, 0
  br i1 %.not48, label %._crit_edge, label %.lr.ph47.preheader

.lr.ph47.preheader:                               ; preds = %.preheader
  %11 = lshr i64 %indvars.iv.next, 1
  %sext = shl i64 %indvars.iv.next, 32
  %12 = ashr exact i64 %sext, 32
  %wide.trip.count = and i64 %11, 2147483647
  %xtraiter = and i64 %11, 1
  %13 = icmp eq i64 %wide.trip.count, 1
  br i1 %13, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph47.preheader.new

.lr.ph47.preheader.new:                           ; preds = %.lr.ph47.preheader
  %unroll_iter = sub nsw i64 %wide.trip.count, %xtraiter
  br label %.lr.ph47

14:                                               ; preds = %.lr.ph, %25
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %25 ]
  %.044 = phi i32 [ 100, %.lr.ph ], [ %.1, %25 ]
  %.03742 = phi ptr [ %2, %.lr.ph ], [ %.138, %25 ]
  tail call void @bignum_idivider(ptr noundef %7, ptr noundef nonnull getelementptr inbounds (i8, ptr @NUMS, i64 160), ptr noundef %8)
  %15 = load ptr, ptr %10, align 8, !tbaa !8
  %16 = load i32, ptr %15, align 4, !tbaa !12
  %17 = trunc i32 %16 to i8
  %18 = add i8 %17, 48
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %19 = getelementptr inbounds i8, ptr %.03742, i64 %indvars.iv
  store i8 %18, ptr %19, align 1, !tbaa !5
  %20 = trunc nuw i64 %indvars.iv.next to i32
  %.not40 = icmp sgt i32 %.044, %20
  br i1 %.not40, label %25, label %21

21:                                               ; preds = %14
  %22 = shl nsw i32 %.044, 1
  %23 = sext i32 %22 to i64
  %24 = tail call ptr @realloc(ptr noundef nonnull %.03742, i64 noundef %23) #4
  br label %25

25:                                               ; preds = %21, %14
  %.138 = phi ptr [ %24, %21 ], [ %.03742, %14 ]
  %.1 = phi i32 [ %22, %21 ], [ %.044, %14 ]
  %26 = tail call i32 @bignum_iszero(ptr noundef %7)
  %.not39 = icmp eq i32 %26, 0
  br i1 %.not39, label %14, label %.preheader, !llvm.loop !13

.lr.ph47:                                         ; preds = %.lr.ph47, %.lr.ph47.preheader.new
  %indvars.iv50 = phi i64 [ 0, %.lr.ph47.preheader.new ], [ %indvars.iv.next51.1, %.lr.ph47 ]
  %niter = phi i64 [ 0, %.lr.ph47.preheader.new ], [ %niter.next.1, %.lr.ph47 ]
  %27 = getelementptr inbounds i8, ptr %.138, i64 %indvars.iv50
  %28 = load i8, ptr %27, align 1, !tbaa !5
  %29 = xor i64 %indvars.iv50, -1
  %30 = add nsw i64 %12, %29
  %31 = getelementptr inbounds i8, ptr %.138, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !5
  store i8 %32, ptr %27, align 1, !tbaa !5
  store i8 %28, ptr %31, align 1, !tbaa !5
  %indvars.iv.next51 = or disjoint i64 %indvars.iv50, 1
  %33 = getelementptr inbounds i8, ptr %.138, i64 %indvars.iv.next51
  %34 = load i8, ptr %33, align 1, !tbaa !5
  %35 = xor i64 %indvars.iv50, -2
  %36 = add nsw i64 %12, %35
  %37 = getelementptr inbounds i8, ptr %.138, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !5
  store i8 %38, ptr %33, align 1, !tbaa !5
  store i8 %34, ptr %37, align 1, !tbaa !5
  %indvars.iv.next51.1 = add nuw nsw i64 %indvars.iv50, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph47, !llvm.loop !15

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph47, %.lr.ph47.preheader
  %indvars.iv50.unr = phi i64 [ 0, %.lr.ph47.preheader ], [ %indvars.iv.next51.1, %.lr.ph47 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph47.epil

.lr.ph47.epil:                                    ; preds = %._crit_edge.loopexit.unr-lcssa
  %39 = getelementptr inbounds i8, ptr %.138, i64 %indvars.iv50.unr
  %40 = load i8, ptr %39, align 1, !tbaa !5
  %41 = xor i64 %indvars.iv50.unr, -1
  %42 = add nsw i64 %12, %41
  %43 = getelementptr inbounds i8, ptr %.138, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !5
  store i8 %44, ptr %39, align 1, !tbaa !5
  store i8 %40, ptr %43, align 1, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph47.epil, %._crit_edge.loopexit.unr-lcssa, %6, %.preheader
  %.035.lcssa57 = phi i64 [ %indvars.iv.next, %.preheader ], [ 0, %6 ], [ %indvars.iv.next, %._crit_edge.loopexit.unr-lcssa ], [ %indvars.iv.next, %.lr.ph47.epil ]
  %.037.lcssa56 = phi ptr [ %.138, %.preheader ], [ %2, %6 ], [ %.138, %._crit_edge.loopexit.unr-lcssa ], [ %.138, %.lr.ph47.epil ]
  %45 = and i64 %.035.lcssa57, 4294967295
  %46 = getelementptr inbounds i8, ptr %.037.lcssa56, i64 %45
  store i8 0, ptr %46, align 1, !tbaa !5
  tail call void @bignum_free(ptr noundef %7)
  tail call void @bignum_free(ptr noundef %8)
  br label %47

47:                                               ; preds = %._crit_edge, %4
  %.2 = phi ptr [ %2, %4 ], [ %.037.lcssa56, %._crit_edge ]
  ret ptr %.2
}

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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }
attributes #4 = { nounwind allocsize(1) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !11, i64 8}
!9 = !{!"", !10, i64 0, !10, i64 4, !11, i64 8}
!10 = !{!"int", !6, i64 0}
!11 = !{!"any pointer", !6, i64 0}
!12 = !{!10, !10, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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