; LLVM IR from kenjin/Awesome-LC-C-Cracker :: leetcode-c-sol/1099_two-sum-less-than-k (1099_two-sum-less-than-k)

; 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 i32 @twoSumLessThanK(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 -1, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %55, %3
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  store i32 2, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %58

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %10, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %51, %17
  %21 = load i32, ptr %10, align 4, !tbaa !9
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 5, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %54

25:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %26, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = load i32, ptr %10, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i32, ptr %31, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = add nsw i32 %30, %35
  store i32 %36, ptr %11, align 4, !tbaa !9
  %37 = load i32, ptr %11, align 4, !tbaa !9
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = icmp slt i32 %37, %38
  br i1 %39, label %40, label %50

40:                                               ; preds = %25
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = icmp sgt i32 %41, %42
  br i1 %43, label %44, label %46

44:                                               ; preds = %40
  %45 = load i32, ptr %7, align 4, !tbaa !9
  br label %48

46:                                               ; preds = %40
  %47 = load i32, ptr %11, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %46, %44
  %49 = phi i32 [ %45, %44 ], [ %47, %46 ]
  store i32 %49, ptr %7, align 4, !tbaa !9
  br label %50

50:                                               ; preds = %48, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %51

51:                                               ; preds = %50
  %52 = load i32, ptr %10, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %10, align 4, !tbaa !9
  br label %20, !llvm.loop !11

54:                                               ; preds = %24
  br label %55

55:                                               ; preds = %54
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %8, align 4, !tbaa !9
  br label %12, !llvm.loop !13

58:                                               ; preds = %16
  %59 = load i32, ptr %7, align 4, !tbaa !9
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i32 %59
}

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

!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 norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i32 @twoSumLessThanK(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %1, 0
  br i1 %4, label %.lr.ph25.preheader, label %._crit_edge

.lr.ph25.preheader:                               ; preds = %3
  %5 = zext nneg i32 %1 to i64
  %wide.trip.count33 = zext nneg i32 %1 to i64
  br label %.lr.ph25

.loopexit:                                        ; preds = %9, %.lr.ph25
  %.1.lcssa = phi i32 [ %.024, %.lr.ph25 ], [ %.2, %9 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond34.not = icmp eq i64 %indvars.iv.next31, %wide.trip.count33
  br i1 %exitcond34.not, label %._crit_edge, label %.lr.ph25, !llvm.loop !5

._crit_edge:                                      ; preds = %.loopexit, %3
  %.0.lcssa = phi i32 [ -1, %3 ], [ %.1.lcssa, %.loopexit ]
  ret i32 %.0.lcssa

.lr.ph25:                                         ; preds = %.lr.ph25.preheader, %.loopexit
  %indvars.iv30 = phi i64 [ 0, %.lr.ph25.preheader ], [ %indvars.iv.next31, %.loopexit ]
  %indvars.iv = phi i64 [ 1, %.lr.ph25.preheader ], [ %indvars.iv.next, %.loopexit ]
  %.024 = phi i32 [ -1, %.lr.ph25.preheader ], [ %.1.lcssa, %.loopexit ]
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %6 = icmp ult i64 %indvars.iv.next31, %5
  br i1 %6, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph25
  %7 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv30
  %8 = load i32, ptr %7, align 4, !tbaa !7
  br label %9

9:                                                ; preds = %.lr.ph, %9
  %indvars.iv27 = phi i64 [ %indvars.iv, %.lr.ph ], [ %indvars.iv.next28, %9 ]
  %.122 = phi i32 [ %.024, %.lr.ph ], [ %.2, %9 ]
  %10 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv27
  %11 = load i32, ptr %10, align 4, !tbaa !7
  %12 = add nsw i32 %11, %8
  %13 = icmp slt i32 %12, %2
  %14 = tail call i32 @llvm.smax.i32(i32 %.122, i32 %12)
  %.2 = select i1 %13, i32 %14, i32 %.122
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next28, %wide.trip.count33
  br i1 %exitcond.not, label %.loopexit, label %9, !llvm.loop !11
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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 speculatable willreturn memory(none) }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = distinct !{!11, !6}


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