; LLVM IR from tangjz/acm-icpc :: zoj/3881.ll (Z1sx)

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

@c = external dso_local global [1000001 x i32], align 16
@res = external dso_local global i32, align 4
@lastn = external dso_local global i64, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z1sx(i64 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  %6 = load i64, ptr @lastn, align 8, !tbaa !5
  %7 = load i64, ptr %3, align 8, !tbaa !5
  %8 = icmp eq i64 %6, %7
  br i1 %8, label %9, label %11

9:                                                ; preds = %1
  %10 = load i32, ptr @res, align 4, !tbaa !9
  store i32 %10, ptr %2, align 4
  br label %61

11:                                               ; preds = %1
  %12 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %12, ptr @lastn, align 8, !tbaa !5
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = icmp slt i64 %13, 1000001
  br i1 %14, label %15, label %19

15:                                               ; preds = %11
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds [1000001 x i32], ptr @c, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !9
  store i32 %18, ptr @res, align 4, !tbaa !9
  store i32 %18, ptr %2, align 4
  br label %61

19:                                               ; preds = %11
  store i32 0, ptr @res, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  br label %20

20:                                               ; preds = %51, %19
  %21 = load i64, ptr %4, align 8, !tbaa !5
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = icmp sle i64 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %54

25:                                               ; preds = %20
  %26 = load i64, ptr %3, align 8, !tbaa !5
  %27 = load i64, ptr %3, align 8, !tbaa !5
  %28 = load i64, ptr %4, align 8, !tbaa !5
  %29 = sdiv i64 %27, %28
  %30 = sdiv i64 %26, %29
  store i64 %30, ptr %5, align 8, !tbaa !5
  %31 = load i32, ptr @res, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = load i64, ptr %3, align 8, !tbaa !5
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = sdiv i64 %33, %34
  %36 = srem i64 %35, 1000000009
  %37 = load i64, ptr %5, align 8, !tbaa !5
  %38 = load i64, ptr %4, align 8, !tbaa !5
  %39 = sub nsw i64 %37, %38
  %40 = add nsw i64 %39, 1
  %41 = mul nsw i64 %36, %40
  %42 = srem i64 %41, 1000000009
  %43 = load i64, ptr %4, align 8, !tbaa !5
  %44 = load i64, ptr %5, align 8, !tbaa !5
  %45 = add nsw i64 %43, %44
  %46 = srem i64 %45, 1000000009
  %47 = mul nsw i64 %42, %46
  %48 = add nsw i64 %32, %47
  %49 = srem i64 %48, 1000000009
  %50 = trunc i64 %49 to i32
  store i32 %50, ptr @res, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %25
  %52 = load i64, ptr %5, align 8, !tbaa !5
  %53 = add nsw i64 %52, 1
  store i64 %53, ptr %4, align 8, !tbaa !5
  br label %20, !llvm.loop !11

54:                                               ; preds = %24
  %55 = load i32, ptr @res, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = mul nsw i64 %56, 500000005
  %58 = srem i64 %57, 1000000009
  %59 = trunc i64 %58 to i32
  store i32 %59, ptr @res, align 4, !tbaa !9
  %60 = load i32, ptr @res, align 4, !tbaa !9
  store i32 %60, ptr %2, align 4
  br label %61

61:                                               ; preds = %54, %15, %9
  %62 = load i32, ptr %2, align 4
  ret i32 %62
}

; 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 = { 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 = { 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 = !{!"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"

@c = external dso_local local_unnamed_addr global [1000001 x i32], align 16
@res = external dso_local local_unnamed_addr global i32, align 4
@lastn = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z1sx(i64 noundef %0) local_unnamed_addr #0 {
  %2 = load i64, ptr @lastn, align 8, !tbaa !5
  %3 = icmp eq i64 %2, %0
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = load i32, ptr @res, align 4, !tbaa !9
  br label %28

6:                                                ; preds = %1
  store i64 %0, ptr @lastn, align 8, !tbaa !5
  %7 = icmp slt i64 %0, 1000001
  br i1 %7, label %8, label %.preheader

8:                                                ; preds = %6
  %9 = getelementptr inbounds [1000001 x i32], ptr @c, i64 0, i64 %0
  %10 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %10, ptr @res, align 4, !tbaa !9
  br label %28

11:                                               ; preds = %.preheader
  %12 = mul nsw i64 %26, 500000005
  %13 = srem i64 %12, 1000000009
  %14 = trunc nsw i64 %13 to i32
  store i32 %14, ptr @res, align 4, !tbaa !9
  br label %28

.preheader:                                       ; preds = %6, %.preheader
  %.01719 = phi i64 [ %27, %.preheader ], [ 1, %6 ]
  %15 = phi i64 [ %26, %.preheader ], [ 0, %6 ]
  %16 = udiv i64 %0, %.01719
  %17 = udiv i64 %0, %16
  %18 = urem i64 %16, 1000000009
  %reass.sub = sub i64 %17, %.01719
  %19 = add i64 %reass.sub, 1
  %20 = mul nsw i64 %19, %18
  %21 = srem i64 %20, 1000000009
  %22 = add nuw nsw i64 %17, %.01719
  %23 = urem i64 %22, 1000000009
  %24 = mul nsw i64 %21, %23
  %25 = add nsw i64 %24, %15
  %26 = srem i64 %25, 1000000009
  %27 = add nuw nsw i64 %17, 1
  %.not.not = icmp slt i64 %17, %0
  br i1 %.not.not, label %.preheader, label %11, !llvm.loop !11

28:                                               ; preds = %11, %8, %4
  %.0 = phi i32 [ %5, %4 ], [ %10, %8 ], [ %14, %11 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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