; LLVM IR from tangjz/acm-icpc :: buaa/oj3rd/968.ll (Z5writei)

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

@len = external dso_local global i32, align 4
@p = external dso_local global [3 x i32], align 4

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5writei(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %6, %1
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = icmp ne i32 %4, 0
  br i1 %5, label %6, label %15

6:                                                ; preds = %3
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = srem i32 %7, 10
  %9 = load i32, ptr @len, align 4, !tbaa !5
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @len, align 4, !tbaa !5
  %11 = sext i32 %9 to i64
  %12 = getelementptr inbounds [3 x i32], ptr @p, i64 0, i64 %11
  store i32 %8, ptr %12, align 4, !tbaa !5
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sdiv i32 %13, 10
  store i32 %14, ptr %2, align 4, !tbaa !5
  br label %3, !llvm.loop !9

15:                                               ; preds = %3
  br label %16

16:                                               ; preds = %19, %15
  %17 = load i32, ptr @len, align 4, !tbaa !5
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %27

19:                                               ; preds = %16
  %20 = load i32, ptr @len, align 4, !tbaa !5
  %21 = add nsw i32 %20, -1
  store i32 %21, ptr @len, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [3 x i32], ptr @p, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = add nsw i32 %24, 48
  %26 = call i32 @putchar(i32 noundef %25)
  br label %16, !llvm.loop !11

27:                                               ; preds = %16
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @putchar(i32 noundef) #1

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

@len = external dso_local local_unnamed_addr global i32, align 4
@p = external dso_local local_unnamed_addr global [3 x i32], align 4

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z5writei(i32 noundef %0) local_unnamed_addr #0 {
  %len.promoted = load i32, ptr @len, align 4, !tbaa !5
  %.not5 = icmp eq i32 %0, 0
  br i1 %.not5, label %.preheader, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %2 = sext i32 %len.promoted to i64
  br label %.lr.ph

..preheader_crit_edge:                            ; preds = %.lr.ph
  %3 = trunc nsw i64 %indvars.iv.next to i32
  store i32 %3, ptr @len, align 4, !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %..preheader_crit_edge, %1
  %4 = phi i32 [ %3, %..preheader_crit_edge ], [ %len.promoted, %1 ]
  %.not47 = icmp eq i32 %4, 0
  br i1 %.not47, label %._crit_edge, label %.lr.ph8

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %2, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %.06 = phi i32 [ %0, %.lr.ph.preheader ], [ %7, %.lr.ph ]
  %5 = srem i32 %.06, 10
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %6 = getelementptr inbounds [3 x i32], ptr @p, i64 0, i64 %indvars.iv
  store i32 %5, ptr %6, align 4, !tbaa !5
  %7 = sdiv i32 %.06, 10
  %.06.off = add i32 %.06, 9
  %.not = icmp ult i32 %.06.off, 19
  br i1 %.not, label %..preheader_crit_edge, label %.lr.ph, !llvm.loop !9

.lr.ph8:                                          ; preds = %.preheader, %.lr.ph8
  %8 = phi i32 [ %15, %.lr.ph8 ], [ %4, %.preheader ]
  %9 = add nsw i32 %8, -1
  store i32 %9, ptr @len, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [3 x i32], ptr @p, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  %13 = add nsw i32 %12, 48
  %14 = tail call i32 @putchar(i32 noundef %13)
  %15 = load i32, ptr @len, align 4, !tbaa !5
  %.not4 = icmp eq i32 %15, 0
  br i1 %.not4, label %._crit_edge, label %.lr.ph8, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph8, %.preheader
  ret void
}

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @putchar(i32 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nofree 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 = { inlinehint mustprogress nofree 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" }

!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,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass