; LLVM IR from tangjz/acm-icpc :: poj (3579)

; 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
@x = external dso_local global [100010 x i32], align 16
@m = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3cnti(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 norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  br label %6

6:                                                ; preds = %64, %0
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  %8 = icmp ne i32 %7, -1
  br i1 %8, label %9, label %67

9:                                                ; preds = %6
  %10 = load i32, ptr @n, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = load i32, ptr @n, align 4, !tbaa !5
  %13 = sub nsw i32 %12, 1
  %14 = sext i32 %13 to i64
  %15 = mul nsw i64 %11, %14
  %16 = sdiv i64 %15, 2
  %17 = add nsw i64 %16, 1
  %18 = sdiv i64 %17, 2
  store i64 %18, ptr @m, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %29, %9
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = load i32, ptr @n, align 4, !tbaa !5
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %32

24:                                               ; preds = %19
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr @x, i64 %26
  %28 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %27)
  br label %29

29:                                               ; preds = %24
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %2, align 4, !tbaa !5
  br label %19, !llvm.loop !11

32:                                               ; preds = %23
  %33 = load i32, ptr @n, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr @x, i64 %34
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef @x, ptr noundef %35)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %36 = load i32, ptr @n, align 4, !tbaa !5
  %37 = sub nsw i32 %36, 1
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [100010 x i32], ptr @x, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = load i32, ptr @x, align 16, !tbaa !5
  %42 = sub nsw i32 %40, %41
  store i32 %42, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  br label %43

43:                                               ; preds = %63, %32
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %47, label %64

47:                                               ; preds = %43
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = load i32, ptr %3, align 4, !tbaa !5
  %51 = sub nsw i32 %49, %50
  %52 = ashr i32 %51, 1
  %53 = add nsw i32 %48, %52
  store i32 %53, ptr %5, align 4, !tbaa !5
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = call noundef i64 @_Z3cnti(i32 noundef %54)
  %56 = load i64, ptr @m, align 8, !tbaa !9
  %57 = icmp slt i64 %55, %56
  br i1 %57, label %58, label %61

58:                                               ; preds = %47
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %3, align 4, !tbaa !5
  br label %63

61:                                               ; preds = %47
  %62 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %62, ptr %4, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %61, %58
  br label %43, !llvm.loop !13

64:                                               ; preds = %43
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %65)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %6, !llvm.loop !14

67:                                               ; preds = %6
  ret i32 0
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) #4

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

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 = { mustprogress norecurse 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 = { "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 #4 = { inlinehint 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 #5 = { 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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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 i32, align 4
@x = external dso_local global [100010 x i32], align 16
@m = external dso_local local_unnamed_addr global i64, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

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

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %.not23 = icmp eq i32 %1, -1
  br i1 %.not23, label %._crit_edge26, label %.lr.ph25

.lr.ph25:                                         ; preds = %0, %._crit_edge21
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = sext i32 %2 to i64
  %4 = add nsw i32 %2, -1
  %5 = sext i32 %4 to i64
  %6 = mul nsw i64 %5, %3
  %7 = sdiv i64 %6, 2
  %8 = add nsw i64 %7, 1
  %9 = sdiv i64 %8, 2
  store i64 %9, ptr @m, align 8, !tbaa !9
  %10 = icmp sgt i32 %2, 0
  br i1 %10, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph25
  %.pre-phi = phi i64 [ %3, %.lr.ph25 ], [ %23, %.lr.ph ]
  %11 = getelementptr inbounds i32, ptr @x, i64 %.pre-phi
  tail call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull @x, ptr noundef nonnull %11)
  %12 = load i32, ptr @n, align 4, !tbaa !5
  %13 = add nsw i32 %12, -1
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [100010 x i32], ptr @x, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = load i32, ptr @x, align 16, !tbaa !5
  %18 = sub nsw i32 %16, %17
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %.lr.ph20, label %._crit_edge21

.lr.ph:                                           ; preds = %.lr.ph25, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.lr.ph25 ]
  %20 = getelementptr inbounds i32, ptr @x, i64 %indvars.iv
  %21 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %20)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %22 = load i32, ptr @n, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = icmp slt i64 %indvars.iv.next, %23
  br i1 %24, label %.lr.ph, label %._crit_edge, !llvm.loop !11

.lr.ph20:                                         ; preds = %._crit_edge, %.lr.ph20
  %.01218 = phi i32 [ %.1, %.lr.ph20 ], [ %18, %._crit_edge ]
  %.01317 = phi i32 [ %.114, %.lr.ph20 ], [ 0, %._crit_edge ]
  %25 = sub nsw i32 %.01218, %.01317
  %26 = ashr i32 %25, 1
  %27 = add nsw i32 %26, %.01317
  %28 = tail call noundef i64 @_Z3cnti(i32 noundef %27)
  %29 = load i64, ptr @m, align 8, !tbaa !9
  %30 = icmp slt i64 %28, %29
  %31 = add nsw i32 %27, 1
  %.114 = select i1 %30, i32 %31, i32 %.01317
  %.1 = select i1 %30, i32 %.01218, i32 %27
  %32 = icmp slt i32 %.114, %.1
  br i1 %32, label %.lr.ph20, label %._crit_edge21, !llvm.loop !13

._crit_edge21:                                    ; preds = %.lr.ph20, %._crit_edge
  %.013.lcssa = phi i32 [ 0, %._crit_edge ], [ %.114, %.lr.ph20 ]
  %33 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %.013.lcssa)
  %34 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %.not = icmp eq i32 %34, -1
  br i1 %.not, label %._crit_edge26, label %.lr.ph25, !llvm.loop !14

._crit_edge26:                                    ; preds = %._crit_edge21, %0
  ret i32 0
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_ZSt4sortIPiEvT_S1_(ptr noundef, ptr noundef) local_unnamed_addr #3

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

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 norecurse 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 #2 = { 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 #3 = { inlinehint 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"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


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