; LLVM IR from kenjin/Awesome-LC-C-Cracker :: leetcode-c-sol/0494_Target-Sum (0494_Target-Sum)

; 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 void @calSum(ptr noundef %0, i32 noundef %1, i32 noundef %2, i64 noundef %3, ptr noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  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 i64 %3, ptr %9, align 8, !tbaa !11
  store ptr %4, ptr %10, align 8, !tbaa !5
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = icmp sgt i32 %12, %13
  br i1 %14, label %15, label %23

15:                                               ; preds = %5
  %16 = load i64, ptr %9, align 8, !tbaa !11
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %18, label %22

18:                                               ; preds = %15
  %19 = load ptr, ptr %10, align 8, !tbaa !5
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %19, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %18, %15
  br label %48

23:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  store i64 %29, ptr %11, align 8, !tbaa !11
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = add nsw i32 %31, 1
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = load i64, ptr %9, align 8, !tbaa !11
  %35 = load i64, ptr %11, align 8, !tbaa !11
  %36 = sub nsw i64 %34, %35
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  call void @calSum(ptr noundef %30, i32 noundef %32, i32 noundef %33, i64 noundef %36, ptr noundef %37)
  %38 = load i64, ptr %11, align 8, !tbaa !11
  %39 = mul nsw i64 %38, -1
  store i64 %39, ptr %11, align 8, !tbaa !11
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  %43 = load i32, ptr %8, align 4, !tbaa !9
  %44 = load i64, ptr %9, align 8, !tbaa !11
  %45 = load i64, ptr %11, align 8, !tbaa !11
  %46 = sub nsw i64 %44, %45
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  call void @calSum(ptr noundef %40, i32 noundef %42, i32 noundef %43, i64 noundef %46, ptr noundef %47)
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %48

48:                                               ; preds = %23, %22
  ret void
}

; 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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}


; 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 nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @calSum(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, i64 noundef %3, ptr nocapture noundef %4) local_unnamed_addr #0 {
  %6 = icmp sgt i32 %1, %2
  br i1 %6, label %tailrecurse._crit_edge, label %tailrecurse.preheader

tailrecurse.preheader:                            ; preds = %5
  %7 = sext i32 %1 to i64
  %8 = add i32 %2, 1
  br label %tailrecurse

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %5
  %.tr21.lcssa = phi i64 [ %3, %5 ], [ %18, %tailrecurse ]
  %9 = icmp eq i64 %.tr21.lcssa, 0
  br i1 %9, label %10, label %19

10:                                               ; preds = %tailrecurse._crit_edge
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = add nsw i32 %11, 1
  store i32 %12, ptr %4, align 4, !tbaa !5
  br label %19

tailrecurse:                                      ; preds = %tailrecurse.preheader, %tailrecurse
  %indvars.iv = phi i64 [ %7, %tailrecurse.preheader ], [ %indvars.iv.next, %tailrecurse ]
  %.tr2124 = phi i64 [ %3, %tailrecurse.preheader ], [ %18, %tailrecurse ]
  %13 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %16 = sub nsw i64 %.tr2124, %15
  %17 = trunc i64 %indvars.iv.next to i32
  tail call void @calSum(ptr noundef %0, i32 noundef %17, i32 noundef %2, i64 noundef %16, ptr noundef %4)
  %18 = add nsw i64 %.tr2124, %15
  %exitcond.not = icmp eq i32 %8, %17
  br i1 %exitcond.not, label %tailrecurse._crit_edge, label %tailrecurse

19:                                               ; preds = %tailrecurse._crit_edge, %10
  ret void
}

attributes #0 = { nofree nosync nounwind memory(argmem: 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" }

!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"}


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