; LLVM IR from tangjz/acm-icpc :: self_training/2015-05-01 (H)

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

%struct.Segment = type { i32, [5 x i64] }

@seg = external dso_local global [262144 x %struct.Segment], align 16
@n = external dso_local global i32, align 4
@tot = external dso_local global i32, align 4
@que = external dso_local global [131072 x i32], align 16
@typ = external dso_local global [131072 x i32], align 16
@val = external dso_local global [131072 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3updiiiii(i32 noundef, i32 noundef, 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 norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca [5 x i8], align 1
  %4 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %5 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @n)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %41, %0
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = icmp slt i32 %7, %8
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 5, ptr %3) #5
  %12 = getelementptr inbounds [5 x i8], ptr %3, i64 0, i64 0
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %12)
  %14 = getelementptr inbounds [5 x i8], ptr %3, i64 0, i64 0
  %15 = load i8, ptr %14, align 1, !tbaa !9
  %16 = sext i8 %15 to i32
  %17 = icmp ne i32 %16, 115
  br i1 %17, label %18, label %40

18:                                               ; preds = %11
  %19 = getelementptr inbounds [5 x i8], ptr %3, i64 0, i64 0
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %21 = sext i8 %20 to i32
  %22 = icmp eq i32 %21, 97
  %23 = zext i1 %22 to i64
  %24 = select i1 %22, i32 1, i32 -1
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [131072 x i32], ptr @typ, i64 0, i64 %26
  store i32 %24, ptr %27, align 4, !tbaa !5
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr @val, i64 %29
  %31 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %30)
  %32 = load i32, ptr %2, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [131072 x i32], ptr @val, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = load i32, ptr @tot, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr @tot, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [131072 x i32], ptr @que, i64 0, i64 %38
  store i32 %35, ptr %39, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %18, %11
  call void @llvm.lifetime.end.p0(i64 5, ptr %3) #5
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !10

44:                                               ; preds = %10
  %45 = load i32, ptr @tot, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i32, ptr @que, i64 %46
  %48 = getelementptr inbounds i32, ptr %47, i64 1
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef getelementptr inbounds (i32, ptr @que, i64 1), ptr noundef %48)
  %49 = load i32, ptr @tot, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i32, ptr @que, i64 %50
  %52 = getelementptr inbounds i32, ptr %51, i64 1
  %53 = call noundef ptr @_ZSt6uniqueIPiET_S1_S1_(ptr noundef getelementptr inbounds (i32, ptr @que, i64 1), ptr noundef %52)
  %54 = ptrtoint ptr %53 to i64
  %55 = sub i64 %54, ptrtoint (ptr @que to i64)
  %56 = sdiv exact i64 %55, 4
  %57 = sub nsw i64 %56, 1
  %58 = trunc i64 %57 to i32
  store i32 %58, ptr @tot, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %99, %44
  %60 = load i32, ptr %4, align 4, !tbaa !5
  %61 = load i32, ptr @n, align 4, !tbaa !5
  %62 = icmp slt i32 %60, %61
  br i1 %62, label %64, label %63

63:                                               ; preds = %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %102

64:                                               ; preds = %59
  %65 = load i32, ptr %4, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [131072 x i32], ptr @typ, i64 0, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !5
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %95

70:                                               ; preds = %64
  %71 = load i32, ptr @tot, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds i32, ptr @que, i64 %72
  %74 = getelementptr inbounds i32, ptr %73, i64 1
  %75 = load i32, ptr %4, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [131072 x i32], ptr @val, i64 0, i64 %76
  %78 = call noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef getelementptr inbounds (i32, ptr @que, i64 1), ptr noundef %74, ptr noundef nonnull align 4 dereferenceable(4) %77)
  %79 = ptrtoint ptr %78 to i64
  %80 = sub i64 %79, ptrtoint (ptr @que to i64)
  %81 = sdiv exact i64 %80, 4
  %82 = trunc i64 %81 to i32
  %83 = load i32, ptr %4, align 4, !tbaa !5
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds [131072 x i32], ptr @val, i64 0, i64 %84
  store i32 %82, ptr %85, align 4, !tbaa !5
  %86 = load i32, ptr @tot, align 4, !tbaa !5
  %87 = load i32, ptr %4, align 4, !tbaa !5
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [131072 x i32], ptr @val, i64 0, i64 %88
  %90 = load i32, ptr %89, align 4, !tbaa !5
  %91 = load i32, ptr %4, align 4, !tbaa !5
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [131072 x i32], ptr @typ, i64 0, i64 %92
  %94 = load i32, ptr %93, align 4, !tbaa !5
  call void @_Z3updiiiii(i32 noundef 1, i32 noundef 1, i32 noundef %86, i32 noundef %90, i32 noundef %94)
  br label %98

95:                                               ; preds = %64
  %96 = load i64, ptr getelementptr inbounds ([5 x i64], ptr getelementptr inbounds (%struct.Segment, ptr getelementptr inbounds ([262144 x %struct.Segment], ptr @seg, i64 0, i64 1), i32 0, i32 1), i64 0, i64 3), align 8, !tbaa !12
  %97 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %96)
  br label %98

98:                                               ; preds = %95, %70
  br label %99

99:                                               ; preds = %98
  %100 = load i32, ptr %4, align 4, !tbaa !5
  %101 = add nsw i32 %100, 1
  store i32 %101, ptr %4, align 4, !tbaa !5
  br label %59, !llvm.loop !14

102:                                              ; preds = %63
  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

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef, ptr noundef, ptr noundef nonnull align 4 dereferenceable(4)) #4

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


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

