; LLVM IR from mythkiven/leetcode :: 000.TwoSum (Solution)

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

; Function Attrs: nounwind uwtable
define dso_local ptr @twoSum1(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  %14 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %14, align 4, !tbaa !9
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %4
  store ptr null, ptr %5, align 8
  br label %70

18:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %19 = call noalias ptr @malloc(i64 noundef 9) #4
  store ptr %19, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %63, %18
  %21 = load i32, ptr %11, align 4, !tbaa !9
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 2, ptr %12, align 4
  br label %66

25:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %26 = load i32, ptr %11, align 4, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %13, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %57, %25
  %29 = load i32, ptr %13, align 4, !tbaa !9
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %28
  store i32 5, ptr %12, align 4
  br label %60

33:                                               ; preds = %28
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %34, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = load i32, ptr %13, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i32, ptr %39, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !9
  %44 = add nsw i32 %38, %43
  %45 = load i32, ptr %8, align 4, !tbaa !9
  %46 = icmp eq i32 %44, %45
  br i1 %46, label %47, label %56

47:                                               ; preds = %33
  %48 = load i32, ptr %11, align 4, !tbaa !9
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = getelementptr inbounds i32, ptr %49, i64 0
  store i32 %48, ptr %50, align 4, !tbaa !9
  %51 = load i32, ptr %13, align 4, !tbaa !9
  %52 = load ptr, ptr %10, align 8, !tbaa !5
  %53 = getelementptr inbounds i32, ptr %52, i64 1
  store i32 %51, ptr %53, align 4, !tbaa !9
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 2, ptr %54, align 4, !tbaa !9
  %55 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %55, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %60

56:                                               ; preds = %33
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %13, align 4, !tbaa !9
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %13, align 4, !tbaa !9
  br label %28, !llvm.loop !11

60:                                               ; preds = %47, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  %61 = load i32, ptr %12, align 4
  switch i32 %61, label %66 [
    i32 5, label %62
  ]

62:                                               ; preds = %60
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %11, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %11, align 4, !tbaa !9
  br label %20, !llvm.loop !13

66:                                               ; preds = %60, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  %67 = load i32, ptr %12, align 4
  switch i32 %67, label %69 [
    i32 2, label %68
  ]

68:                                               ; preds = %66
  store ptr null, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %69

69:                                               ; preds = %68, %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %70

70:                                               ; preds = %69, %17
  %71 = load ptr, ptr %5, align 8
  ret ptr %71
}

; 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

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 }
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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

; Function Attrs: nofree nounwind memory(write, argmem: readwrite, inaccessiblemem: readwrite) uwtable
define dso_local noalias noundef ptr @twoSum1(ptr noundef readonly %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  store i32 0, ptr %3, align 4, !tbaa !5
  %5 = icmp eq ptr %0, null
  br i1 %5, label %.loopexit31, label %6

6:                                                ; preds = %4
  %7 = tail call noalias dereferenceable_or_null(9) ptr @malloc(i64 noundef 9) #2
  %.not2936 = icmp sgt i32 %1, 0
  br i1 %.not2936, label %.lr.ph38.preheader, label %.loopexit31

.lr.ph38.preheader:                               ; preds = %6
  %8 = zext nneg i32 %1 to i64
  %wide.trip.count47 = zext nneg i32 %1 to i64
  br label %.lr.ph38

.loopexit:                                        ; preds = %19, %.lr.ph38
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond48.not = icmp eq i64 %indvars.iv.next45, %wide.trip.count47
  br i1 %exitcond48.not, label %.loopexit31, label %.lr.ph38

.lr.ph38:                                         ; preds = %.lr.ph38.preheader, %.loopexit
  %indvars.iv44 = phi i64 [ 0, %.lr.ph38.preheader ], [ %indvars.iv.next45, %.loopexit ]
  %indvars.iv = phi i64 [ 1, %.lr.ph38.preheader ], [ %indvars.iv.next, %.loopexit ]
  %indvars.iv.next45 = add nuw nsw i64 %indvars.iv44, 1
  %.not34 = icmp ult i64 %indvars.iv.next45, %8
  br i1 %.not34, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph38
  %9 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv44
  %10 = load i32, ptr %9, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %.lr.ph, %19
  %indvars.iv41 = phi i64 [ %indvars.iv, %.lr.ph ], [ %indvars.iv.next42, %19 ]
  %12 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv41
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = add nsw i32 %13, %10
  %15 = icmp eq i32 %14, %2
  br i1 %15, label %.thread, label %19

.thread:                                          ; preds = %11
  %16 = trunc nuw nsw i64 %indvars.iv44 to i32
  %17 = trunc nuw nsw i64 %indvars.iv41 to i32
  store i32 %16, ptr %7, align 4, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %7, i64 4
  store i32 %17, ptr %18, align 4, !tbaa !5
  store i32 2, ptr %3, align 4, !tbaa !5
  br label %.loopexit31

19:                                               ; preds = %11
  %indvars.iv.next42 = add nuw nsw i64 %indvars.iv41, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next42, %wide.trip.count47
  br i1 %exitcond.not, label %.loopexit, label %11, !llvm.loop !9

.loopexit31:                                      ; preds = %.loopexit, %6, %.thread, %4
  %.4 = phi ptr [ null, %4 ], [ %7, %.thread ], [ null, %6 ], [ null, %.loopexit ]
  ret ptr %.4
}

; 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

attributes #0 = { nofree nounwind memory(write, argmem: readwrite, 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,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 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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