; LLVM IR from VastoLorde95/Competitive-Programming :: Codeforces/Educational 10/Problem B (B)

; 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
@a = external dso_local global [1001 x i32], align 16
@i = external dso_local global i32, align 4
@j = external dso_local global i32, align 4
@id = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  store i32 0, ptr @i, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %12, %0
  %4 = load i32, ptr @i, align 4, !tbaa !5
  %5 = load i32, ptr @n, align 4, !tbaa !5
  %6 = icmp slt i32 %4, %5
  br i1 %6, label %7, label %15

7:                                                ; preds = %3
  %8 = load i32, ptr @i, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [1001 x i32], ptr @a, i64 0, i64 %9
  %11 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %10)
  br label %12

12:                                               ; preds = %7
  %13 = load i32, ptr @i, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr @i, align 4, !tbaa !5
  br label %3, !llvm.loop !9

15:                                               ; preds = %3
  %16 = load i32, ptr @n, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i32, ptr @a, i64 %17
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef @a, ptr noundef %18)
  store i32 0, ptr @id, align 4, !tbaa !5
  store i32 0, ptr @i, align 4, !tbaa !5
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = sub nsw i32 %19, 1
  store i32 %20, ptr @j, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %41, %15
  %22 = load i32, ptr @i, align 4, !tbaa !5
  %23 = load i32, ptr @j, align 4, !tbaa !5
  %24 = icmp sle i32 %22, %23
  br i1 %24, label %25, label %44

25:                                               ; preds = %21
  %26 = load i32, ptr @id, align 4, !tbaa !5
  %27 = srem i32 %26, 2
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %32

29:                                               ; preds = %25
  %30 = load i32, ptr @i, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr @i, align 4, !tbaa !5
  br label %35

32:                                               ; preds = %25
  %33 = load i32, ptr @j, align 4, !tbaa !5
  %34 = add nsw i32 %33, -1
  store i32 %34, ptr @j, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %32, %29
  %36 = phi i32 [ %30, %29 ], [ %33, %32 ]
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [1001 x i32], ptr @a, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %39)
  br label %41

41:                                               ; preds = %35
  %42 = load i32, ptr @id, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr @id, align 4, !tbaa !5
  br label %21, !llvm.loop !11

44:                                               ; preds = %21
  %45 = call i32 @puts(ptr noundef @.str.2)
  ret i32 0
}

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

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

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

declare i32 @puts(ptr noundef) #1

attributes #0 = { 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 #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 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; 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
@a = external dso_local global [1001 x i32], align 16
@i = external dso_local local_unnamed_addr global i32, align 4
@j = external dso_local local_unnamed_addr global i32, align 4
@id = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  store i32 0, ptr @i, align 4, !tbaa !5
  %2 = load i32, ptr @n, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %storemerge4 = phi i32 [ %8, %.lr.ph ], [ 0, %0 ]
  %4 = sext i32 %storemerge4 to i64
  %5 = getelementptr inbounds [1001 x i32], ptr @a, i64 0, i64 %4
  %6 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %5)
  %7 = load i32, ptr @i, align 4, !tbaa !5
  %8 = add nsw i32 %7, 1
  store i32 %8, ptr @i, align 4, !tbaa !5
  %9 = load i32, ptr @n, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %0
  %.lcssa = phi i32 [ %2, %0 ], [ %9, %.lr.ph ]
  %11 = sext i32 %.lcssa to i64
  %12 = getelementptr inbounds i32, ptr @a, i64 %11
  tail call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull @a, ptr noundef nonnull %12)
  store i32 0, ptr @id, align 4, !tbaa !5
  store i32 0, ptr @i, align 4, !tbaa !5
  %13 = load i32, ptr @n, align 4, !tbaa !5
  %14 = add nsw i32 %13, -1
  store i32 %14, ptr @j, align 4, !tbaa !5
  %.not5 = icmp slt i32 %13, 1
  br i1 %.not5, label %._crit_edge8, label %.lr.ph7

.lr.ph7:                                          ; preds = %._crit_edge, %24
  %15 = phi i32 [ %31, %24 ], [ 0, %._crit_edge ]
  %16 = phi i32 [ %33, %24 ], [ %14, %._crit_edge ]
  %17 = phi i32 [ %32, %24 ], [ 0, %._crit_edge ]
  %18 = and i32 %15, 1
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %22

20:                                               ; preds = %.lr.ph7
  %21 = add nsw i32 %17, 1
  store i32 %21, ptr @i, align 4, !tbaa !5
  br label %24

22:                                               ; preds = %.lr.ph7
  %23 = add nsw i32 %16, -1
  store i32 %23, ptr @j, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %22, %20
  %25 = phi i32 [ %17, %20 ], [ %16, %22 ]
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [1001 x i32], ptr @a, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %28)
  %30 = load i32, ptr @id, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr @id, align 4, !tbaa !5
  %32 = load i32, ptr @i, align 4, !tbaa !5
  %33 = load i32, ptr @j, align 4, !tbaa !5
  %.not = icmp sgt i32 %32, %33
  br i1 %.not, label %._crit_edge8, label %.lr.ph7, !llvm.loop !11

._crit_edge8:                                     ; preds = %24, %._crit_edge
  %34 = tail call i32 @puts(ptr noundef nonnull dereferenceable(1) @.str.2)
  ret i32 0
}

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

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

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

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

attributes #0 = { 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 #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" }
attributes #2 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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