; LLVM IR from JessonYue/LeetCodeLearning :: Array/caiweixiong/c (homework_032)

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

@.str = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @toHex_1(i32 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 i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %1
  store ptr @.str, ptr %2, align 8
  br label %67

17:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 8, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 7, ptr %5, align 4, !tbaa !5
  %18 = call ptr @llvm.stacksave.p0()
  store ptr %18, ptr %6, align 8
  %19 = alloca i8, i64 8, align 16
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %20 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %20, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 15, ptr %9, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %24, %17
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %36

24:                                               ; preds = %21
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !5
  %27 = and i32 %25, %26
  store i32 %27, ptr %8, align 4, !tbaa !5
  %28 = load i32, ptr %8, align 4, !tbaa !5
  %29 = call signext i8 @getHexChar(i32 noundef %28)
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = add nsw i32 %30, -1
  store i32 %31, ptr %5, align 4, !tbaa !5
  %32 = sext i32 %30 to i64
  %33 = getelementptr inbounds i8, ptr %19, i64 %32
  store i8 %29, ptr %33, align 1, !tbaa !9
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = lshr i32 %34, 4
  store i32 %35, ptr %7, align 4, !tbaa !5
  br label %21, !llvm.loop !10

36:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = sub nsw i32 8, %37
  store i32 %38, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %39 = load i32, ptr %10, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = call noalias ptr @malloc(i64 noundef %40) #5
  store ptr %41, ptr %11, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %42 = load ptr, ptr %11, align 8, !tbaa !12
  store ptr %42, ptr %12, align 8, !tbaa !12
  %43 = load ptr, ptr %11, align 8, !tbaa !12
  %44 = load i32, ptr %10, align 4, !tbaa !5
  %45 = sub nsw i32 %44, 1
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %43, i64 %46
  store i8 0, ptr %47, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %13, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %55, %36
  %51 = load i32, ptr %13, align 4, !tbaa !5
  %52 = icmp slt i32 %51, 8
  br i1 %52, label %54, label %53

53:                                               ; preds = %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  br label %64

54:                                               ; preds = %50
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %13, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i8, ptr %19, i64 %57
  %59 = load i8, ptr %58, align 1, !tbaa !9
  %60 = load ptr, ptr %12, align 8, !tbaa !12
  %61 = getelementptr inbounds i8, ptr %60, i32 1
  store ptr %61, ptr %12, align 8, !tbaa !12
  store i8 %59, ptr %60, align 1, !tbaa !9
  %62 = load i32, ptr %13, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %13, align 4, !tbaa !5
  br label %50, !llvm.loop !14

64:                                               ; preds = %53
  %65 = load ptr, ptr %11, align 8, !tbaa !12
  store ptr %65, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  %66 = load ptr, ptr %6, align 8
  call void @llvm.stackrestore.p0(ptr %66)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %67

67:                                               ; preds = %64, %16
  %68 = load ptr, ptr %2, align 8
  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: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @getHexChar(i32 noundef) #0

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { 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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

!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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = distinct !{!14, !11}


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

@.str = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @toHex_1(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq i32 %0, 0
  br i1 %2, label %20, label %3

3:                                                ; preds = %1
  %4 = tail call ptr @llvm.stacksave.p0()
  %5 = alloca [8 x i8], align 16
  br label %6

6:                                                ; preds = %3, %6
  %indvar = phi i64 [ 0, %3 ], [ %indvar.next, %6 ]
  %indvars.iv = phi i64 [ 7, %3 ], [ %indvars.iv.next, %6 ]
  %.02325 = phi i32 [ %0, %3 ], [ %10, %6 ]
  %7 = and i32 %.02325, 15
  %8 = tail call signext i8 @getHexChar(i32 noundef %7)
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %9 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv
  store i8 %8, ptr %9, align 1, !tbaa !5
  %10 = lshr i32 %.02325, 4
  %.not = icmp ult i32 %.02325, 16
  %indvar.next = add i64 %indvar, 1
  br i1 %.not, label %11, label %6, !llvm.loop !8

11:                                               ; preds = %6
  %12 = shl i64 %indvars.iv, 32
  %sext = sub i64 38654705664, %12
  %13 = ashr exact i64 %sext, 32
  %14 = tail call noalias ptr @malloc(i64 noundef %13) #4
  %15 = shl i64 %indvars.iv, 32
  %sext35 = sub i64 34359738368, %15
  %16 = ashr exact i64 %sext35, 32
  %17 = getelementptr inbounds i8, ptr %14, i64 %16
  store i8 0, ptr %17, align 1, !tbaa !5
  %18 = icmp slt i64 %indvars.iv, 8
  br i1 %18, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %11
  %19 = sub i64 7, %indvar
  %scevgep = getelementptr i8, ptr %5, i64 %19
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %14, ptr align 1 %scevgep, i64 %indvar.next, i1 false), !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %11
  tail call void @llvm.stackrestore.p0(ptr %4)
  br label %20

20:                                               ; preds = %1, %._crit_edge
  %.021 = phi ptr [ %14, %._crit_edge ], [ @.str, %1 ]
  ret ptr %.021
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @getHexChar(i32 noundef) 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 #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 nocallback nofree nosync nounwind willreturn }
attributes #2 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind allocsize(0) }

!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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass