; LLVM IR from tangjz/acm-icpc :: hackerrank (beautiful-string)

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

@c1 = external dso_local global i32, align 4
@c2 = external dso_local global i32, align 4
@c3 = external dso_local global i32, align 4
@buf = external dso_local global [1000001 x i8], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

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

5:                                                ; preds = %70, %0
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds [1000001 x i8], ptr @buf, i64 0, i64 %7
  %9 = load i8, ptr %8, align 1, !tbaa !9
  %10 = icmp ne i8 %9, 0
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %5
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %3, align 4, !tbaa !5
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [1000001 x i8], ptr @buf, i64 0, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = sext i8 %18 to i32
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1000001 x i8], ptr @buf, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %25 = sext i8 %24 to i32
  %26 = icmp ne i32 %19, %25
  br i1 %26, label %27, label %35

27:                                               ; preds = %12
  %28 = load i32, ptr @c1, align 4, !tbaa !5
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr @c1, align 4, !tbaa !5
  %30 = load i32, ptr %3, align 4, !tbaa !5
  %31 = icmp sge i32 %30, 2
  %32 = zext i1 %31 to i32
  %33 = load i32, ptr @c2, align 4, !tbaa !5
  %34 = add nsw i32 %33, %32
  store i32 %34, ptr @c2, align 4, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %27, %12
  %36 = load i32, ptr %2, align 4, !tbaa !5
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %65

38:                                               ; preds = %35
  %39 = load i32, ptr %2, align 4, !tbaa !5
  %40 = sub nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [1000001 x i8], ptr @buf, i64 0, i64 %41
  %43 = load i8, ptr %42, align 1, !tbaa !9
  %44 = sext i8 %43 to i32
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1000001 x i8], ptr @buf, i64 0, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !9
  %49 = sext i8 %48 to i32
  %50 = icmp ne i32 %44, %49
  br i1 %50, label %51, label %65

51:                                               ; preds = %38
  %52 = load i32, ptr %2, align 4, !tbaa !5
  %53 = sub nsw i32 %52, 1
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [1000001 x i8], ptr @buf, i64 0, i64 %54
  %56 = load i8, ptr %55, align 1, !tbaa !9
  %57 = sext i8 %56 to i32
  %58 = load i32, ptr %2, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [1000001 x i8], ptr @buf, i64 0, i64 %60
  %62 = load i8, ptr %61, align 1, !tbaa !9
  %63 = sext i8 %62 to i32
  %64 = icmp eq i32 %57, %63
  br label %65

65:                                               ; preds = %51, %38, %35
  %66 = phi i1 [ false, %38 ], [ false, %35 ], [ %64, %51 ]
  %67 = zext i1 %66 to i32
  %68 = load i32, ptr @c3, align 4, !tbaa !5
  %69 = add nsw i32 %68, %67
  store i32 %69, ptr @c3, align 4, !tbaa !5
  br label %70

70:                                               ; preds = %65
  %71 = load i32, ptr %2, align 4, !tbaa !5
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %2, align 4, !tbaa !5
  br label %5, !llvm.loop !10

73:                                               ; preds = %11
  %74 = load i32, ptr @c1, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = load i32, ptr @c1, align 4, !tbaa !5
  %77 = sext i32 %76 to i64
  %78 = sub nsw i64 %77, 1
  %79 = mul nsw i64 %75, %78
  %80 = sdiv i64 %79, 2
  %81 = load i32, ptr @c2, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = add nsw i64 %80, %82
  %84 = load i32, ptr @c3, align 4, !tbaa !5
  %85 = sext i32 %84 to i64
  %86 = sub nsw i64 %83, %85
  %87 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %86)
  ret i32 0
}

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

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

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

declare i32 @printf(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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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"}


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

@c1 = external dso_local local_unnamed_addr global i32, align 4
@c2 = external dso_local local_unnamed_addr global i32, align 4
@c3 = external dso_local local_unnamed_addr global i32, align 4
@buf = external dso_local global [1000001 x i8], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress nofree norecurse nounwind 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 @buf)
  %c1.promoted = load i32, ptr @c1, align 4, !tbaa !5
  %c2.promoted = load i32, ptr @c2, align 4, !tbaa !5
  %c3.promoted = load i32, ptr @c3, align 4, !tbaa !5
  %2 = load i8, ptr @buf, align 16, !tbaa !9
  %.not17 = icmp eq i8 %2, 0
  br i1 %.not17, label %8, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %3 = load i8, ptr getelementptr inbounds (i8, ptr @buf, i64 1), align 1, !tbaa !9
  %.not14.peel = icmp eq i8 %2, %3
  br i1 %.not14.peel, label %.lr.ph.preheader31, label %4

4:                                                ; preds = %.lr.ph.preheader
  %5 = add nsw i32 %c1.promoted, 1
  store i32 %5, ptr @c1, align 4, !tbaa !5
  %.not.peel = icmp eq i8 %3, 0
  br i1 %.not.peel, label %._crit_edge, label %.lr.ph.preheader31

.lr.ph.preheader31:                               ; preds = %.lr.ph.preheader, %4
  %.019.ph = phi i32 [ 0, %4 ], [ 1, %.lr.ph.preheader ]
  %.ph = phi i32 [ %5, %4 ], [ %c1.promoted, %.lr.ph.preheader ]
  br label %.lr.ph

._crit_edge:                                      ; preds = %34, %4
  %6 = phi i32 [ %c2.promoted, %4 ], [ %35, %34 ]
  %7 = phi i32 [ %5, %4 ], [ %37, %34 ]
  %.lcssa = phi i32 [ %c3.promoted, %4 ], [ %43, %34 ]
  store i32 %.lcssa, ptr @c3, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %._crit_edge, %0
  %9 = phi i32 [ %.lcssa, %._crit_edge ], [ %c3.promoted, %0 ]
  %10 = phi i32 [ %6, %._crit_edge ], [ %c2.promoted, %0 ]
  %11 = phi i32 [ %7, %._crit_edge ], [ %c1.promoted, %0 ]
  %12 = sext i32 %11 to i64
  %13 = add nsw i64 %12, -1
  %14 = mul nsw i64 %13, %12
  %15 = sdiv i64 %14, 2
  %16 = sext i32 %10 to i64
  %17 = sext i32 %9 to i64
  %18 = sub nsw i64 %16, %17
  %19 = add nsw i64 %18, %15
  %20 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %19)
  ret i32 0

.lr.ph:                                           ; preds = %.lr.ph.preheader31, %34
  %21 = phi i32 [ %35, %34 ], [ %c2.promoted, %.lr.ph.preheader31 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %34 ], [ 1, %.lr.ph.preheader31 ]
  %22 = phi i8 [ %28, %34 ], [ %3, %.lr.ph.preheader31 ]
  %.019 = phi i32 [ %.1, %34 ], [ %.019.ph, %.lr.ph.preheader31 ]
  %23 = phi i32 [ %37, %34 ], [ %.ph, %.lr.ph.preheader31 ]
  %24 = phi i32 [ %36, %34 ], [ %c2.promoted, %.lr.ph.preheader31 ]
  %25 = phi i32 [ %43, %34 ], [ %c3.promoted, %.lr.ph.preheader31 ]
  %26 = add nsw i32 %.019, 1
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %27 = getelementptr inbounds [1000001 x i8], ptr @buf, i64 0, i64 %indvars.iv.next
  %28 = load i8, ptr %27, align 1, !tbaa !9
  %.not14 = icmp eq i8 %22, %28
  br i1 %.not14, label %34, label %29

29:                                               ; preds = %.lr.ph
  %30 = add nsw i32 %23, 1
  store i32 %30, ptr @c1, align 4, !tbaa !5
  %31 = icmp sgt i32 %.019, 0
  %32 = zext i1 %31 to i32
  %33 = add nsw i32 %24, %32
  store i32 %33, ptr @c2, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %29, %.lr.ph
  %35 = phi i32 [ %33, %29 ], [ %21, %.lr.ph ]
  %36 = phi i32 [ %33, %29 ], [ %24, %.lr.ph ]
  %37 = phi i32 [ %30, %29 ], [ %23, %.lr.ph ]
  %.1 = phi i32 [ 0, %29 ], [ %26, %.lr.ph ]
  %38 = add nsw i64 %indvars.iv, -1
  %39 = getelementptr inbounds [1000001 x i8], ptr @buf, i64 0, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !9
  %.not16 = icmp ne i8 %40, %22
  %41 = icmp eq i8 %40, %28
  %spec.select = and i1 %.not16, %41
  %42 = zext i1 %spec.select to i32
  %43 = add nsw i32 %25, %42
  %.not = icmp eq i8 %28, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10
}

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

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

attributes #0 = { mustprogress nofree norecurse 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 = { 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 = !{!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, !12}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!"llvm.loop.peeled.count", i32 1}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopFullUnrollPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass