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

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

@rp = external dso_local global [4 x %struct.node], align 16
@tmp = external dso_local global %struct.node, align 4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; 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

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z9transfromi(i32 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3dfsi(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 4
  br i1 %5, label %6, label %7

6:                                                ; preds = %1
  call void @_Z9transfromi(i32 noundef 0)
  br label %7

7:                                                ; preds = %6, %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %8 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %8, ptr %3, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %40, %7
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 4
  br i1 %11, label %13, label %12

12:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %43

13:                                               ; preds = %9
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %15
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 @tmp, ptr align 8 %16, i64 8, i1 false), !tbaa.struct !9
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %18
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %21
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %22, ptr align 8 %19, i64 8, i1 false), !tbaa.struct !9
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %24
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %25, ptr align 4 @tmp, i64 8, i1 false), !tbaa.struct !9
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  call void @_Z3dfsi(i32 noundef %27)
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %29
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 @tmp, ptr align 8 %30, i64 8, i1 false), !tbaa.struct !9
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %32
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %35
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %36, ptr align 8 %33, i64 8, i1 false), !tbaa.struct !9
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %38
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %39, ptr align 4 @tmp, i64 8, i1 false), !tbaa.struct !9
  br label %40

40:                                               ; preds = %13
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %3, align 4, !tbaa !5
  br label %9, !llvm.loop !10

43:                                               ; preds = %12
  ret void
}

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

%struct.node = type { i32, i32 }

@rp = external dso_local local_unnamed_addr global [4 x %struct.node], align 16
@tmp = external dso_local local_unnamed_addr global %struct.node, align 4

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z9transfromi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3dfsi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq i32 %0, 4
  br i1 %2, label %.thread, label %3

.thread:                                          ; preds = %1
  tail call void @_Z9transfromi(i32 noundef 0)
  br label %._crit_edge

3:                                                ; preds = %1
  %4 = icmp slt i32 %0, 4
  br i1 %4, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %5 = sext i32 %0 to i64
  %6 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %5
  %7 = add nsw i32 %0, 1
  br label %8

._crit_edge:                                      ; preds = %8, %.thread, %3
  ret void

8:                                                ; preds = %.lr.ph, %8
  %indvars.iv = phi i64 [ %5, %.lr.ph ], [ %indvars.iv.next, %8 ]
  %9 = load i64, ptr %6, align 8
  store i64 %9, ptr @tmp, align 4
  %10 = getelementptr inbounds [4 x %struct.node], ptr @rp, i64 0, i64 %indvars.iv
  %11 = load i64, ptr %10, align 8
  store i64 %11, ptr %6, align 8
  store i64 %9, ptr %10, align 8
  tail call void @_Z3dfsi(i32 noundef %7)
  %12 = load i64, ptr %6, align 8
  store i64 %12, ptr @tmp, align 4
  %13 = load i64, ptr %10, align 8
  store i64 %13, ptr %6, align 8
  store i64 %12, ptr %10, align 8
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %14 = and i64 %indvars.iv.next, 4294967295
  %exitcond.not = icmp eq i64 %14, 4
  br i1 %exitcond.not, label %._crit_edge, label %8, !llvm.loop !5
}

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

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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