; LLVM IR from lfarcaro/ARINC653_ARMV7A_Z7000 :: ARINC653_ARMV7A_Z7000.sw/ARINC653_ARMV7A_Z7000_bsp/ps7_cortexa9_0/libsrc (ConstructNestedLoop)

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

; 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: nounwind uwtable
declare dso_local i32 @XDmaPs_ConstructSingleLoop(ptr noundef, i32 noundef, ptr noundef, i32 noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMALP(ptr noundef, i32 noundef, i32 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMANOP(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMALD(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMAST(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMALPEND(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @XDmaPs_ConstructNestedLoop(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  %17 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %18, ptr %16, align 8, !tbaa !5
  %19 = load ptr, ptr %16, align 8, !tbaa !5
  %20 = load i32, ptr %10, align 4, !tbaa !9
  %21 = call i32 @XDmaPs_Instr_DMALP(ptr noundef %19, i32 noundef 1, i32 noundef %20)
  %22 = load ptr, ptr %16, align 8, !tbaa !5
  %23 = sext i32 %21 to i64
  %24 = getelementptr inbounds i8, ptr %22, i64 %23
  store ptr %24, ptr %16, align 8, !tbaa !5
  %25 = load ptr, ptr %16, align 8, !tbaa !5
  store ptr %25, ptr %15, align 8, !tbaa !5
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = icmp sgt i32 %26, 0
  br i1 %27, label %28, label %86

28:                                               ; preds = %5
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = icmp slt i32 %29, 8
  br i1 %30, label %31, label %52

31:                                               ; preds = %28
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = load ptr, ptr %16, align 8, !tbaa !5
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = call i32 @XDmaPs_ConstructSingleLoop(ptr noundef %32, i32 noundef %33, ptr noundef %34, i32 noundef %35)
  %37 = load ptr, ptr %16, align 8, !tbaa !5
  %38 = sext i32 %36 to i64
  %39 = getelementptr inbounds i8, ptr %37, i64 %38
  store ptr %39, ptr %16, align 8, !tbaa !5
  %40 = load ptr, ptr %16, align 8, !tbaa !5
  %41 = load ptr, ptr %15, align 8, !tbaa !5
  %42 = call i32 @XDmaPs_Instr_DMALPEND(ptr noundef %40, ptr noundef %41, i32 noundef 1)
  %43 = load ptr, ptr %16, align 8, !tbaa !5
  %44 = sext i32 %42 to i64
  %45 = getelementptr inbounds i8, ptr %43, i64 %44
  store ptr %45, ptr %16, align 8, !tbaa !5
  %46 = load ptr, ptr %16, align 8, !tbaa !5
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = ptrtoint ptr %46 to i64
  %49 = ptrtoint ptr %47 to i64
  %50 = sub i64 %48, %49
  %51 = trunc i64 %50 to i32
  store i32 %51, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %122

52:                                               ; preds = %28
  %53 = load ptr, ptr %16, align 8, !tbaa !5
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = ptrtoint ptr %53 to i64
  %56 = ptrtoint ptr %54 to i64
  %57 = sub i64 %55, %56
  %58 = trunc i64 %57 to i32
  store i32 %58, ptr %12, align 4, !tbaa !9
  %59 = load i32, ptr %12, align 4, !tbaa !9
  %60 = add nsw i32 %59, 7
  store i32 %60, ptr %13, align 4, !tbaa !9
  %61 = load i32, ptr %12, align 4, !tbaa !9
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = sdiv i32 %61, %62
  %64 = load i32, ptr %13, align 4, !tbaa !9
  %65 = load i32, ptr %8, align 4, !tbaa !9
  %66 = sdiv i32 %64, %65
  %67 = icmp ne i32 %63, %66
  br i1 %67, label %68, label %85

68:                                               ; preds = %52
  %69 = load i32, ptr %8, align 4, !tbaa !9
  %70 = load i32, ptr %12, align 4, !tbaa !9
  %71 = load i32, ptr %8, align 4, !tbaa !9
  %72 = srem i32 %70, %71
  %73 = sub nsw i32 %69, %72
  store i32 %73, ptr %14, align 4, !tbaa !9
  br label %74

74:                                               ; preds = %78, %68
  %75 = load i32, ptr %14, align 4, !tbaa !9
  %76 = add nsw i32 %75, -1
  store i32 %76, ptr %14, align 4, !tbaa !9
  %77 = icmp ne i32 %75, 0
  br i1 %77, label %78, label %84

78:                                               ; preds = %74
  %79 = load ptr, ptr %16, align 8, !tbaa !5
  %80 = call i32 @XDmaPs_Instr_DMANOP(ptr noundef %79)
  %81 = load ptr, ptr %16, align 8, !tbaa !5
  %82 = sext i32 %80 to i64
  %83 = getelementptr inbounds i8, ptr %81, i64 %82
  store ptr %83, ptr %16, align 8, !tbaa !5
  br label %74, !llvm.loop !11

84:                                               ; preds = %74
  br label %85

85:                                               ; preds = %84, %52
  br label %86

86:                                               ; preds = %85, %5
  %87 = load ptr, ptr %16, align 8, !tbaa !5
  %88 = load i32, ptr %11, align 4, !tbaa !9
  %89 = call i32 @XDmaPs_Instr_DMALP(ptr noundef %87, i32 noundef 0, i32 noundef %88)
  %90 = load ptr, ptr %16, align 8, !tbaa !5
  %91 = sext i32 %89 to i64
  %92 = getelementptr inbounds i8, ptr %90, i64 %91
  store ptr %92, ptr %16, align 8, !tbaa !5
  %93 = load ptr, ptr %16, align 8, !tbaa !5
  %94 = call i32 @XDmaPs_Instr_DMALD(ptr noundef %93)
  %95 = load ptr, ptr %16, align 8, !tbaa !5
  %96 = sext i32 %94 to i64
  %97 = getelementptr inbounds i8, ptr %95, i64 %96
  store ptr %97, ptr %16, align 8, !tbaa !5
  %98 = load ptr, ptr %16, align 8, !tbaa !5
  %99 = call i32 @XDmaPs_Instr_DMAST(ptr noundef %98)
  %100 = load ptr, ptr %16, align 8, !tbaa !5
  %101 = sext i32 %99 to i64
  %102 = getelementptr inbounds i8, ptr %100, i64 %101
  store ptr %102, ptr %16, align 8, !tbaa !5
  %103 = load ptr, ptr %16, align 8, !tbaa !5
  %104 = load ptr, ptr %16, align 8, !tbaa !5
  %105 = getelementptr inbounds i8, ptr %104, i64 -2
  %106 = call i32 @XDmaPs_Instr_DMALPEND(ptr noundef %103, ptr noundef %105, i32 noundef 0)
  %107 = load ptr, ptr %16, align 8, !tbaa !5
  %108 = sext i32 %106 to i64
  %109 = getelementptr inbounds i8, ptr %107, i64 %108
  store ptr %109, ptr %16, align 8, !tbaa !5
  %110 = load ptr, ptr %16, align 8, !tbaa !5
  %111 = load ptr, ptr %15, align 8, !tbaa !5
  %112 = call i32 @XDmaPs_Instr_DMALPEND(ptr noundef %110, ptr noundef %111, i32 noundef 1)
  %113 = load ptr, ptr %16, align 8, !tbaa !5
  %114 = sext i32 %112 to i64
  %115 = getelementptr inbounds i8, ptr %113, i64 %114
  store ptr %115, ptr %16, align 8, !tbaa !5
  %116 = load ptr, ptr %16, align 8, !tbaa !5
  %117 = load ptr, ptr %9, align 8, !tbaa !5
  %118 = ptrtoint ptr %116 to i64
  %119 = ptrtoint ptr %117 to i64
  %120 = sub i64 %118, %119
  %121 = trunc i64 %120 to i32
  store i32 %121, ptr %6, align 4
  store i32 1, ptr %17, align 4
  br label %122

122:                                              ; preds = %86, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %123 = load i32, ptr %6, align 4
  ret i32 %123
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { inlinehint 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local i32 @XDmaPs_ConstructSingleLoop(ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMALP(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMANOP(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMALD(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMAST(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @XDmaPs_Instr_DMALPEND(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XDmaPs_ConstructNestedLoop(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = tail call i32 @XDmaPs_Instr_DMALP(ptr noundef %2, i32 noundef 1, i32 noundef %3)
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds i8, ptr %2, i64 %7
  %9 = icmp sgt i32 %1, 0
  br i1 %9, label %10, label %.loopexit

10:                                               ; preds = %5
  %11 = icmp ult i32 %1, 8
  br i1 %11, label %12, label %14

12:                                               ; preds = %10
  %13 = tail call i32 @XDmaPs_ConstructSingleLoop(ptr noundef %0, i32 noundef %1, ptr noundef %8, i32 noundef %4)
  br label %39

14:                                               ; preds = %10
  %15 = ptrtoint ptr %8 to i64
  %16 = ptrtoint ptr %0 to i64
  %17 = sub i64 %15, %16
  %18 = trunc i64 %17 to i32
  %19 = add nsw i32 %18, 7
  %20 = sdiv i32 %18, %1
  %21 = srem i32 %18, %1
  %22 = sdiv i32 %19, %1
  %.not = icmp eq i32 %20, %22
  %23 = sub nsw i32 %1, %21
  %.not4849 = icmp eq i32 %23, 0
  %or.cond = select i1 %.not, i1 true, i1 %.not4849
  br i1 %or.cond, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %14, %.lr.ph
  %.051 = phi ptr [ %27, %.lr.ph ], [ %8, %14 ]
  %.04650 = phi i32 [ %24, %.lr.ph ], [ %23, %14 ]
  %24 = add nsw i32 %.04650, -1
  %25 = tail call i32 @XDmaPs_Instr_DMANOP(ptr noundef %.051)
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i8, ptr %.051, i64 %26
  %.not48 = icmp eq i32 %24, 0
  br i1 %.not48, label %.loopexit, label %.lr.ph, !llvm.loop !5

.loopexit:                                        ; preds = %.lr.ph, %14, %5
  %.1 = phi ptr [ %8, %14 ], [ %8, %5 ], [ %27, %.lr.ph ]
  %28 = tail call i32 @XDmaPs_Instr_DMALP(ptr noundef %.1, i32 noundef 0, i32 noundef %4)
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i8, ptr %.1, i64 %29
  %31 = tail call i32 @XDmaPs_Instr_DMALD(ptr noundef %30)
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = tail call i32 @XDmaPs_Instr_DMAST(ptr noundef %33)
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %33, i64 %35
  %37 = getelementptr inbounds i8, ptr %36, i64 -2
  %38 = tail call i32 @XDmaPs_Instr_DMALPEND(ptr noundef %36, ptr noundef nonnull %37, i32 noundef 0)
  br label %39

39:                                               ; preds = %.loopexit, %12
  %.sink = phi i32 [ %38, %.loopexit ], [ %13, %12 ]
  %.sink57 = phi ptr [ %36, %.loopexit ], [ %8, %12 ]
  %40 = sext i32 %.sink to i64
  %41 = getelementptr inbounds i8, ptr %.sink57, i64 %40
  %42 = tail call i32 @XDmaPs_Instr_DMALPEND(ptr noundef %41, ptr noundef %8, i32 noundef 1)
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i8, ptr %41, i64 %43
  %45 = ptrtoint ptr %44 to i64
  %46 = ptrtoint ptr %2 to i64
  %47 = sub i64 %45, %46
  %.045 = trunc i64 %47 to i32
  ret i32 %.045
}

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


; Effective O3 passes: CorrelatedValuePropagationPass,DivRemPairsPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass