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

; 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
@pos = external dso_local global [262144 x i32], align 16
@val = external dso_local global [262144 x i32], align 16
@ans = external dso_local global [262144 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5buildiii(i32 noundef, i32 noundef, 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 uwtable
declare dso_local noundef i32 @_Z5queryiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef) #0

; 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
  store i32 0, ptr %1, align 4
  br label %5

5:                                                ; preds = %71, %0
  %6 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  %7 = icmp eq i32 %6, 1
  br i1 %7, label %8, label %72

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

9:                                                ; preds = %22, %8
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = load i32, ptr @n, align 4, !tbaa !5
  %12 = icmp sle i32 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %25

14:                                               ; preds = %9
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds i32, ptr @pos, i64 %16
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i32, ptr @val, i64 %19
  %21 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %17, ptr noundef %20)
  br label %22

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

25:                                               ; preds = %13
  %26 = load i32, ptr @n, align 4, !tbaa !5
  call void @_Z5buildiii(i32 noundef 1, i32 noundef 1, i32 noundef %26)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %27 = load i32, ptr @n, align 4, !tbaa !5
  store i32 %27, ptr %3, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %46, %25
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = icmp sge i32 %29, 1
  br i1 %30, label %32, label %31

31:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %49

32:                                               ; preds = %28
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [262144 x i32], ptr @val, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !5
  %37 = load i32, ptr @n, align 4, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [262144 x i32], ptr @pos, i64 0, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  %43 = call noundef i32 @_Z5queryiiii(i32 noundef 1, i32 noundef 1, i32 noundef %37, i32 noundef %42)
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [262144 x i32], ptr @ans, i64 0, i64 %44
  store i32 %36, ptr %45, align 4, !tbaa !5
  br label %46

46:                                               ; preds = %32
  %47 = load i32, ptr %3, align 4, !tbaa !5
  %48 = add nsw i32 %47, -1
  store i32 %48, ptr %3, align 4, !tbaa !5
  br label %28, !llvm.loop !11

49:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 1, ptr %4, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %68, %49
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = load i32, ptr @n, align 4, !tbaa !5
  %53 = icmp sle i32 %51, %52
  br i1 %53, label %55, label %54

54:                                               ; preds = %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %71

55:                                               ; preds = %50
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [262144 x i32], ptr @ans, i64 0, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !5
  %60 = load i32, ptr %4, align 4, !tbaa !5
  %61 = load i32, ptr @n, align 4, !tbaa !5
  %62 = icmp eq i32 %60, %61
  %63 = zext i1 %62 to i64
  %64 = getelementptr inbounds [3 x i8], ptr @.str.3, i64 0, i64 %63
  %65 = load i8, ptr %64, align 1, !tbaa !12
  %66 = sext i8 %65 to i32
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %59, i32 noundef %66)
  br label %68

68:                                               ; preds = %55
  %69 = load i32, ptr %4, align 4, !tbaa !5
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %4, align 4, !tbaa !5
  br label %50, !llvm.loop !13

71:                                               ; preds = %54
  br label %5, !llvm.loop !14

72:                                               ; preds = %5
  ret i32 0
}

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

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

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 = { 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 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !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
@pos = external dso_local global [262144 x i32], align 16
@val = external dso_local global [262144 x i32], align 16
@ans = external dso_local local_unnamed_addr global [262144 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z5buildiii(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z5queryiiii(i32 noundef, i32 noundef, i32 noundef, 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)
  %2 = icmp eq i32 %1, 1
  br i1 %2, label %.preheader17, label %._crit_edge26

.loopexit:                                        ; preds = %.lr.ph25, %._crit_edge, %.preheader
  %3 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %4 = icmp eq i32 %3, 1
  br i1 %4, label %.preheader17, label %._crit_edge26, !llvm.loop !5

.preheader17:                                     ; preds = %0, %.loopexit
  %5 = load i32, ptr @n, align 4, !tbaa !7
  %.not18 = icmp slt i32 %5, 1
  br i1 %.not18, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.preheader17
  %.lcssa = phi i32 [ %5, %.preheader17 ], [ %12, %.lr.ph ]
  tail call void @_Z5buildiii(i32 noundef 1, i32 noundef 1, i32 noundef %.lcssa)
  %6 = load i32, ptr @n, align 4, !tbaa !7
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph22.preheader, label %.loopexit

.lr.ph22.preheader:                               ; preds = %._crit_edge
  %8 = zext nneg i32 %6 to i64
  br label %.lr.ph22

.lr.ph:                                           ; preds = %.preheader17, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %.preheader17 ]
  %9 = getelementptr inbounds i32, ptr @pos, i64 %indvars.iv
  %10 = getelementptr inbounds i32, ptr @val, i64 %indvars.iv
  %11 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %9, ptr noundef nonnull %10)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %12 = load i32, ptr @n, align 4, !tbaa !7
  %13 = sext i32 %12 to i64
  %.not.not = icmp slt i64 %indvars.iv, %13
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !11

.preheader:                                       ; preds = %.lr.ph22
  %.pre = load i32, ptr @n, align 4, !tbaa !7
  %.not1623 = icmp slt i32 %.pre, 1
  br i1 %.not1623, label %.loopexit, label %.lr.ph25

.lr.ph22:                                         ; preds = %.lr.ph22.preheader, %.lr.ph22
  %indvars.iv29 = phi i64 [ %8, %.lr.ph22.preheader ], [ %indvars.iv.next30, %.lr.ph22 ]
  %14 = getelementptr inbounds [262144 x i32], ptr @val, i64 0, i64 %indvars.iv29
  %15 = load i32, ptr %14, align 4, !tbaa !7
  %16 = load i32, ptr @n, align 4, !tbaa !7
  %17 = getelementptr inbounds [262144 x i32], ptr @pos, i64 0, i64 %indvars.iv29
  %18 = load i32, ptr %17, align 4, !tbaa !7
  %19 = add nsw i32 %18, 1
  %20 = tail call noundef i32 @_Z5queryiiii(i32 noundef 1, i32 noundef 1, i32 noundef %16, i32 noundef %19)
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [262144 x i32], ptr @ans, i64 0, i64 %21
  store i32 %15, ptr %22, align 4, !tbaa !7
  %indvars.iv.next30 = add nsw i64 %indvars.iv29, -1
  %23 = icmp ugt i64 %indvars.iv29, 1
  br i1 %23, label %.lr.ph22, label %.preheader, !llvm.loop !12

.lr.ph25:                                         ; preds = %.preheader, %.lr.ph25
  %indvars.iv32 = phi i64 [ %indvars.iv.next33, %.lr.ph25 ], [ 1, %.preheader ]
  %24 = phi i32 [ %34, %.lr.ph25 ], [ %.pre, %.preheader ]
  %25 = getelementptr inbounds [262144 x i32], ptr @ans, i64 0, i64 %indvars.iv32
  %26 = load i32, ptr %25, align 4, !tbaa !7
  %27 = zext i32 %24 to i64
  %28 = icmp eq i64 %indvars.iv32, %27
  %29 = zext i1 %28 to i64
  %30 = getelementptr inbounds [3 x i8], ptr @.str.3, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !13
  %32 = sext i8 %31 to i32
  %33 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %26, i32 noundef %32)
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %34 = load i32, ptr @n, align 4, !tbaa !7
  %35 = sext i32 %34 to i64
  %.not16.not = icmp slt i64 %indvars.iv32, %35
  br i1 %.not16.not, label %.lr.ph25, label %.loopexit, !llvm.loop !14

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

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

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

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 = { 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" }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = distinct !{!11, !6}
!12 = distinct !{!12, !6}
!13 = !{!9, !9, i64 0}
!14 = distinct !{!14, !6}


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