; LLVM IR from kenjin/Awesome-LC-C-Cracker :: leetcode-c-sol/0287_Find-the-Duplicate-Number (0287_Find-the-Duplicate-Number)

; 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 @findDuplicate(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %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
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  br label %80

14:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = sub nsw i32 %15, 1
  store i32 %16, ptr %7, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %77, %14
  %18 = load i32, ptr %6, align 4, !tbaa !9
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = icmp ne i32 %18, %19
  br i1 %20, label %21, label %78

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = sub nsw i32 %23, %24
  %26 = ashr i32 %25, 1
  %27 = add nsw i32 %22, %26
  store i32 %27, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %62, %21
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %33, label %32

32:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %65

33:                                               ; preds = %28
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = load i32, ptr %10, 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 i32, ptr %6, align 4, !tbaa !9
  %40 = icmp slt i32 %38, %39
  br i1 %40, label %49, label %41

41:                                               ; preds = %33
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i32, ptr %10, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i32, ptr %42, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !9
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = icmp sgt i32 %46, %47
  br i1 %48, label %49, label %50

49:                                               ; preds = %41, %33
  br label %62

50:                                               ; preds = %41
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load i32, ptr %10, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i32, ptr %51, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !9
  %56 = load i32, ptr %8, align 4, !tbaa !9
  %57 = icmp sle i32 %55, %56
  br i1 %57, label %58, label %61

58:                                               ; preds = %50
  %59 = load i32, ptr %9, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %9, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %58, %50
  br label %62

62:                                               ; preds = %61, %49
  %63 = load i32, ptr %10, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %10, align 4, !tbaa !9
  br label %28, !llvm.loop !11

65:                                               ; preds = %32
  %66 = load i32, ptr %8, align 4, !tbaa !9
  %67 = load i32, ptr %6, align 4, !tbaa !9
  %68 = sub nsw i32 %66, %67
  %69 = add nsw i32 %68, 1
  %70 = load i32, ptr %9, align 4, !tbaa !9
  %71 = icmp slt i32 %69, %70
  br i1 %71, label %72, label %74

72:                                               ; preds = %65
  %73 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %73, ptr %7, align 4, !tbaa !9
  br label %77

74:                                               ; preds = %65
  %75 = load i32, ptr %8, align 4, !tbaa !9
  %76 = add nsw i32 %75, 1
  store i32 %76, ptr %6, align 4, !tbaa !9
  br label %77

77:                                               ; preds = %74, %72
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %17, !llvm.loop !13

78:                                               ; preds = %17
  %79 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %79, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %80

80:                                               ; preds = %78, %13
  %81 = load i32, ptr %3, align 4
  ret i32 %81
}

; 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 @findDuplicate(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %.loopexit, label %4

4:                                                ; preds = %2
  %5 = add nsw i32 %1, -1
  %.not41 = icmp eq i32 %5, 1
  br i1 %.not41, label %.loopexit, label %.lr.ph45

.lr.ph45:                                         ; preds = %4
  %6 = icmp sgt i32 %1, 0
  br i1 %6, label %.lr.ph.us.preheader, label %.lr.ph45.split

.lr.ph.us.preheader:                              ; preds = %.lr.ph45
  %wide.trip.count = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  %n.vec = and i64 %wide.trip.count, 2147483640
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %._crit_edge.us
  %.02843.us = phi i32 [ %.129.us, %._crit_edge.us ], [ %5, %.lr.ph.us.preheader ]
  %.03042.us = phi i32 [ %.131.us, %._crit_edge.us ], [ 1, %.lr.ph.us.preheader ]
  %7 = sub nsw i32 %.02843.us, %.03042.us
  %8 = ashr i32 %7, 1
  %9 = add nsw i32 %8, %.03042.us
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.us
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %.03042.us, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %broadcast.splatinsert53 = insertelement <4 x i32> poison, i32 %.02843.us, i64 0
  %broadcast.splat54 = shufflevector <4 x i32> %broadcast.splatinsert53, <4 x i32> poison, <4 x i32> zeroinitializer
  %broadcast.splatinsert55 = insertelement <4 x i32> poison, i32 %9, i64 0
  %broadcast.splat56 = shufflevector <4 x i32> %broadcast.splatinsert55, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %24, %vector.body ]
  %vec.phi51 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %25, %vector.body ]
  %10 = getelementptr inbounds i32, ptr %0, i64 %index
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %wide.load = load <4 x i32>, ptr %10, align 4, !tbaa !5
  %wide.load52 = load <4 x i32>, ptr %11, align 4, !tbaa !5
  %12 = icmp sge <4 x i32> %wide.load, %broadcast.splat
  %13 = icmp sge <4 x i32> %wide.load52, %broadcast.splat
  %14 = icmp sle <4 x i32> %wide.load, %broadcast.splat54
  %15 = icmp sle <4 x i32> %wide.load52, %broadcast.splat54
  %16 = and <4 x i1> %12, %14
  %17 = and <4 x i1> %13, %15
  %18 = icmp sle <4 x i32> %wide.load, %broadcast.splat56
  %19 = icmp sle <4 x i32> %wide.load52, %broadcast.splat56
  %20 = select <4 x i1> %16, <4 x i1> %18, <4 x i1> zeroinitializer
  %21 = select <4 x i1> %17, <4 x i1> %19, <4 x i1> zeroinitializer
  %22 = zext <4 x i1> %20 to <4 x i32>
  %23 = zext <4 x i1> %21 to <4 x i32>
  %24 = add <4 x i32> %vec.phi, %22
  %25 = add <4 x i32> %vec.phi51, %23
  %index.next = add nuw i64 %index, 8
  %26 = icmp eq i64 %index.next, %n.vec
  br i1 %26, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %25, %24
  %27 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph.us
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.us ], [ %n.vec, %middle.block ]
  %.02639.us.ph = phi i32 [ 0, %.lr.ph.us ], [ %27, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %.02639.us = phi i32 [ %.1.us, %scalar.ph ], [ %.02639.us.ph, %scalar.ph.preheader ]
  %28 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp sge i32 %29, %.03042.us
  %31 = icmp sle i32 %29, %.02843.us
  %or.cond.not38.us = and i1 %30, %31
  %.not34.us = icmp sle i32 %29, %9
  %or.cond35.not.us = select i1 %or.cond.not38.us, i1 %.not34.us, i1 false
  %32 = zext i1 %or.cond35.not.us to i32
  %.1.us = add nuw nsw i32 %.02639.us, %32
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.us, label %scalar.ph, !llvm.loop !13

._crit_edge.us:                                   ; preds = %scalar.ph, %middle.block
  %.1.us.lcssa = phi i32 [ %27, %middle.block ], [ %.1.us, %scalar.ph ]
  %33 = add nsw i32 %8, 1
  %34 = icmp slt i32 %33, %.1.us.lcssa
  %35 = add nsw i32 %9, 1
  %.131.us = select i1 %34, i32 %.03042.us, i32 %35
  %.129.us = select i1 %34, i32 %9, i32 %.02843.us
  %.not.us = icmp eq i32 %.131.us, %.129.us
  br i1 %.not.us, label %.loopexit, label %.lr.ph.us, !llvm.loop !14

.lr.ph45.split:                                   ; preds = %.lr.ph45, %.lr.ph45.split
  %.02843 = phi i32 [ %.129, %.lr.ph45.split ], [ %5, %.lr.ph45 ]
  %.03042 = phi i32 [ %.131, %.lr.ph45.split ], [ 1, %.lr.ph45 ]
  %36 = sub nsw i32 %.02843, %.03042
  %37 = ashr i32 %36, 1
  %38 = add nsw i32 %37, %.03042
  %39 = icmp slt i32 %37, -1
  %40 = add nsw i32 %38, 1
  %.131 = select i1 %39, i32 %.03042, i32 %40
  %.129 = select i1 %39, i32 %38, i32 %.02843
  %.not = icmp eq i32 %.131, %.129
  br i1 %.not, label %.loopexit, label %.lr.ph45.split, !llvm.loop !14

.loopexit:                                        ; preds = %.lr.ph45.split, %._crit_edge.us, %4, %2
  %.027 = phi i32 [ 0, %2 ], [ 1, %4 ], [ %.131.us, %._crit_edge.us ], [ %.131, %.lr.ph45.split ]
  ret i32 %.027
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x 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 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = distinct !{!14, !10}


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