; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (1556)

; 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 @thousandSeparator(i32 noundef %0) #0 {
  %2 = alloca ptr, align 8
  %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 ptr, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef null, i64 noundef 0, ptr noundef @.str, i32 noundef %10) #4
  store i32 %11, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = add nsw i32 %12, 1
  %14 = sext i32 %13 to i64
  %15 = call noalias ptr @malloc(i64 noundef %14) #5
  store ptr %15, ptr %5, align 8, !tbaa !9
  %16 = load ptr, ptr %5, align 8, !tbaa !9
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  %19 = sext i32 %18 to i64
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %16, i64 noundef %19, ptr noundef @.str, i32 noundef %20) #4
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = icmp slt i32 %22, 1000
  br i1 %23, label %24, label %26

24:                                               ; preds = %1
  %25 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %25, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %84

26:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = sdiv i32 %28, 3
  %30 = add nsw i32 %27, %29
  store i32 %30, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  %33 = sext i32 %32 to i64
  %34 = call noalias ptr @malloc(i64 noundef %33) #5
  store ptr %34, ptr %9, align 8, !tbaa !9
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = add nsw i32 %36, -1
  store i32 %37, ptr %4, align 4, !tbaa !5
  %38 = sext i32 %36 to i64
  %39 = getelementptr inbounds i8, ptr %35, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !11
  %41 = load ptr, ptr %9, align 8, !tbaa !9
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = add nsw i32 %42, -1
  store i32 %43, ptr %7, align 4, !tbaa !5
  %44 = sext i32 %42 to i64
  %45 = getelementptr inbounds i8, ptr %41, i64 %44
  store i8 %40, ptr %45, align 1, !tbaa !11
  br label %46

46:                                               ; preds = %68, %26
  %47 = load i32, ptr %7, align 4, !tbaa !5
  %48 = icmp sge i32 %47, 0
  br i1 %48, label %49, label %80

49:                                               ; preds = %46
  %50 = load i32, ptr %8, align 4, !tbaa !5
  %51 = icmp eq i32 %50, 3
  br i1 %51, label %52, label %65

52:                                               ; preds = %49
  %53 = load i32, ptr %7, align 4, !tbaa !5
  %54 = icmp eq i32 %53, 0
  br i1 %54, label %55, label %59

55:                                               ; preds = %52
  %56 = load ptr, ptr %5, align 8, !tbaa !9
  call void @free(ptr noundef %56) #4
  %57 = load ptr, ptr %9, align 8, !tbaa !9
  %58 = getelementptr inbounds i8, ptr %57, i64 1
  store ptr %58, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %83

59:                                               ; preds = %52
  %60 = load ptr, ptr %9, align 8, !tbaa !9
  %61 = load i32, ptr %7, align 4, !tbaa !5
  %62 = add nsw i32 %61, -1
  store i32 %62, ptr %7, align 4, !tbaa !5
  %63 = sext i32 %61 to i64
  %64 = getelementptr inbounds i8, ptr %60, i64 %63
  store i8 46, ptr %64, align 1, !tbaa !11
  store i32 1, ptr %8, align 4, !tbaa !5
  br label %68

65:                                               ; preds = %49
  %66 = load i32, ptr %8, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %8, align 4, !tbaa !5
  br label %68

68:                                               ; preds = %65, %59
  %69 = load ptr, ptr %5, align 8, !tbaa !9
  %70 = load i32, ptr %4, align 4, !tbaa !5
  %71 = add nsw i32 %70, -1
  store i32 %71, ptr %4, align 4, !tbaa !5
  %72 = sext i32 %70 to i64
  %73 = getelementptr inbounds i8, ptr %69, i64 %72
  %74 = load i8, ptr %73, align 1, !tbaa !11
  %75 = load ptr, ptr %9, align 8, !tbaa !9
  %76 = load i32, ptr %7, align 4, !tbaa !5
  %77 = add nsw i32 %76, -1
  store i32 %77, ptr %7, align 4, !tbaa !5
  %78 = sext i32 %76 to i64
  %79 = getelementptr inbounds i8, ptr %75, i64 %78
  store i8 %74, ptr %79, align 1, !tbaa !11
  br label %46, !llvm.loop !12

80:                                               ; preds = %46
  %81 = load ptr, ptr %5, align 8, !tbaa !9
  call void @free(ptr noundef %81) #4
  %82 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %82, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %83

83:                                               ; preds = %80, %55
  call void @llvm.lifetime.end.p0(i64 8, 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
  br label %84

84:                                               ; preds = %83, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %85 = load ptr, ptr %2, align 8
  ret ptr %85
}

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

; Function Attrs: nounwind
declare i32 @snprintf(ptr noundef, i64 noundef, ptr noundef, ...) #2

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

; 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 "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) "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 = !{!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"

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

; Function Attrs: nounwind uwtable
define dso_local noalias ptr @thousandSeparator(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef null, i64 noundef 0, ptr noundef nonnull @.str, i32 noundef %0) #4
  %3 = add nsw i32 %2, 1
  %4 = sext i32 %3 to i64
  %5 = tail call noalias ptr @malloc(i64 noundef %4) #5
  %6 = tail call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %5, i64 noundef %4, ptr noundef nonnull @.str, i32 noundef %0) #4
  %7 = icmp slt i32 %0, 1000
  br i1 %7, label %37, label %8

8:                                                ; preds = %1
  %9 = sdiv i32 %2, 3
  %10 = add nsw i32 %9, %2
  %11 = add nsw i32 %10, 1
  %12 = sext i32 %11 to i64
  %13 = tail call noalias ptr @malloc(i64 noundef %12) #5
  %14 = sext i32 %2 to i64
  %15 = getelementptr inbounds i8, ptr %5, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = sext i32 %10 to i64
  %18 = getelementptr inbounds i8, ptr %13, i64 %17
  store i8 %16, ptr %18, align 1, !tbaa !5
  %19 = icmp sgt i32 %10, 0
  br i1 %19, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %8, %31
  %indvars.iv = phi i64 [ %indvars.iv.next, %31 ], [ %14, %8 ]
  %.02836 = phi i32 [ %.129, %31 ], [ 0, %8 ]
  %.030.in35 = phi i32 [ %.131, %31 ], [ %10, %8 ]
  %.03037 = add nsw i32 %.030.in35, -1
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %20 = icmp eq i32 %.02836, 3
  br i1 %20, label %21, label %29

21:                                               ; preds = %.lr.ph
  %22 = icmp eq i32 %.03037, 0
  br i1 %22, label %23, label %25

23:                                               ; preds = %21
  tail call void @free(ptr noundef nonnull %5) #4
  %24 = getelementptr inbounds i8, ptr %13, i64 1
  br label %37

25:                                               ; preds = %21
  %26 = add nsw i32 %.030.in35, -2
  %27 = zext nneg i32 %.03037 to i64
  %28 = getelementptr inbounds i8, ptr %13, i64 %27
  store i8 46, ptr %28, align 1, !tbaa !5
  br label %31

29:                                               ; preds = %.lr.ph
  %30 = add nsw i32 %.02836, 1
  br label %31

31:                                               ; preds = %29, %25
  %.131 = phi i32 [ %26, %25 ], [ %.03037, %29 ]
  %.129 = phi i32 [ 1, %25 ], [ %30, %29 ]
  %32 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv.next
  %33 = load i8, ptr %32, align 1, !tbaa !5
  %34 = zext nneg i32 %.131 to i64
  %35 = getelementptr inbounds i8, ptr %13, i64 %34
  store i8 %33, ptr %35, align 1, !tbaa !5
  %36 = icmp sgt i32 %.131, 0
  br i1 %36, label %.lr.ph, label %._crit_edge, !llvm.loop !8

._crit_edge:                                      ; preds = %31, %8
  tail call void @free(ptr noundef nonnull %5) #4
  br label %37

37:                                               ; preds = %23, %._crit_edge, %1
  %.1 = phi ptr [ %5, %1 ], [ %24, %23 ], [ %13, %._crit_edge ]
  ret ptr %.1
}

; Function Attrs: nofree nounwind
declare noundef i32 @snprintf(ptr noalias nocapture noundef writeonly, i64 noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; 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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture 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 = { 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 #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 = { mustprogress nounwind willreturn allockind("free") 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 #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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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