%struct.Segment = type { i32, [5 x i64] }

@seg = external dso_local local_unnamed_addr global [262144 x %struct.Segment], align 16
@n = external dso_local global i32, align 4
@tot = external dso_local local_unnamed_addr global i32, align 4
@que = external dso_local global [131072 x i32], align 16
@typ = external dso_local local_unnamed_addr global [131072 x i32], align 16
@val = external dso_local global [131072 x i32], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1

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

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

; Function Attrs: mustprogress 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() local_unnamed_addr #2 {
  %1 = alloca [5 x i8], align 1
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @n)
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %34, %0
  %5 = load i32, ptr @tot, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds i32, ptr @que, i64 %6
  %8 = getelementptr inbounds i8, ptr %7, i64 4
  call void @_ZSt4sortIPiEvT_S1_(ptr noundef nonnull getelementptr inbounds (i8, ptr @que, i64 4), ptr noundef nonnull %8)
  %9 = load i32, ptr @tot, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i32, ptr @que, i64 %10
  %12 = getelementptr inbounds i8, ptr %11, i64 4
  %13 = call noundef ptr @_ZSt6uniqueIPiET_S1_S1_(ptr noundef nonnull getelementptr inbounds (i8, ptr @que, i64 4), ptr noundef nonnull %12)
  %14 = ptrtoint ptr %13 to i64
  %15 = sub i64 %14, ptrtoint (ptr @que to i64)
  %16 = lshr exact i64 %15, 2
  %17 = trunc i64 %16 to i32
  %18 = add i32 %17, -1
  store i32 %18, ptr @tot, align 4, !tbaa !5
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %.lr.ph18, label %._crit_edge19

.lr.ph:                                           ; preds = %0, %34
  %indvars.iv = phi i64 [ %indvars.iv.next, %34 ], [ 0, %0 ]
  call void @llvm.lifetime.start.p0(i64 5, ptr nonnull %1) #5
  %21 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %1)
  %22 = load i8, ptr %1, align 1, !tbaa !9
  %.not14 = icmp eq i8 %22, 115
  br i1 %.not14, label %34, label %23

23:                                               ; preds = %.lr.ph
  %24 = icmp eq i8 %22, 97
  %25 = select i1 %24, i32 1, i32 -1
  %26 = getelementptr inbounds [131072 x i32], ptr @typ, i64 0, i64 %indvars.iv
  store i32 %25, ptr %26, align 4, !tbaa !5
  %27 = getelementptr inbounds i32, ptr @val, i64 %indvars.iv
  %28 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %27)
  %29 = load i32, ptr %27, align 4, !tbaa !5
  %30 = load i32, ptr @tot, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr @tot, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [131072 x i32], ptr @que, i64 0, i64 %32
  store i32 %29, ptr %33, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %23, %.lr.ph
  call void @llvm.lifetime.end.p0(i64 5, ptr nonnull %1) #5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %35 = load i32, ptr @n, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = icmp slt i64 %indvars.iv.next, %36
  br i1 %37, label %.lr.ph, label %._crit_edge, !llvm.loop !10

._crit_edge19:                                    ; preds = %54, %._crit_edge
  ret i32 0

.lr.ph18:                                         ; preds = %._crit_edge, %54
  %indvars.iv21 = phi i64 [ %indvars.iv.next22, %54 ], [ 0, %._crit_edge ]
  %38 = getelementptr inbounds [131072 x i32], ptr @typ, i64 0, i64 %indvars.iv21
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %.not = icmp eq i32 %39, 0
  br i1 %.not, label %51, label %40

40:                                               ; preds = %.lr.ph18
  %41 = load i32, ptr @tot, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %gep = getelementptr i32, ptr getelementptr inbounds (i8, ptr @que, i64 4), i64 %42
  %43 = getelementptr inbounds [131072 x i32], ptr @val, i64 0, i64 %indvars.iv21
  %44 = call noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef nonnull getelementptr inbounds (i8, ptr @que, i64 4), ptr noundef nonnull %gep, ptr noundef nonnull align 4 dereferenceable(4) %43)
  %45 = ptrtoint ptr %44 to i64
  %46 = sub i64 %45, ptrtoint (ptr @que to i64)
  %47 = lshr exact i64 %46, 2
  %48 = trunc i64 %47 to i32
  store i32 %48, ptr %43, align 4, !tbaa !5
  %49 = load i32, ptr @tot, align 4, !tbaa !5
  %50 = load i32, ptr %38, align 4, !tbaa !5
  call void @_Z3updiiiii(i32 noundef 1, i32 noundef 1, i32 noundef %49, i32 noundef %48, i32 noundef %50)
  br label %54

51:                                               ; preds = %.lr.ph18
  %52 = load i64, ptr getelementptr inbounds (i8, ptr @seg, i64 80), align 16, !tbaa !12
  %53 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %52)
  br label %54

54:                                               ; preds = %40, %51
  %indvars.iv.next22 = add nuw nsw i64 %indvars.iv21, 1
  %55 = load i32, ptr @n, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = icmp slt i64 %indvars.iv.next22, %56
  br i1 %57, label %.lr.ph18, label %._crit_edge19, !llvm.loop !14
}

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

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZSt11lower_boundIPiiET_S1_S1_RKT0_(ptr noundef, ptr noundef, ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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 = { mustprogress 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 = { 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 #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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!13, !13, i64 0}
!13 = !{!"long long", !7, i64 0}
!14 = distinct !{!14, !11}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass