; LLVM IR from Izaron/ACM-ICPC :: Timus Online Judge/Volume 1/1085. Meeting.ll (Z4getti)

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

@cnt = external dso_local global [105 x [105 x i32]], align 16
@k = external dso_local global i32, align 4
@money = external dso_local global [105 x i32], align 16
@place = external dso_local global [105 x i32], align 16
@pr = external dso_local global [105 x i32], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z4getti(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
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %76, %1
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = load i32, ptr @k, align 4, !tbaa !5
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  store i32 2, ptr %6, align 4
  br label %79

14:                                               ; preds = %9
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [105 x i32], ptr @place, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [105 x [105 x i32]], ptr @cnt, i64 0, i64 %19
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [105 x i32], ptr %20, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = icmp eq i32 %24, 100000000
  br i1 %25, label %26, label %27

26:                                               ; preds = %14
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %79

27:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [105 x i32], ptr @place, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [105 x [105 x i32]], ptr @cnt, i64 0, i64 %32
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [105 x i32], ptr %33, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = mul nsw i32 %37, 4
  store i32 %38, ptr %7, align 4, !tbaa !5
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [105 x i32], ptr @money, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = icmp slt i32 %42, %43
  br i1 %44, label %45, label %52

45:                                               ; preds = %27
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [105 x i32], ptr @pr, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = icmp ne i32 %49, 1
  br i1 %50, label %51, label %52

51:                                               ; preds = %45
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %73

52:                                               ; preds = %45, %27
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [105 x i32], ptr @pr, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = icmp ne i32 %56, 1
  br i1 %57, label %58, label %72

58:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !5
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [105 x i32], ptr @place, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [105 x [105 x i32]], ptr @cnt, i64 0, i64 %63
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [105 x i32], ptr %64, i64 0, i64 %66
  %68 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %8, ptr noundef nonnull align 4 dereferenceable(4) %67)
  %69 = load i32, ptr %68, align 4, !tbaa !5
  %70 = load i32, ptr %4, align 4, !tbaa !5
  %71 = add nsw i32 %70, %69
  store i32 %71, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %72

72:                                               ; preds = %58, %52
  store i32 0, ptr %6, align 4
  br label %73

73:                                               ; preds = %72, %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %74 = load i32, ptr %6, align 4
  switch i32 %74, label %79 [
    i32 0, label %75
  ]

75:                                               ; preds = %73
  br label %76

76:                                               ; preds = %75
  %77 = load i32, ptr %5, align 4, !tbaa !5
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %5, align 4, !tbaa !5
  br label %9, !llvm.loop !9

79:                                               ; preds = %73, %26, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %80 = load i32, ptr %6, align 4
  switch i32 %80, label %83 [
    i32 2, label %81
  ]

81:                                               ; preds = %79
  %82 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %82, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %83

83:                                               ; preds = %81, %79
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %84 = load i32, ptr %2, align 4
  ret i32 %84
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

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

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 = { inlinehint mustprogress 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 #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++ 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"

@cnt = external dso_local global [105 x [105 x i32]], align 16
@k = external dso_local local_unnamed_addr global i32, align 4
@money = external dso_local local_unnamed_addr global [105 x i32], align 16
@place = external dso_local local_unnamed_addr global [105 x i32], align 16
@pr = external dso_local local_unnamed_addr global [105 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z4getti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  %3 = load i32, ptr @k, align 4, !tbaa !5
  %.not2731 = icmp sgt i32 %3, 0
  br i1 %.not2731, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %4 = sext i32 %0 to i64
  br label %5

5:                                                ; preds = %.lr.ph, %.thread
  %6 = phi i32 [ %3, %.lr.ph ], [ %26, %.thread ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %.thread ]
  %.02132 = phi i32 [ 0, %.lr.ph ], [ %.223.ph, %.thread ]
  %7 = getelementptr inbounds [105 x i32], ptr @place, i64 0, i64 %indvars.iv
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [105 x [105 x i32]], ptr @cnt, i64 0, i64 %9, i64 %4
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp eq i32 %11, 100000000
  br i1 %12, label %._crit_edge, label %13

13:                                               ; preds = %5
  %14 = shl nsw i32 %11, 2
  %15 = getelementptr inbounds [105 x i32], ptr @money, i64 0, i64 %indvars.iv
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp slt i32 %16, %14
  %18 = getelementptr inbounds [105 x i32], ptr @pr, i64 0, i64 %indvars.iv
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %.not = icmp eq i32 %19, 1
  br i1 %17, label %20, label %21

20:                                               ; preds = %13
  br i1 %.not, label %.thread, label %._crit_edge

21:                                               ; preds = %13
  br i1 %.not, label %.thread, label %22

22:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  %23 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %2, ptr noundef nonnull align 4 dereferenceable(4) %10)
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = add nsw i32 %24, %.02132
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  %.pre40 = load i32, ptr @k, align 4, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %20, %22, %21
  %26 = phi i32 [ %6, %21 ], [ %.pre40, %22 ], [ %6, %20 ]
  %.223.ph = phi i32 [ %.02132, %21 ], [ %25, %22 ], [ %.02132, %20 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %27 = sext i32 %26 to i64
  %.not27 = icmp slt i64 %indvars.iv.next, %27
  br i1 %.not27, label %5, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.thread, %5, %20, %1
  %spec.select = phi i32 [ 0, %1 ], [ -1, %20 ], [ -1, %5 ], [ %.223.ph, %.thread ]
  ret i32 %spec.select
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

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

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint mustprogress 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 #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++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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