; LLVM IR from JessonYue/LeetCodeLearning :: Array/zhangjunwei/7.13-7.18 (toHex)

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

@__const.toHex.table = external hidden unnamed_addr constant [16 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local ptr @toHex(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca [16 x i8], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %3) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %3, ptr align 16 @__const.toHex.table, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 7, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %7 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %7, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %8 = call noalias ptr @calloc(i64 noundef 9, i64 noundef 1) #5
  store ptr %8, ptr %6, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %28, %1
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = and i32 %10, 15
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [16 x i8], ptr %3, i64 0, i64 %12
  %14 = load i8, ptr %13, align 1, !tbaa !11
  %15 = load ptr, ptr %6, align 8, !tbaa !9
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = add nsw i32 %16, -1
  store i32 %17, ptr %4, 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
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = ashr i32 %20, 4
  store i32 %21, ptr %5, align 4, !tbaa !5
  br label %22

22:                                               ; preds = %9
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %28

25:                                               ; preds = %22
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = icmp sge i32 %26, 0
  br label %28

28:                                               ; preds = %25, %22
  %29 = phi i1 [ false, %22 ], [ %27, %25 ]
  br i1 %29, label %9, label %30, !llvm.loop !12

30:                                               ; preds = %28
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i8, ptr %31, i64 %33
  %35 = getelementptr inbounds i8, ptr %34, i64 1
  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 16, ptr %3) #4
  ret ptr %35
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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

; 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind allocsize(0,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,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 = !{!"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}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@__const.toHex.table = external hidden unnamed_addr constant [16 x i8], align 16

; Function Attrs: nofree nounwind memory(write, argmem: none, inaccessiblemem: readwrite) uwtable
define dso_local noalias nonnull ptr @toHex(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noalias dereferenceable_or_null(9) ptr @calloc(i64 noundef 9, i64 noundef 1) #2
  %3 = and i32 %0, 15
  %4 = zext nneg i32 %3 to i64
  %5 = getelementptr inbounds [16 x i8], ptr @__const.toHex.table, i64 0, i64 %4
  %6 = load i8, ptr %5, align 1, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %2, i64 7
  store i8 %6, ptr %7, align 1, !tbaa !5
  %8 = icmp ugt i32 %0, 15
  br i1 %8, label %9, label %62, !llvm.loop !8

9:                                                ; preds = %1
  %10 = ashr i32 %0, 4
  %11 = and i32 %10, 15
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds [16 x i8], ptr @__const.toHex.table, i64 0, i64 %12
  %14 = load i8, ptr %13, align 1, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %2, i64 6
  store i8 %14, ptr %15, align 1, !tbaa !5
  %16 = icmp ugt i32 %10, 15
  br i1 %16, label %17, label %62, !llvm.loop !8

17:                                               ; preds = %9
  %18 = ashr i32 %0, 8
  %19 = and i32 %18, 15
  %20 = zext nneg i32 %19 to i64
  %21 = getelementptr inbounds [16 x i8], ptr @__const.toHex.table, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %2, i64 5
  store i8 %22, ptr %23, align 1, !tbaa !5
  %24 = icmp ugt i32 %18, 15
  br i1 %24, label %25, label %62, !llvm.loop !8

25:                                               ; preds = %17
  %26 = ashr i32 %0, 12
  %27 = and i32 %26, 15
  %28 = zext nneg i32 %27 to i64
  %29 = getelementptr inbounds [16 x i8], ptr @__const.toHex.table, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %2, i64 4
  store i8 %30, ptr %31, align 1, !tbaa !5
  %32 = icmp ugt i32 %26, 15
  br i1 %32, label %33, label %62, !llvm.loop !8

33:                                               ; preds = %25
  %34 = ashr i32 %0, 16
  %35 = and i32 %34, 15
  %36 = zext nneg i32 %35 to i64
  %37 = getelementptr inbounds [16 x i8], ptr @__const.toHex.table, i64 0, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %2, i64 3
  store i8 %38, ptr %39, align 1, !tbaa !5
  %40 = icmp ugt i32 %34, 15
  br i1 %40, label %41, label %62, !llvm.loop !8

41:                                               ; preds = %33
  %42 = ashr i32 %0, 20
  %43 = and i32 %42, 15
  %44 = zext nneg i32 %43 to i64
  %45 = getelementptr inbounds [16 x i8], ptr @__const.toHex.table, i64 0, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %2, i64 2
  store i8 %46, ptr %47, align 1, !tbaa !5
  %48 = icmp ugt i32 %42, 15
  br i1 %48, label %49, label %62, !llvm.loop !8

49:                                               ; preds = %41
  %50 = ashr i32 %0, 24
  %51 = and i32 %50, 15
  %52 = zext nneg i32 %51 to i64
  %53 = getelementptr inbounds [16 x i8], ptr @__const.toHex.table, i64 0, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !5
  %55 = getelementptr inbounds i8, ptr %2, i64 1
  store i8 %54, ptr %55, align 1, !tbaa !5
  %56 = icmp ugt i32 %50, 15
  br i1 %56, label %57, label %62, !llvm.loop !8

57:                                               ; preds = %49
  %58 = lshr i32 %0, 28
  %59 = zext nneg i32 %58 to i64
  %60 = getelementptr inbounds [16 x i8], ptr @__const.toHex.table, i64 0, i64 %59
  %61 = load i8, ptr %60, align 1, !tbaa !5
  store i8 %61, ptr %2, align 1, !tbaa !5
  br label %62

62:                                               ; preds = %57, %49, %41, %33, %25, %17, %9, %1
  %indvars.iv.next.lcssa = phi i64 [ 6, %1 ], [ 5, %9 ], [ 4, %17 ], [ 3, %25 ], [ 2, %33 ], [ 1, %41 ], [ 0, %49 ], [ -1, %57 ]
  %63 = getelementptr inbounds i8, ptr %2, i64 %indvars.iv.next.lcssa
  %64 = getelementptr inbounds i8, ptr %63, i64 1
  ret ptr %64
}

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

attributes #0 = { nofree nounwind memory(write, argmem: none, inaccessiblemem: readwrite) 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,zeroed") allocsize(0,1) 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 = { nounwind allocsize(0,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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass