; LLVM IR from MostafaTwfiq/C-DataStructures-And-Algorithms :: Algorithms/IntegersAlg/Sources (IntegerAlg)

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

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [33 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local ptr @intToCharArr(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i16, align 2
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #5
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 0
  %9 = zext i1 %8 to i64
  %10 = select i1 %8, i32 1, i32 0
  %11 = trunc i32 %10 to i16
  store i16 %11, ptr %3, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = call i32 @integerNumOfDigits(i32 noundef %12)
  store i32 %13, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  %16 = load i16, ptr %3, align 2, !tbaa !9
  %17 = sext i16 %16 to i32
  %18 = add nsw i32 %15, %17
  %19 = sext i32 %18 to i64
  %20 = mul i64 1, %19
  %21 = call noalias ptr @malloc(i64 noundef %20) #6
  store ptr %21, ptr %5, align 8, !tbaa !11
  %22 = load ptr, ptr %5, align 8, !tbaa !11
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %27

24:                                               ; preds = %1
  %25 = load ptr, ptr @stderr, align 8, !tbaa !11
  %26 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %25, ptr noundef @.str, ptr noundef @.str.1, ptr noundef @.str.2) #5
  call void @exit(i32 noundef -833811484) #7
  unreachable

27:                                               ; preds = %1
  %28 = load ptr, ptr %5, align 8, !tbaa !11
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = load i16, ptr %3, align 2, !tbaa !9
  %31 = sext i16 %30 to i32
  %32 = add nsw i32 %29, %31
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i8, ptr %28, i64 %33
  store i8 0, ptr %34, align 1, !tbaa !13
  %35 = load i16, ptr %3, align 2, !tbaa !9
  %36 = icmp ne i16 %35, 0
  br i1 %36, label %37, label %41

37:                                               ; preds = %27
  %38 = load ptr, ptr %5, align 8, !tbaa !11
  store i8 45, ptr %38, align 1, !tbaa !13
  %39 = load i32, ptr %2, align 4, !tbaa !5
  %40 = mul nsw i32 %39, -1
  store i32 %40, ptr %2, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %37, %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %42

42:                                               ; preds = %62, %41
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %45 = icmp slt i32 %43, %44
  br i1 %45, label %47, label %46

46:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %67

47:                                               ; preds = %42
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = srem i32 %48, 10
  %50 = add nsw i32 %49, 48
  %51 = trunc i32 %50 to i8
  %52 = load ptr, ptr %5, align 8, !tbaa !11
  %53 = load i32, ptr %4, align 4, !tbaa !5
  %54 = load i16, ptr %3, align 2, !tbaa !9
  %55 = sext i16 %54 to i32
  %56 = add nsw i32 %53, %55
  %57 = sub nsw i32 %56, 1
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = sub nsw i32 %57, %58
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i8, ptr %52, i64 %60
  store i8 %51, ptr %61, align 1, !tbaa !13
  br label %62

62:                                               ; preds = %47
  %63 = load i32, ptr %6, align 4, !tbaa !5
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %6, align 4, !tbaa !5
  %65 = load i32, ptr %2, align 4, !tbaa !5
  %66 = sdiv i32 %65, 10
  store i32 %66, ptr %2, align 4, !tbaa !5
  br label %42, !llvm.loop !14

67:                                               ; preds = %46
  %68 = load ptr, ptr %5, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #5
  ret ptr %68
}

; 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: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #3

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #4

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

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 "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 = { noreturn 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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }
attributes #7 = { noreturn 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 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [33 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @intToCharArr(i32 noundef %0) local_unnamed_addr #0 {
  %.lobit = lshr i32 %0, 31
  %2 = tail call i32 @integerNumOfDigits(i32 noundef %0)
  %3 = add i32 %2, %.lobit
  %4 = add i32 %3, 1
  %5 = sext i32 %4 to i64
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #4
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %11

8:                                                ; preds = %1
  %9 = load ptr, ptr @stderr, align 8, !tbaa !5
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %9, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, ptr noundef nonnull @.str.2) #5
  tail call void @exit(i32 noundef -833811484) #6
  unreachable

11:                                               ; preds = %1
  %12 = sext i32 %3 to i64
  %13 = getelementptr inbounds i8, ptr %6, i64 %12
  store i8 0, ptr %13, align 1, !tbaa !9
  %.not = icmp sgt i32 %0, -1
  br i1 %.not, label %16, label %14

14:                                               ; preds = %11
  store i8 45, ptr %6, align 1, !tbaa !9
  %15 = sub nsw i32 0, %0
  br label %16

16:                                               ; preds = %14, %11
  %.020 = phi i32 [ %15, %14 ], [ %0, %11 ]
  %17 = icmp sgt i32 %2, 0
  br i1 %17, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %16
  %wide.trip.count = zext nneg i32 %2 to i64
  %18 = getelementptr i8, ptr %6, i64 %12
  %xtraiter = and i64 %wide.trip.count, 1
  %19 = icmp eq i32 %2, 1
  br i1 %19, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa.loopexit:          ; preds = %.lr.ph
  %20 = sub nuw i64 -3, %indvars.iv
  br label %._crit_edge.loopexit.unr-lcssa

._crit_edge.loopexit.unr-lcssa:                   ; preds = %._crit_edge.loopexit.unr-lcssa.loopexit, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ -1, %.lr.ph.preheader ], [ %20, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.122.unr = phi i32 [ %.020, %.lr.ph.preheader ], [ %36, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %21 = srem i32 %.122.unr, 10
  %22 = trunc nsw i32 %21 to i8
  %23 = add nsw i8 %22, 48
  %24 = getelementptr i8, ptr %18, i64 %indvars.iv.unr
  store i8 %23, ptr %24, align 1, !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %16
  ret ptr %6

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %.122 = phi i32 [ %.020, %.lr.ph.preheader.new ], [ %36, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %25 = srem i32 %.122, 10
  %26 = trunc nsw i32 %25 to i8
  %27 = add nsw i8 %26, 48
  %28 = xor i64 %indvars.iv, -1
  %29 = getelementptr i8, ptr %18, i64 %28
  store i8 %27, ptr %29, align 1, !tbaa !9
  %30 = sdiv i32 %.122, 10
  %31 = srem i32 %30, 10
  %32 = trunc nsw i32 %31 to i8
  %33 = add nsw i8 %32, 48
  %34 = xor i64 %indvars.iv, -2
  %35 = getelementptr i8, ptr %18, i64 %34
  store i8 %33, ptr %35, align 1, !tbaa !9
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %36 = sdiv i32 %.122, 100
  %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.loopexit, label %.lr.ph, !llvm.loop !10
}

; 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: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #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 = { 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 = { 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 #3 = { noreturn 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 #4 = { nounwind allocsize(0) }
attributes #5 = { cold nounwind }
attributes #6 = { noreturn 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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