; LLVM IR from ippeb/ACM-ICPC :: algebra/bignum/bignum.ll (Z5printR6bignumb)

; 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.bignum = type { [12 x i64], i32 }

@POW10 = external dso_local global [20 x i64], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 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 @_Z5printR6bignumb(ptr noundef nonnull align 8 dereferenceable(104) %0, i1 noundef zeroext %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %8 = zext i1 %1 to i8
  store i8 %8, ptr %4, align 1, !tbaa !9
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.bignum, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.bignum, ptr %11, i32 0, i32 1
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = sub nsw i32 %13, 1
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [12 x i64], ptr %10, i64 0, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !14
  %18 = call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %17)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.bignum, ptr %19, i32 0, i32 1
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = sub nsw i32 %21, 2
  store i32 %22, ptr %5, align 4, !tbaa !16
  br label %23

23:                                               ; preds = %50, %2
  %24 = load i32, ptr %5, align 4, !tbaa !16
  %25 = icmp sge i32 %24, 0
  br i1 %25, label %27, label %26

26:                                               ; preds = %23
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %53

27:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 8, ptr %7, align 4, !tbaa !16
  br label %28

28:                                               ; preds = %46, %27
  %29 = load i32, ptr %7, align 4, !tbaa !16
  %30 = icmp sge i32 %29, 0
  br i1 %30, label %32, label %31

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

32:                                               ; preds = %28
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.bignum, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %5, align 4, !tbaa !16
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [12 x i64], ptr %34, i64 0, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !14
  %39 = load i32, ptr %7, align 4, !tbaa !16
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [20 x i64], ptr @POW10, i64 0, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !14
  %43 = sdiv i64 %38, %42
  %44 = srem i64 %43, 10
  %45 = call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %44)
  br label %46

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

49:                                               ; preds = %31
  br label %50

50:                                               ; preds = %49
  %51 = load i32, ptr %5, align 4, !tbaa !16
  %52 = add nsw i32 %51, -1
  store i32 %52, ptr %5, align 4, !tbaa !16
  br label %23, !llvm.loop !19

53:                                               ; preds = %26
  %54 = load i8, ptr %4, align 1, !tbaa !9, !range !20, !noundef !21
  %55 = trunc i8 %54 to i1
  br i1 %55, label %56, label %58

56:                                               ; preds = %53
  %57 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  br label %58

58:                                               ; preds = %56, %53
  ret void
}

declare i32 @printf(ptr 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 = { "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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{!12, !13, i64 96}
!12 = !{!"_ZTS6bignum", !7, i64 0, !13, i64 96}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"long long", !7, i64 0}
!16 = !{!13, !13, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}
!20 = !{i8 0, i8 2}
!21 = !{}


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

@POW10 = external dso_local local_unnamed_addr global [20 x i64], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z5printR6bignumb(ptr nocapture noundef nonnull readonly align 8 dereferenceable(104) %0, i1 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 96
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = add nsw i32 %4, -1
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds [12 x i64], ptr %0, i64 0, i64 %6
  %8 = load i64, ptr %7, align 8, !tbaa !10
  %9 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %8)
  %10 = load i32, ptr %3, align 8, !tbaa !5
  %11 = icmp sgt i32 %10, 1
  br i1 %11, label %.preheader.preheader, label %._crit_edge

.preheader.preheader:                             ; preds = %2
  %12 = add nsw i32 %10, -2
  %13 = zext nneg i32 %12 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %indvars.iv = phi i64 [ %13, %.preheader.preheader ], [ %indvars.iv.next, %.preheader ]
  %14 = getelementptr inbounds [12 x i64], ptr %0, i64 0, i64 %indvars.iv
  %15 = load i64, ptr %14, align 8, !tbaa !10
  %16 = load i64, ptr getelementptr inbounds (i8, ptr @POW10, i64 64), align 16, !tbaa !10
  %17 = sdiv i64 %15, %16
  %18 = srem i64 %17, 10
  %19 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %18)
  %20 = load i64, ptr %14, align 8, !tbaa !10
  %21 = load i64, ptr getelementptr inbounds (i8, ptr @POW10, i64 56), align 8, !tbaa !10
  %22 = sdiv i64 %20, %21
  %23 = srem i64 %22, 10
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %23)
  %25 = load i64, ptr %14, align 8, !tbaa !10
  %26 = load i64, ptr getelementptr inbounds (i8, ptr @POW10, i64 48), align 16, !tbaa !10
  %27 = sdiv i64 %25, %26
  %28 = srem i64 %27, 10
  %29 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %28)
  %30 = load i64, ptr %14, align 8, !tbaa !10
  %31 = load i64, ptr getelementptr inbounds (i8, ptr @POW10, i64 40), align 8, !tbaa !10
  %32 = sdiv i64 %30, %31
  %33 = srem i64 %32, 10
  %34 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %33)
  %35 = load i64, ptr %14, align 8, !tbaa !10
  %36 = load i64, ptr getelementptr inbounds (i8, ptr @POW10, i64 32), align 16, !tbaa !10
  %37 = sdiv i64 %35, %36
  %38 = srem i64 %37, 10
  %39 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %38)
  %40 = load i64, ptr %14, align 8, !tbaa !10
  %41 = load i64, ptr getelementptr inbounds (i8, ptr @POW10, i64 24), align 8, !tbaa !10
  %42 = sdiv i64 %40, %41
  %43 = srem i64 %42, 10
  %44 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %43)
  %45 = load i64, ptr %14, align 8, !tbaa !10
  %46 = load i64, ptr getelementptr inbounds (i8, ptr @POW10, i64 16), align 16, !tbaa !10
  %47 = sdiv i64 %45, %46
  %48 = srem i64 %47, 10
  %49 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %48)
  %50 = load i64, ptr %14, align 8, !tbaa !10
  %51 = load i64, ptr getelementptr inbounds (i8, ptr @POW10, i64 8), align 8, !tbaa !10
  %52 = sdiv i64 %50, %51
  %53 = srem i64 %52, 10
  %54 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %53)
  %55 = load i64, ptr %14, align 8, !tbaa !10
  %56 = load i64, ptr @POW10, align 16, !tbaa !10
  %57 = sdiv i64 %55, %56
  %58 = srem i64 %57, 10
  %59 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %58)
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %.not = icmp eq i64 %indvars.iv, 0
  br i1 %.not, label %._crit_edge, label %.preheader, !llvm.loop !12

._crit_edge:                                      ; preds = %.preheader, %2
  br i1 %1, label %60, label %62

60:                                               ; preds = %._crit_edge
  %61 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  br label %62

62:                                               ; preds = %60, %._crit_edge
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) 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 = { 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, !9, i64 96}
!6 = !{!"_ZTS6bignum", !7, i64 0, !9, i64 96}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long long", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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