; LLVM IR from Izaron/ACM-ICPC :: Timus Online Judge/Volume 8/1724. Origin of Man Clarified.ll (Z8findNexti)

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

@n = external dso_local global i32, align 4
@ans = external dso_local global [100005 x [4 x i32]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z6getNxti(i32 noundef) #0

; 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

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z6ansGetiiiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z8findNexti(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = call noundef i32 @_Z6getNxti(i32 noundef %8)
  store i32 %9, ptr %4, align 4, !tbaa !5
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = icmp sge i32 %10, %11
  br i1 %12, label %13, label %15

13:                                               ; preds = %1
  %14 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %14, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %56

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

16:                                               ; preds = %49, %15
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = icmp slt i32 %17, 4
  br i1 %18, label %20, label %19

19:                                               ; preds = %16
  store i32 2, ptr %5, align 4
  br label %52

20:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %21 = load i32, ptr @n, align 4, !tbaa !5
  %22 = sub nsw i32 %21, 1
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = sub nsw i32 %25, 1
  %27 = call noundef i32 @_Z6ansGetiiiiii(i32 noundef 1, i32 noundef 0, i32 noundef %22, i32 noundef %23, i32 noundef %24, i32 noundef %26)
  store i32 %27, ptr %7, align 4, !tbaa !5
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = icmp sgt i32 %28, 0
  br i1 %29, label %30, label %41

30:                                               ; preds = %20
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sub nsw i32 %31, 1
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [100005 x [4 x i32]], ptr @ans, i64 0, i64 %33
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [4 x i32], ptr %34, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = load i32, ptr %7, align 4, !tbaa !5
  %40 = sub nsw i32 %39, %38
  store i32 %40, ptr %7, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %30, %20
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = icmp slt i32 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %41
  store i32 1000000000, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %46

45:                                               ; preds = %41
  store i32 0, ptr %5, align 4
  br label %46

46:                                               ; preds = %45, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %47 = load i32, ptr %5, align 4
  switch i32 %47, label %52 [
    i32 0, label %48
  ]

48:                                               ; preds = %46
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %6, align 4, !tbaa !5
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %6, align 4, !tbaa !5
  br label %16, !llvm.loop !9

52:                                               ; preds = %46, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %53 = load i32, ptr %5, align 4
  switch i32 %53, label %56 [
    i32 2, label %54
  ]

54:                                               ; preds = %52
  %55 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %55, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %56

56:                                               ; preds = %54, %52, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %57 = load i32, ptr %2, align 4
  ret i32 %57
}

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


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

@n = external dso_local local_unnamed_addr global i32, align 4
@ans = external dso_local local_unnamed_addr global [100005 x [4 x i32]], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z6getNxti(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z6ansGetiiiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z8findNexti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef i32 @_Z6getNxti(i32 noundef %0)
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %.not = icmp slt i32 %2, %3
  br i1 %.not, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %1
  %4 = add nsw i32 %2, -1
  %5 = icmp sgt i32 %0, 0
  %6 = add nsw i32 %0, -1
  %7 = zext nneg i32 %6 to i64
  %8 = add nsw i32 %3, -1
  %9 = tail call noundef i32 @_Z6ansGetiiiiii(i32 noundef 1, i32 noundef 0, i32 noundef %8, i32 noundef 0, i32 noundef %0, i32 noundef %4)
  br i1 %5, label %.preheader.split.us.preheader, label %.preheader.split.preheader

.preheader.split.preheader:                       ; preds = %.preheader
  %10 = icmp sgt i32 %9, -1
  br i1 %10, label %.preheader.split.1, label %.loopexit

.preheader.split.us.preheader:                    ; preds = %.preheader
  %11 = getelementptr inbounds [100005 x [4 x i32]], ptr @ans, i64 0, i64 %7, i64 0
  %12 = load i32, ptr %11, align 16, !tbaa !5
  %.not26 = icmp slt i32 %9, %12
  br i1 %.not26, label %.loopexit, label %.preheader.split.us.1

.preheader.split.us.1:                            ; preds = %.preheader.split.us.preheader
  %13 = load i32, ptr @n, align 4, !tbaa !5
  %14 = add nsw i32 %13, -1
  %15 = tail call noundef i32 @_Z6ansGetiiiiii(i32 noundef 1, i32 noundef 0, i32 noundef %14, i32 noundef 1, i32 noundef %0, i32 noundef %4)
  %16 = getelementptr inbounds [100005 x [4 x i32]], ptr @ans, i64 0, i64 %7, i64 1
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %.not26.1 = icmp slt i32 %15, %17
  br i1 %.not26.1, label %.loopexit, label %.preheader.split.us.2

.preheader.split.us.2:                            ; preds = %.preheader.split.us.1
  %18 = load i32, ptr @n, align 4, !tbaa !5
  %19 = add nsw i32 %18, -1
  %20 = tail call noundef i32 @_Z6ansGetiiiiii(i32 noundef 1, i32 noundef 0, i32 noundef %19, i32 noundef 2, i32 noundef %0, i32 noundef %4)
  %21 = getelementptr inbounds [100005 x [4 x i32]], ptr @ans, i64 0, i64 %7, i64 2
  %22 = load i32, ptr %21, align 8, !tbaa !5
  %.not26.2 = icmp slt i32 %20, %22
  br i1 %.not26.2, label %.loopexit, label %.preheader.split.us.3

.preheader.split.us.3:                            ; preds = %.preheader.split.us.2
  %23 = load i32, ptr @n, align 4, !tbaa !5
  %24 = add nsw i32 %23, -1
  %25 = tail call noundef i32 @_Z6ansGetiiiiii(i32 noundef 1, i32 noundef 0, i32 noundef %24, i32 noundef 3, i32 noundef %0, i32 noundef %4)
  %26 = getelementptr inbounds [100005 x [4 x i32]], ptr @ans, i64 0, i64 %7, i64 3
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %.not26.3 = icmp slt i32 %25, %27
  %spec.select = select i1 %.not26.3, i32 1000000000, i32 %2
  br label %.loopexit

.preheader.split.1:                               ; preds = %.preheader.split.preheader
  %28 = load i32, ptr @n, align 4, !tbaa !5
  %29 = add nsw i32 %28, -1
  %30 = tail call noundef i32 @_Z6ansGetiiiiii(i32 noundef 1, i32 noundef 0, i32 noundef %29, i32 noundef 1, i32 noundef %0, i32 noundef %4)
  %31 = icmp sgt i32 %30, -1
  br i1 %31, label %.preheader.split.2, label %.loopexit

.preheader.split.2:                               ; preds = %.preheader.split.1
  %32 = load i32, ptr @n, align 4, !tbaa !5
  %33 = add nsw i32 %32, -1
  %34 = tail call noundef i32 @_Z6ansGetiiiiii(i32 noundef 1, i32 noundef 0, i32 noundef %33, i32 noundef 2, i32 noundef %0, i32 noundef %4)
  %35 = icmp sgt i32 %34, -1
  br i1 %35, label %.preheader.split.3, label %.loopexit

.preheader.split.3:                               ; preds = %.preheader.split.2
  %36 = load i32, ptr @n, align 4, !tbaa !5
  %37 = add nsw i32 %36, -1
  %38 = tail call noundef i32 @_Z6ansGetiiiiii(i32 noundef 1, i32 noundef 0, i32 noundef %37, i32 noundef 3, i32 noundef %0, i32 noundef %4)
  %39 = icmp sgt i32 %38, -1
  %spec.select30 = select i1 %39, i32 %2, i32 1000000000
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader.split.3, %.preheader.split.us.3, %.preheader.split.preheader, %.preheader.split.1, %.preheader.split.2, %.preheader.split.us.preheader, %.preheader.split.us.1, %.preheader.split.us.2, %1
  %.3 = phi i32 [ %2, %1 ], [ 1000000000, %.preheader.split.us.preheader ], [ 1000000000, %.preheader.split.us.1 ], [ 1000000000, %.preheader.split.us.2 ], [ 1000000000, %.preheader.split.preheader ], [ 1000000000, %.preheader.split.1 ], [ 1000000000, %.preheader.split.2 ], [ %spec.select, %.preheader.split.us.3 ], [ %spec.select30, %.preheader.split.3 ]
  ret i32 %.3
}

attributes #0 = { mustprogress 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++ TBAA"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass