; LLVM IR from tangjz/acm-icpc :: 51nod/1162.ll (Z5writenc)

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

@.str = external hidden unnamed_addr constant [3 x i8], align 1

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5writenc(i64 noundef %0, i64 noundef %1, i8 noundef signext %2) #1 {
  %4 = alloca i128, align 16
  %5 = alloca i128, align 16
  %6 = alloca i8, align 1
  %7 = alloca [31 x i8], align 16
  %8 = alloca ptr, align 8
  %9 = getelementptr inbounds { i64, i64 }, ptr %4, i32 0, i32 0
  store i64 %0, ptr %9, align 16
  %10 = getelementptr inbounds { i64, i64 }, ptr %4, i32 0, i32 1
  store i64 %1, ptr %10, align 8
  %11 = load i128, ptr %4, align 16, !tbaa !5
  store i128 %11, ptr %5, align 16, !tbaa !5
  store i8 %2, ptr %6, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 31, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %12 = getelementptr inbounds [31 x i8], ptr %7, i64 0, i64 0
  store ptr %12, ptr %8, align 8, !tbaa !10
  br label %13

13:                                               ; preds = %24, %3
  %14 = load i128, ptr %5, align 16, !tbaa !5
  %15 = icmp sgt i128 %14, 0
  br i1 %15, label %16, label %27

16:                                               ; preds = %13
  %17 = load i128, ptr %5, align 16, !tbaa !5
  %18 = srem i128 %17, 10
  %19 = trunc i128 %18 to i32
  %20 = add nsw i32 48, %19
  %21 = trunc i32 %20 to i8
  %22 = load ptr, ptr %8, align 8, !tbaa !10
  %23 = getelementptr inbounds i8, ptr %22, i32 1
  store ptr %23, ptr %8, align 8, !tbaa !10
  store i8 %21, ptr %22, align 1, !tbaa !9
  br label %24

24:                                               ; preds = %16
  %25 = load i128, ptr %5, align 16, !tbaa !5
  %26 = sdiv i128 %25, 10
  store i128 %26, ptr %5, align 16, !tbaa !5
  br label %13, !llvm.loop !12

27:                                               ; preds = %13
  %28 = getelementptr inbounds [31 x i8], ptr %7, i64 0, i64 0
  %29 = load ptr, ptr %8, align 8, !tbaa !10
  %30 = icmp eq ptr %28, %29
  br i1 %30, label %31, label %34

31:                                               ; preds = %27
  %32 = load ptr, ptr %8, align 8, !tbaa !10
  %33 = getelementptr inbounds i8, ptr %32, i32 1
  store ptr %33, ptr %8, align 8, !tbaa !10
  store i8 48, ptr %32, align 1, !tbaa !9
  br label %34

34:                                               ; preds = %31, %27
  %35 = getelementptr inbounds [31 x i8], ptr %7, i64 0, i64 0
  %36 = load ptr, ptr %8, align 8, !tbaa !10
  call void @_ZSt7reverseIPcEvT_S1_(ptr noundef %35, ptr noundef %36)
  %37 = load ptr, ptr %8, align 8, !tbaa !10
  store i8 0, ptr %37, align 1, !tbaa !9
  %38 = getelementptr inbounds [31 x i8], ptr %7, i64 0, i64 0
  %39 = call i32 (ptr, ...) @printf(ptr noundef @.str, ptr noundef %38)
  %40 = load i8, ptr %6, align 1, !tbaa !9
  %41 = icmp ne i8 %40, 0
  br i1 %41, label %42, label %46

42:                                               ; preds = %34
  %43 = load i8, ptr %6, align 1, !tbaa !9
  %44 = sext i8 %43 to i32
  %45 = call i32 @putchar(i32 noundef %44)
  br label %46

46:                                               ; preds = %42, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 31, ptr %7) #4
  ret void
}

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

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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" }
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 = !{!"__int128", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@.str = external hidden unnamed_addr constant [3 x i8], align 1

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5writenc(i64 noundef %0, i64 noundef %1, i8 noundef signext %2) local_unnamed_addr #1 {
  %4 = alloca [31 x i8], align 16
  %.sroa.2.0.insert.ext = zext i64 %1 to i128
  %.sroa.2.0.insert.shift = shl nuw i128 %.sroa.2.0.insert.ext, 64
  %.sroa.0.0.insert.ext = zext i64 %0 to i128
  %.sroa.0.0.insert.insert = or disjoint i128 %.sroa.2.0.insert.shift, %.sroa.0.0.insert.ext
  call void @llvm.lifetime.start.p0(i64 31, ptr nonnull %4) #5
  %5 = icmp sgt i128 %.sroa.0.0.insert.insert, 0
  br i1 %5, label %.lr.ph, label %11

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.012 = phi ptr [ %10, %.lr.ph ], [ %4, %3 ]
  %.0911 = phi i128 [ %6, %.lr.ph ], [ %.sroa.0.0.insert.insert, %3 ]
  %.0911.frozen = freeze i128 %.0911
  %6 = udiv i128 %.0911.frozen, 10
  %7 = mul i128 %6, 10
  %.decomposed = sub i128 %.0911.frozen, %7
  %8 = trunc nuw nsw i128 %.decomposed to i8
  %9 = or disjoint i8 %8, 48
  %10 = getelementptr inbounds i8, ptr %.012, i64 1
  store i8 %9, ptr %.012, align 1, !tbaa !5
  %.not13 = icmp ult i128 %.0911, 10
  br i1 %.not13, label %._crit_edge, label %.lr.ph, !llvm.loop !8

11:                                               ; preds = %3
  %12 = getelementptr inbounds i8, ptr %4, i64 1
  store i8 48, ptr %4, align 16, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %11
  %.1 = phi ptr [ %12, %11 ], [ %10, %.lr.ph ]
  call void @_ZSt7reverseIPcEvT_S1_(ptr noundef nonnull %4, ptr noundef nonnull %.1)
  store i8 0, ptr %.1, align 1, !tbaa !5
  %13 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, ptr noundef nonnull %4)
  %.not = icmp eq i8 %2, 0
  br i1 %.not, label %17, label %14

14:                                               ; preds = %._crit_edge
  %15 = sext i8 %2 to i32
  %16 = call i32 @putchar(i32 noundef %15)
  br label %17

17:                                               ; preds = %14, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 31, ptr nonnull %4) #5
  ret void
}

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

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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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" }
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 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 #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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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