; LLVM IR from tangjz/acm-icpc :: usaco/Training/Chapter 6 Contest Practice/packrec.ll (Z4copyv)

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

@tmp = external dso_local global %struct.node, align 4
@ans = external dso_local global i32, align 4
@used = external dso_local global [101 x i8], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z4copyv() #0 {
  %1 = load i32, ptr @tmp, align 4, !tbaa !5
  %2 = load i32, ptr getelementptr inbounds (%struct.node, ptr @tmp, i32 0, i32 1), align 4, !tbaa !10
  %3 = mul nsw i32 %1, %2
  %4 = load i32, ptr @ans, align 4, !tbaa !11
  %5 = icmp slt i32 %3, %4
  br i1 %5, label %9, label %6

6:                                                ; preds = %0
  %7 = load i32, ptr @ans, align 4, !tbaa !11
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %13, label %9

9:                                                ; preds = %6, %0
  %10 = load i32, ptr @tmp, align 4, !tbaa !5
  %11 = load i32, ptr getelementptr inbounds (%struct.node, ptr @tmp, i32 0, i32 1), align 4, !tbaa !10
  %12 = mul nsw i32 %10, %11
  store i32 %12, ptr @ans, align 4, !tbaa !11
  call void @llvm.memset.p0.i64(ptr align 16 @used, i8 0, i64 101, i1 false)
  br label %13

13:                                               ; preds = %9, %6
  %14 = load i32, ptr @tmp, align 4, !tbaa !5
  %15 = load i32, ptr getelementptr inbounds (%struct.node, ptr @tmp, i32 0, i32 1), align 4, !tbaa !10
  %16 = mul nsw i32 %14, %15
  %17 = load i32, ptr @ans, align 4, !tbaa !11
  %18 = icmp eq i32 %16, %17
  br i1 %18, label %19, label %24

19:                                               ; preds = %13
  %20 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @tmp, ptr noundef nonnull align 4 dereferenceable(4) getelementptr inbounds (%struct.node, ptr @tmp, i32 0, i32 1))
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [101 x i8], ptr @used, i64 0, i64 %22
  store i8 1, ptr %23, align 1, !tbaa !12
  br label %24

24:                                               ; preds = %19, %13
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { inlinehint 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" }

!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, !7, i64 0}
!6 = !{!"_ZTS4node", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"bool", !8, i64 0}


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

@tmp = external dso_local global %struct.node, align 4
@ans = external dso_local local_unnamed_addr global i32, align 4
@used = external dso_local local_unnamed_addr global [101 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z4copyv() local_unnamed_addr #0 {
  %1 = load i32, ptr @tmp, align 4, !tbaa !5
  %2 = load i32, ptr getelementptr inbounds (i8, ptr @tmp, i64 4), align 4, !tbaa !10
  %3 = mul nsw i32 %2, %1
  %4 = load i32, ptr @ans, align 4
  %5 = icmp sge i32 %3, %4
  %6 = icmp ne i32 %4, 0
  %or.cond = and i1 %5, %6
  br i1 %or.cond, label %7, label %.thread

.thread:                                          ; preds = %0
  store i32 %3, ptr @ans, align 4, !tbaa !11
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(101) @used, i8 0, i64 101, i1 false)
  br label %9

7:                                                ; preds = %0
  %8 = icmp eq i32 %3, %4
  br i1 %8, label %9, label %14

9:                                                ; preds = %.thread, %7
  %10 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) @tmp, ptr noundef nonnull align 4 dereferenceable(4) getelementptr inbounds (i8, ptr @tmp, i64 4))
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [101 x i8], ptr @used, i64 0, i64 %12
  store i8 1, ptr %13, align 1, !tbaa !12
  br label %14

14:                                               ; preds = %9, %7
  ret void
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { inlinehint 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" }

!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, !7, i64 0}
!6 = !{!"_ZTS4node", !7, i64 0, !7, i64 4}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"bool", !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SimplifyCFGPass,TailCallElimPass