; LLVM IR from tangjz/acm-icpc :: TDL_training/2015-11-22/upsolved/G - Garbling Game - tjz.ll (Z9divAndModi)

; 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
@num = external dso_local global [110 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z9divAndModi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 0, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %5 = load i32, ptr @len, align 4, !tbaa !5
  %6 = sub nsw i32 %5, 1
  store i32 %6, ptr %4, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %32, %1
  %8 = load i32, ptr %4, align 4, !tbaa !5
  %9 = icmp sgt i32 %8, 0
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %7
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [110 x i32], ptr @num, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = load i64, ptr %3, align 8, !tbaa !9
  %18 = add nsw i64 %17, %16
  store i64 %18, ptr %3, align 8, !tbaa !9
  %19 = load i64, ptr %3, align 8, !tbaa !9
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = sdiv i64 %19, %21
  %23 = trunc i64 %22 to i32
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [110 x i32], ptr @num, i64 0, i64 %25
  store i32 %23, ptr %26, align 4, !tbaa !5
  %27 = load i64, ptr %3, align 8, !tbaa !9
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = srem i64 %27, %29
  %31 = mul nsw i64 %30, 1000000000
  store i64 %31, ptr %3, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %11
  %33 = load i32, ptr %4, align 4, !tbaa !5
  %34 = add nsw i32 %33, -1
  store i32 %34, ptr %4, align 4, !tbaa !5
  br label %7, !llvm.loop !11

35:                                               ; preds = %10
  %36 = load i32, ptr @num, align 16, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = load i64, ptr %3, align 8, !tbaa !9
  %39 = add nsw i64 %38, %37
  store i64 %39, ptr %3, align 8, !tbaa !9
  %40 = load i64, ptr %3, align 8, !tbaa !9
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = sdiv i64 %40, %42
  %44 = trunc i64 %43 to i32
  store i32 %44, ptr @num, align 16, !tbaa !5
  %45 = load i32, ptr %2, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = load i64, ptr %3, align 8, !tbaa !9
  %48 = srem i64 %47, %46
  store i64 %48, ptr %3, align 8, !tbaa !9
  br label %49

49:                                               ; preds = %63, %35
  %50 = load i32, ptr @len, align 4, !tbaa !5
  %51 = icmp sgt i32 %50, 1
  br i1 %51, label %52, label %60

52:                                               ; preds = %49
  %53 = load i32, ptr @len, align 4, !tbaa !5
  %54 = sub nsw i32 %53, 1
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [110 x i32], ptr @num, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = icmp ne i32 %57, 0
  %59 = xor i1 %58, true
  br label %60

60:                                               ; preds = %52, %49
  %61 = phi i1 [ false, %49 ], [ %59, %52 ]
  br i1 %61, label %62, label %66

62:                                               ; preds = %60
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr @len, align 4, !tbaa !5
  %65 = add nsw i32 %64, -1
  store i32 %65, ptr @len, align 4, !tbaa !5
  br label %49, !llvm.loop !13

66:                                               ; preds = %60
  %67 = load i64, ptr %3, align 8, !tbaa !9
  %68 = trunc i64 %67 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i32 %68
}

; 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

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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
@num = external dso_local local_unnamed_addr global [110 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef range(i32 -2147483647, -2147483648) i32 @_Z9divAndModi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @len, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 1
  %4 = sext i32 %0 to i64
  br i1 %3, label %.lr.ph, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %1
  %5 = load i32, ptr @num, align 16, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = sdiv i64 %6, %4
  %8 = trunc i64 %7 to i32
  store i32 %8, ptr @num, align 16, !tbaa !5
  br label %.critedge

.lr.ph:                                           ; preds = %1
  %9 = zext nneg i32 %2 to i64
  br label %16

._crit_edge:                                      ; preds = %16
  %10 = load i32, ptr @num, align 16, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = add nsw i64 %24, %11
  %13 = sdiv i64 %12, %4
  %14 = trunc i64 %13 to i32
  store i32 %14, ptr @num, align 16, !tbaa !5
  br i1 %3, label %.lr.ph20.preheader, label %.critedge

.lr.ph20.preheader:                               ; preds = %._crit_edge
  %15 = zext nneg i32 %2 to i64
  br label %.lr.ph20

16:                                               ; preds = %.lr.ph, %16
  %indvars.iv = phi i64 [ %9, %.lr.ph ], [ %indvars.iv.next, %16 ]
  %.01517 = phi i64 [ 0, %.lr.ph ], [ %24, %16 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %17 = getelementptr inbounds [110 x i32], ptr @num, i64 0, i64 %indvars.iv.next
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = add nsw i64 %.01517, %19
  %21 = sdiv i64 %20, %4
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %17, align 4, !tbaa !5
  %23 = srem i64 %20, %4
  %24 = mul nsw i64 %23, 1000000000
  %25 = icmp ugt i64 %indvars.iv, 2
  br i1 %25, label %16, label %._crit_edge, !llvm.loop !9

.lr.ph20:                                         ; preds = %.lr.ph20.preheader, %28
  %indvars.iv23 = phi i64 [ %15, %.lr.ph20.preheader ], [ %indvars.iv.next24, %28 ]
  %indvars.iv.next24 = add nsw i64 %indvars.iv23, -1
  %26 = getelementptr inbounds [110 x i32], ptr @num, i64 0, i64 %indvars.iv.next24
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %.not = icmp eq i32 %27, 0
  br i1 %.not, label %28, label %.critedge

28:                                               ; preds = %.lr.ph20
  %29 = trunc nuw nsw i64 %indvars.iv.next24 to i32
  store i32 %29, ptr @len, align 4, !tbaa !5
  %30 = icmp ugt i64 %indvars.iv23, 2
  br i1 %30, label %.lr.ph20, label %.critedge, !llvm.loop !11

.critedge:                                        ; preds = %.lr.ph20, %28, %._crit_edge.thread, %._crit_edge
  %31 = phi i64 [ %6, %._crit_edge.thread ], [ %12, %._crit_edge ], [ %12, %28 ], [ %12, %.lr.ph20 ]
  %32 = srem i64 %31, %4
  %33 = trunc nsw i64 %32 to i32
  ret i32 %33
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass