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

; 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.Node = type { i32, i32 }

@fact = external dso_local global [10007 x i32], align 16

; 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 i64 @_Z8mod_facti(i32 noundef %0) #1 {
  %2 = alloca %struct.Node, align 4
  %3 = alloca i32, align 4
  %4 = alloca %struct.Node, align 4
  %5 = alloca %struct.Node, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %6 = load i32, ptr %3, align 4, !tbaa !5
  %7 = icmp ne i32 %6, 0
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  br i1 %7, label %8, label %36

8:                                                ; preds = %1
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = sdiv i32 %9, 10007
  %11 = call i64 @_Z8mod_facti(i32 noundef %10)
  store i64 %11, ptr %4, align 4
  %12 = getelementptr inbounds %struct.Node, ptr %5, i32 0, i32 0
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sdiv i32 %13, 10007
  %15 = and i32 %14, 1
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %24

17:                                               ; preds = %8
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = srem i32 %18, 10007
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [10007 x i32], ptr @fact, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = sub nsw i32 10007, %22
  br label %30

24:                                               ; preds = %8
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = srem i32 %25, 10007
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [10007 x i32], ptr @fact, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %24, %17
  %31 = phi i32 [ %23, %17 ], [ %29, %24 ]
  store i32 %31, ptr %12, align 4, !tbaa !9
  %32 = getelementptr inbounds %struct.Node, ptr %5, i32 0, i32 1
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = sdiv i32 %33, 10007
  store i32 %34, ptr %32, align 4, !tbaa !11
  %35 = call i64 @_ZNK4NodeplERKS_(ptr noundef nonnull align 4 dereferenceable(8) %4, ptr noundef nonnull align 4 dereferenceable(8) %5)
  store i64 %35, ptr %2, align 4
  br label %39

36:                                               ; preds = %1
  %37 = getelementptr inbounds %struct.Node, ptr %2, i32 0, i32 0
  store i32 1, ptr %37, align 4, !tbaa !9
  %38 = getelementptr inbounds %struct.Node, ptr %2, i32 0, i32 1
  store i32 0, ptr %38, align 4, !tbaa !11
  br label %39

39:                                               ; preds = %36, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %40 = load i64, ptr %2, align 4
  ret i64 %40
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local i64 @_ZNK4NodeplERKS_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(8)) #2 align 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 = { 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 #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 = !{!10, !6, i64 0}
!10 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4}
!11 = !{!10, !6, i64 4}


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

%struct.Node = type { i32, i32 }

@fact = external dso_local local_unnamed_addr global [10007 x i32], align 16

; 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 nounwind uwtable
define dso_local i64 @_Z8mod_facti(i32 noundef %0) local_unnamed_addr #1 {
  %2 = alloca %struct.Node, align 8
  %3 = alloca %struct.Node, align 4
  %.not = icmp eq i32 %0, 0
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #2
  br i1 %.not, label %16, label %4

4:                                                ; preds = %1
  %5 = sdiv i32 %0, 10007
  %6 = tail call i64 @_Z8mod_facti(i32 noundef %5)
  store i64 %6, ptr %2, align 8
  %7 = and i32 %5, 1
  %.not6 = icmp eq i32 %7, 0
  %8 = srem i32 %0, 10007
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [10007 x i32], ptr @fact, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = sub nsw i32 10007, %11
  %13 = select i1 %.not6, i32 %11, i32 %12
  store i32 %13, ptr %3, align 4, !tbaa !9
  %14 = getelementptr inbounds i8, ptr %3, i64 4
  store i32 %5, ptr %14, align 4, !tbaa !11
  %15 = call i64 @_ZNK4NodeplERKS_(ptr noundef nonnull align 4 dereferenceable(8) %2, ptr noundef nonnull align 4 dereferenceable(8) %3)
  br label %16

16:                                               ; preds = %1, %4
  %.sroa.0.0.insert.insert = phi i64 [ %15, %4 ], [ 1, %1 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #2
  ret i64 %.sroa.0.0.insert.insert
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local i64 @_ZNK4NodeplERKS_(ptr noundef nonnull align 4 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(8)) local_unnamed_addr #1 align 2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !6, i64 0}
!10 = !{!"_ZTS4Node", !6, i64 0, !6, i64 4}
!11 = !{!10, !6, i64 4}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass