; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (1608)

; 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 @specialArray(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
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %67, %2
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = add nsw i32 %13, 1
  %15 = icmp slt i32 %12, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %11
  store i32 2, ptr %7, align 4
  br label %70

17:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, 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
  br label %18

18:                                               ; preds = %53, %17
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  store i32 5, ptr %7, align 4
  br label %56

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i32, ptr %9, 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
  store i32 %28, ptr %10, align 4, !tbaa !9
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = icmp sge i32 %29, %30
  br i1 %31, label %32, label %40

32:                                               ; preds = %23
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %8, align 4, !tbaa !9
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = icmp sgt i32 %35, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %32
  store i32 5, ptr %7, align 4
  br label %50

39:                                               ; preds = %32
  store i32 7, ptr %7, align 4
  br label %50

40:                                               ; preds = %23
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = sub nsw i32 %42, %43
  %45 = add nsw i32 %41, %44
  %46 = load i32, ptr %6, align 4, !tbaa !9
  %47 = icmp slt i32 %45, %46
  br i1 %47, label %48, label %49

48:                                               ; preds = %40
  store i32 5, ptr %7, align 4
  br label %50

49:                                               ; preds = %40
  store i32 0, ptr %7, align 4
  br label %50

50:                                               ; preds = %49, %48, %39, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %51 = load i32, ptr %7, align 4
  switch i32 %51, label %56 [
    i32 0, label %52
    i32 7, label %53
  ]

52:                                               ; preds = %50
  br label %53

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

56:                                               ; preds = %50, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = load i32, ptr %6, align 4, !tbaa !9
  %60 = icmp eq i32 %58, %59
  br i1 %60, label %61, label %63

61:                                               ; preds = %57
  %62 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %62, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %64

63:                                               ; preds = %57
  store i32 0, ptr %7, align 4
  br label %64

64:                                               ; preds = %63, %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %65 = load i32, ptr %7, align 4
  switch i32 %65, label %70 [
    i32 0, label %66
  ]

66:                                               ; preds = %64
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %6, align 4, !tbaa !9
  br label %11, !llvm.loop !13

70:                                               ; preds = %64, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %71 = load i32, ptr %7, align 4
  switch i32 %71, label %75 [
    i32 2, label %72
    i32 1, label %73
  ]

72:                                               ; preds = %70
  store i32 -1, ptr %3, align 4
  br label %73

73:                                               ; preds = %72, %70
  %74 = load i32, ptr %3, align 4
  ret i32 %74

75:                                               ; preds = %70
  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}


; 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 @specialArray(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not47 = icmp slt i32 %1, 1
  br i1 %.not47, label %._crit_edge, label %.preheader.us.preheader

.preheader.us.preheader:                          ; preds = %2
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %13
  %.03048.us = phi i32 [ %14, %13 ], [ 1, %.preheader.us.preheader ]
  br label %3

3:                                                ; preds = %.preheader.us, %select.unfold.us
  %indvars.iv = phi i64 [ 0, %.preheader.us ], [ %indvars.iv.next, %select.unfold.us ]
  %.02441.us = phi i32 [ 0, %.preheader.us ], [ %.125.us, %select.unfold.us ]
  %4 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %.not34.us = icmp slt i32 %5, %.03048.us
  br i1 %.not34.us, label %8, label %6

6:                                                ; preds = %3
  %7 = add nsw i32 %.02441.us, 1
  %.not35.us = icmp slt i32 %.02441.us, %.03048.us
  br i1 %.not35.us, label %select.unfold.us, label %.thread.us

8:                                                ; preds = %3
  %9 = add i32 %.02441.us, %1
  %10 = trunc nuw nsw i64 %indvars.iv to i32
  %11 = sub i32 %9, %10
  %12 = icmp slt i32 %11, %.03048.us
  br i1 %12, label %.thread.us, label %select.unfold.us

select.unfold.us:                                 ; preds = %8, %6
  %.125.us = phi i32 [ %.02441.us, %8 ], [ %7, %6 ]
  %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 %.thread.us, label %3, !llvm.loop !9

.thread.us:                                       ; preds = %select.unfold.us, %6, %8
  %.226.us = phi i32 [ %.02441.us, %8 ], [ %7, %6 ], [ %.125.us, %select.unfold.us ]
  %.not36.us = icmp eq i32 %.226.us, %.03048.us
  br i1 %.not36.us, label %._crit_edge, label %13

13:                                               ; preds = %.thread.us
  %14 = add nuw i32 %.03048.us, 1
  %exitcond53.not = icmp eq i32 %.03048.us, %1
  br i1 %exitcond53.not, label %._crit_edge, label %.preheader.us, !llvm.loop !11

._crit_edge:                                      ; preds = %.thread.us, %13, %2
  %spec.select = phi i32 [ -1, %2 ], [ -1, %13 ], [ %.03048.us, %.thread.us ]
  ret i32 %spec.select
}

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

!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"}
!11 = distinct !{!11, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass