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

; 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
@sum = 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 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6harry1v() #0 {
  %1 = alloca i32, align 4
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n, ptr noundef @x)
  store i64 0, ptr @maxScore, align 8, !tbaa !5
  store i64 0, ptr @minScore, align 8, !tbaa !5
  store i64 0, ptr @sum, 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 = %32, %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 %35

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 @sum, align 8, !tbaa !5
  %13 = add nsw i64 %12, %11
  store i64 %13, ptr @sum, align 8, !tbaa !5
  %14 = load i64, ptr @in, align 8, !tbaa !5
  %15 = load i64, ptr @x, align 8, !tbaa !5
  %16 = srem i64 %14, %15
  %17 = icmp ne i64 %16, 0
  br i1 %17, label %18, label %25

18:                                               ; preds = %9
  %19 = load i64, ptr @in, align 8, !tbaa !5
  %20 = load i64, ptr @x, align 8, !tbaa !5
  %21 = sdiv i64 %19, %20
  %22 = add nsw i64 %21, 1
  %23 = load i64, ptr @maxScore, align 8, !tbaa !5
  %24 = add nsw i64 %23, %22
  store i64 %24, ptr @maxScore, align 8, !tbaa !5
  br label %31

25:                                               ; preds = %9
  %26 = load i64, ptr @in, align 8, !tbaa !5
  %27 = load i64, ptr @x, align 8, !tbaa !5
  %28 = sdiv i64 %26, %27
  %29 = load i64, ptr @maxScore, align 8, !tbaa !5
  %30 = add nsw i64 %29, %28
  store i64 %30, ptr @maxScore, align 8, !tbaa !5
  br label %31

31:                                               ; preds = %25, %18
  br label %32

32:                                               ; preds = %31
  %33 = load i32, ptr %1, align 4, !tbaa !9
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %1, align 4, !tbaa !9
  br label %3, !llvm.loop !11

35:                                               ; preds = %8
  %36 = load i64, ptr @sum, align 8, !tbaa !5
  %37 = load i64, ptr @x, align 8, !tbaa !5
  %38 = srem i64 %36, %37
  %39 = icmp ne i64 %38, 0
  br i1 %39, label %40, label %45

40:                                               ; preds = %35
  %41 = load i64, ptr @sum, align 8, !tbaa !5
  %42 = load i64, ptr @x, align 8, !tbaa !5
  %43 = sdiv i64 %41, %42
  %44 = add nsw i64 %43, 1
  store i64 %44, ptr @minScore, align 8, !tbaa !5
  br label %49

45:                                               ; preds = %35
  %46 = load i64, ptr @sum, align 8, !tbaa !5
  %47 = load i64, ptr @x, align 8, !tbaa !5
  %48 = sdiv i64 %46, %47
  store i64 %48, ptr @minScore, align 8, !tbaa !5
  br label %49

49:                                               ; preds = %45, %40
  %50 = load i64, ptr @minScore, align 8, !tbaa !5
  %51 = load i64, ptr @maxScore, align 8, !tbaa !5
  %52 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %50, i64 noundef %51)
  ret void
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #1

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

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

declare i32 @printf(ptr noundef, ...) #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 = { "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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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
@sum = external dso_local local_unnamed_addr 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 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z6harry1v() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, 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
  store i64 0, ptr @sum, 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:                                      ; preds = %25, %.._crit_edge_crit_edge
  %4 = phi i64 [ 0, %.._crit_edge_crit_edge ], [ %storemerge3, %25 ]
  %5 = phi i64 [ %.pre, %.._crit_edge_crit_edge ], [ %15, %25 ]
  %6 = phi i64 [ 0, %.._crit_edge_crit_edge ], [ %14, %25 ]
  %7 = srem i64 %6, %5
  %.not = icmp ne i64 %7, 0
  %8 = sdiv i64 %6, %5
  %9 = zext i1 %.not to i64
  %storemerge = add nsw i64 %8, %9
  store i64 %storemerge, ptr @minScore, align 8, !tbaa !5
  %10 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %storemerge, i64 noundef %4)
  ret void

.lr.ph:                                           ; preds = %0, %25
  %indvars.iv = phi i64 [ %indvars.iv.next, %25 ], [ 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 @sum, align 8, !tbaa !5
  %14 = add nsw i64 %13, %12
  store i64 %14, ptr @sum, align 8, !tbaa !5
  %15 = load i64, ptr @x, align 8, !tbaa !5
  %16 = srem i64 %12, %15
  %.not2 = icmp eq i64 %16, 0
  %17 = sdiv i64 %12, %15
  br i1 %.not2, label %22, label %18

18:                                               ; preds = %.lr.ph
  %19 = add nsw i64 %17, 1
  %20 = load i64, ptr @maxScore, align 8, !tbaa !5
  %21 = add nsw i64 %19, %20
  br label %25

22:                                               ; preds = %.lr.ph
  %23 = load i64, ptr @maxScore, align 8, !tbaa !5
  %24 = add nsw i64 %23, %17
  br label %25

25:                                               ; preds = %18, %22
  %storemerge3 = phi i64 [ %24, %22 ], [ %21, %18 ]
  store i64 %storemerge3, ptr @maxScore, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = load i64, ptr @n, align 8, !tbaa !5
  %27 = icmp sgt i64 %26, %indvars.iv.next
  br i1 %27, label %.lr.ph, label %._crit_edge, !llvm.loop !9
}

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

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

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

!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,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass