; LLVM IR from lolosssss/leetcode :: c (41_first_missing_positive)

; 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 @firstMissingPositive(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca [1000 x i32], align 16
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4000, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %16, %2
  %10 = load i32, ptr %7, align 4, !tbaa !9
  %11 = icmp slt i32 %10, 1000
  br i1 %11, label %12, label %19

12:                                               ; preds = %9
  %13 = load i32, ptr %7, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [1000 x i32], ptr %6, i64 0, i64 %14
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %12
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %7, align 4, !tbaa !9
  br label %9, !llvm.loop !11

19:                                               ; preds = %9
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %40, %19
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %24, label %43

24:                                               ; preds = %20
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %25, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = icmp sgt i32 %29, 0
  br i1 %30, label %31, label %39

31:                                               ; preds = %24
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [1000 x i32], ptr %6, i64 0, i64 %37
  store i32 1, ptr %38, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %31, %24
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %7, align 4, !tbaa !9
  br label %20, !llvm.loop !13

43:                                               ; preds = %20
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %56, %43
  %45 = load i32, ptr %7, align 4, !tbaa !9
  %46 = icmp slt i32 %45, 1000
  br i1 %46, label %47, label %59

47:                                               ; preds = %44
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [1000 x i32], ptr %6, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !9
  %52 = icmp eq i32 %51, 0
  br i1 %52, label %53, label %55

53:                                               ; preds = %47
  %54 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %54, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %60

55:                                               ; preds = %47
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %7, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %7, align 4, !tbaa !9
  br label %44, !llvm.loop !14

59:                                               ; preds = %44
  store i32 0, ptr %8, align 4
  br label %60

60:                                               ; preds = %59, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4000, ptr %6) #2
  %61 = load i32, ptr %8, align 4
  switch i32 %61, label %64 [
    i32 0, label %62
    i32 1, label %62
  ]

62:                                               ; preds = %60, %60
  %63 = load i32, ptr %3, align 4
  ret i32 %63

64:                                               ; preds = %60
  unreachable
}

; 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}
!14 = distinct !{!14, !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 @firstMissingPositive(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
.preheader16:
  %2 = alloca [1000 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 4000, ptr nonnull %2) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(4000) %2, i8 0, i64 4000, i1 false), !tbaa !5
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.lr.ph.preheader, label %.preheader.preheader

.lr.ph.preheader:                                 ; preds = %.preheader16
  %wide.trip.count = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  br i1 %min.iters.check, label %.lr.ph.preheader45, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %wide.trip.count, 2147483640
  br label %vector.body

vector.body:                                      ; preds = %pred.store.continue43, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %pred.store.continue43 ]
  %4 = getelementptr inbounds i32, ptr %0, i64 %index
  %5 = getelementptr inbounds i8, ptr %4, i64 16
  %wide.load = load <4 x i32>, ptr %4, align 4, !tbaa !5
  %wide.load29 = load <4 x i32>, ptr %5, align 4, !tbaa !5
  %6 = icmp sgt <4 x i32> %wide.load, zeroinitializer
  %7 = icmp sgt <4 x i32> %wide.load29, zeroinitializer
  %8 = extractelement <4 x i1> %6, i64 0
  br i1 %8, label %pred.store.if, label %pred.store.continue

pred.store.if:                                    ; preds = %vector.body
  %9 = extractelement <4 x i32> %wide.load, i64 0
  %10 = zext nneg i32 %9 to i64
  %11 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %10
  store i32 1, ptr %11, align 4, !tbaa !5
  br label %pred.store.continue

pred.store.continue:                              ; preds = %pred.store.if, %vector.body
  %12 = extractelement <4 x i1> %6, i64 1
  br i1 %12, label %pred.store.if30, label %pred.store.continue31

pred.store.if30:                                  ; preds = %pred.store.continue
  %13 = extractelement <4 x i32> %wide.load, i64 1
  %14 = zext nneg i32 %13 to i64
  %15 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %14
  store i32 1, ptr %15, align 4, !tbaa !5
  br label %pred.store.continue31

pred.store.continue31:                            ; preds = %pred.store.if30, %pred.store.continue
  %16 = extractelement <4 x i1> %6, i64 2
  br i1 %16, label %pred.store.if32, label %pred.store.continue33

pred.store.if32:                                  ; preds = %pred.store.continue31
  %17 = extractelement <4 x i32> %wide.load, i64 2
  %18 = zext nneg i32 %17 to i64
  %19 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %18
  store i32 1, ptr %19, align 4, !tbaa !5
  br label %pred.store.continue33

pred.store.continue33:                            ; preds = %pred.store.if32, %pred.store.continue31
  %20 = extractelement <4 x i1> %6, i64 3
  br i1 %20, label %pred.store.if34, label %pred.store.continue35

pred.store.if34:                                  ; preds = %pred.store.continue33
  %21 = extractelement <4 x i32> %wide.load, i64 3
  %22 = zext nneg i32 %21 to i64
  %23 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %22
  store i32 1, ptr %23, align 4, !tbaa !5
  br label %pred.store.continue35

pred.store.continue35:                            ; preds = %pred.store.if34, %pred.store.continue33
  %24 = extractelement <4 x i1> %7, i64 0
  br i1 %24, label %pred.store.if36, label %pred.store.continue37

pred.store.if36:                                  ; preds = %pred.store.continue35
  %25 = extractelement <4 x i32> %wide.load29, i64 0
  %26 = zext nneg i32 %25 to i64
  %27 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %26
  store i32 1, ptr %27, align 4, !tbaa !5
  br label %pred.store.continue37

pred.store.continue37:                            ; preds = %pred.store.if36, %pred.store.continue35
  %28 = extractelement <4 x i1> %7, i64 1
  br i1 %28, label %pred.store.if38, label %pred.store.continue39

pred.store.if38:                                  ; preds = %pred.store.continue37
  %29 = extractelement <4 x i32> %wide.load29, i64 1
  %30 = zext nneg i32 %29 to i64
  %31 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %30
  store i32 1, ptr %31, align 4, !tbaa !5
  br label %pred.store.continue39

pred.store.continue39:                            ; preds = %pred.store.if38, %pred.store.continue37
  %32 = extractelement <4 x i1> %7, i64 2
  br i1 %32, label %pred.store.if40, label %pred.store.continue41

pred.store.if40:                                  ; preds = %pred.store.continue39
  %33 = extractelement <4 x i32> %wide.load29, i64 2
  %34 = zext nneg i32 %33 to i64
  %35 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %34
  store i32 1, ptr %35, align 4, !tbaa !5
  br label %pred.store.continue41

pred.store.continue41:                            ; preds = %pred.store.if40, %pred.store.continue39
  %36 = extractelement <4 x i1> %7, i64 3
  br i1 %36, label %pred.store.if42, label %pred.store.continue43

pred.store.if42:                                  ; preds = %pred.store.continue41
  %37 = extractelement <4 x i32> %wide.load29, i64 3
  %38 = zext nneg i32 %37 to i64
  %39 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %38
  store i32 1, ptr %39, align 4, !tbaa !5
  br label %pred.store.continue43

pred.store.continue43:                            ; preds = %pred.store.if42, %pred.store.continue41
  %index.next = add nuw i64 %index, 8
  %40 = icmp eq i64 %index.next, %n.vec
  br i1 %40, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %pred.store.continue43
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.preheader.preheader, label %.lr.ph.preheader45

.lr.ph.preheader45:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader45, %47
  %indvars.iv = phi i64 [ %indvars.iv.next, %47 ], [ %indvars.iv.ph, %.lr.ph.preheader45 ]
  %41 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = icmp sgt i32 %42, 0
  br i1 %43, label %44, label %47

44:                                               ; preds = %.lr.ph
  %45 = zext nneg i32 %42 to i64
  %46 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %45
  store i32 1, ptr %46, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %.lr.ph, %44
  %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 %.preheader.preheader, label %.lr.ph, !llvm.loop !13

.preheader.preheader:                             ; preds = %47, %middle.block, %.preheader16
  br label %.preheader

.preheader:                                       ; preds = %57, %.preheader.preheader
  %indvars.iv22 = phi i64 [ 1, %.preheader.preheader ], [ %indvars.iv.next23.2, %57 ]
  %48 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %indvars.iv22
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = icmp eq i32 %49, 0
  br i1 %50, label %.split.loop.exit26, label %.preheader.1

.preheader.1:                                     ; preds = %.preheader
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 1
  %51 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %indvars.iv.next23
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %.split.loop.exit26, label %.preheader.2

.preheader.2:                                     ; preds = %.preheader.1
  %indvars.iv.next23.1 = add nuw nsw i64 %indvars.iv22, 2
  %54 = getelementptr inbounds [1000 x i32], ptr %2, i64 0, i64 %indvars.iv.next23.1
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = icmp eq i32 %55, 0
  br i1 %56, label %.split.loop.exit26, label %57

57:                                               ; preds = %.preheader.2
  %indvars.iv.next23.2 = add nuw nsw i64 %indvars.iv22, 3
  %exitcond25.not.2 = icmp eq i64 %indvars.iv.next23.2, 1000
  br i1 %exitcond25.not.2, label %.split.loop.exit, label %.preheader, !llvm.loop !14

.split.loop.exit26:                               ; preds = %.preheader.2, %.preheader.1, %.preheader
  %indvars.iv22.lcssa = phi i64 [ %indvars.iv22, %.preheader ], [ %indvars.iv.next23, %.preheader.1 ], [ %indvars.iv.next23.1, %.preheader.2 ]
  %58 = trunc nuw nsw i64 %indvars.iv22.lcssa to i32
  br label %.split.loop.exit

.split.loop.exit:                                 ; preds = %57, %.split.loop.exit26
  %.2.lcssa = phi i32 [ %58, %.split.loop.exit26 ], [ 1000, %57 ]
  call void @llvm.lifetime.end.p0(i64 4000, ptr nonnull %2) #3
  ret i32 %.2.lcssa
}

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = !{!"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,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass