; LLVM IR from ravireddy07/cpp-book :: src/Code-Forces/#694_strange-partition.ll (Z5harryv)

; 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 i64, align 8
@x = external dso_local global i64, align 8
@in = external dso_local global i64, align 8
@maxScore = external dso_local global i64, align 8
@minScore = external dso_local global i64, align 8
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [9 x i8], align 1

declare i32 @__isoc99_scanf(ptr 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

declare i32 @printf(ptr noundef, ...) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5harryv() #2 {
  %1 = alloca i32, align 4
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.3, ptr noundef @n, ptr noundef @x)
  store i64 0, ptr @maxScore, align 8, !tbaa !5
  store i64 0, ptr @minScore, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !9
  br label %3

3:                                                ; preds = %22, %0
  %4 = load i32, ptr %1, align 4, !tbaa !9
  %5 = sext i32 %4 to i64
  %6 = load i64, ptr @n, align 8, !tbaa !5
  %7 = icmp slt i64 %5, %6
  br i1 %7, label %9, label %8

8:                                                ; preds = %3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %25

9:                                                ; preds = %3
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef @in)
  %11 = load i64, ptr @in, align 8, !tbaa !5
  %12 = load i64, ptr @x, align 8, !tbaa !5
  %13 = add nsw i64 %11, %12
  %14 = sub nsw i64 %13, 1
  %15 = load i64, ptr @x, align 8, !tbaa !5
  %16 = sdiv i64 %14, %15
  %17 = load i64, ptr @maxScore, align 8, !tbaa !5
  %18 = add nsw i64 %17, %16
  store i64 %18, ptr @maxScore, align 8, !tbaa !5
  %19 = load i64, ptr @in, align 8, !tbaa !5
  %20 = load i64, ptr @minScore, align 8, !tbaa !5
  %21 = add nsw i64 %20, %19
  store i64 %21, ptr @minScore, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %9
  %23 = load i32, ptr %1, align 4, !tbaa !9
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %1, align 4, !tbaa !9
  br label %3, !llvm.loop !11

25:                                               ; preds = %8
  %26 = load i64, ptr @minScore, align 8, !tbaa !5
  %27 = load i64, ptr @x, align 8, !tbaa !5
  %28 = add nsw i64 %26, %27
  %29 = sub nsw i64 %28, 1
  %30 = load i64, ptr @x, align 8, !tbaa !5
  %31 = sdiv i64 %29, %30
  store i64 %31, ptr @minScore, align 8, !tbaa !5
  %32 = load i64, ptr @minScore, align 8, !tbaa !5
  %33 = load i64, ptr @maxScore, align 8, !tbaa !5
  %34 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %32, i64 noundef %33)
  ret void
}

attributes #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 = { 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 #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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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 global i64, align 8
@x = external dso_local global i64, align 8
@in = external dso_local global i64, align 8
@maxScore = external dso_local local_unnamed_addr global i64, align 8
@minScore = external dso_local local_unnamed_addr global i64, align 8
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [9 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z5harryv() local_unnamed_addr #1 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.3, ptr noundef nonnull @n, ptr noundef nonnull @x)
  store i64 0, ptr @maxScore, align 8, !tbaa !5
  store i64 0, ptr @minScore, align 8, !tbaa !5
  %2 = load i64, ptr @n, align 8, !tbaa !5
  %3 = icmp sgt i64 %2, 0
  br i1 %3, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %0
  %.pre = load i64, ptr @x, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %4 = add i64 %20, -1
  br label %._crit_edge

._crit_edge:                                      ; preds = %.._crit_edge_crit_edge, %._crit_edge.loopexit
  %5 = phi i64 [ %18, %._crit_edge.loopexit ], [ 0, %.._crit_edge_crit_edge ]
  %6 = phi i64 [ %13, %._crit_edge.loopexit ], [ %.pre, %.._crit_edge_crit_edge ]
  %7 = phi i64 [ %4, %._crit_edge.loopexit ], [ -1, %.._crit_edge_crit_edge ]
  %8 = add i64 %7, %6
  %9 = sdiv i64 %8, %6
  store i64 %9, ptr @minScore, align 8, !tbaa !5
  %10 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %9, i64 noundef %5)
  ret void

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %11 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull @in)
  %12 = load i64, ptr @in, align 8, !tbaa !5
  %13 = load i64, ptr @x, align 8, !tbaa !5
  %14 = add i64 %12, -1
  %15 = add i64 %14, %13
  %16 = sdiv i64 %15, %13
  %17 = load i64, ptr @maxScore, align 8, !tbaa !5
  %18 = add nsw i64 %17, %16
  store i64 %18, ptr @maxScore, align 8, !tbaa !5
  %19 = load i64, ptr @minScore, align 8, !tbaa !5
  %20 = add nsw i64 %19, %12
  store i64 %20, ptr @minScore, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %21 = load i64, ptr @n, align 8, !tbaa !5
  %22 = icmp sgt i64 %21, %indvars.iv.next
  br i1 %22, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !9
}

attributes #0 = { nofree nounwind "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 nofree 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" }

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass