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

; 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
define dso_local i32 @XDmaPs_ConstructSingleLoop(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %13, ptr %12, align 8, !tbaa !5
  %14 = load ptr, ptr %12, align 8, !tbaa !5
  %15 = load i32, ptr %8, align 4, !tbaa !9
  %16 = call i32 @XDmaPs_Instr_DMALP(ptr noundef %14, i32 noundef 0, i32 noundef %15)
  %17 = load ptr, ptr %12, align 8, !tbaa !5
  %18 = sext i32 %16 to i64
  %19 = getelementptr inbounds i8, ptr %17, i64 %18
  store ptr %19, ptr %12, align 8, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %22, label %56

22:                                               ; preds = %4
  %23 = load ptr, ptr %12, align 8, !tbaa !5
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = ptrtoint ptr %23 to i64
  %26 = ptrtoint ptr %24 to i64
  %27 = sub i64 %25, %26
  %28 = trunc i64 %27 to i32
  store i32 %28, ptr %9, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = add nsw i32 %29, 3
  store i32 %30, ptr %10, align 4, !tbaa !9
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = sdiv i32 %31, %32
  %34 = load i32, ptr %10, align 4, !tbaa !9
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = sdiv i32 %34, %35
  %37 = icmp ne i32 %33, %36
  br i1 %37, label %38, label %55

38:                                               ; preds = %22
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = srem i32 %40, %41
  %43 = sub nsw i32 %39, %42
  store i32 %43, ptr %11, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %48, %38
  %45 = load i32, ptr %11, align 4, !tbaa !9
  %46 = add nsw i32 %45, -1
  store i32 %46, ptr %11, align 4, !tbaa !9
  %47 = icmp ne i32 %45, 0
  br i1 %47, label %48, label %54

48:                                               ; preds = %44
  %49 = load ptr, ptr %12, align 8, !tbaa !5
  %50 = call i32 @XDmaPs_Instr_DMANOP(ptr noundef %49)
  %51 = load ptr, ptr %12, align 8, !tbaa !5
  %52 = sext i32 %50 to i64
  %53 = getelementptr inbounds i8, ptr %51, i64 %52
  store ptr %53, ptr %12, align 8, !tbaa !5
  br label %44, !llvm.loop !11

54:                                               ; preds = %44
  br label %55

55:                                               ; preds = %54, %22
  br label %56

56:                                               ; preds = %55, %4
  %57 = load ptr, ptr %12, align 8, !tbaa !5
  %58 = call i32 @XDmaPs_Instr_DMALD(ptr noundef %57)
  %59 = load ptr, ptr %12, align 8, !tbaa !5
  %60 = sext i32 %58 to i64
  %61 = getelementptr inbounds i8, ptr %59, i64 %60
  store ptr %61, ptr %12, align 8, !tbaa !5
  %62 = load ptr, ptr %12, align 8, !tbaa !5
  %63 = call i32 @XDmaPs_Instr_DMAST(ptr noundef %62)
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  %65 = sext i32 %63 to i64
  %66 = getelementptr inbounds i8, ptr %64, i64 %65
  store ptr %66, ptr %12, align 8, !tbaa !5
  %67 = load ptr, ptr %12, align 8, !tbaa !5
  %68 = load ptr, ptr %12, align 8, !tbaa !5
  %69 = getelementptr inbounds i8, ptr %68, i64 -2
  %70 = call i32 @XDmaPs_Instr_DMALPEND(ptr noundef %67, ptr noundef %69, i32 noundef 0)
  %71 = load ptr, ptr %12, align 8, !tbaa !5
  %72 = sext i32 %70 to i64
  %73 = getelementptr inbounds i8, ptr %71, i64 %72
  store ptr %73, ptr %12, align 8, !tbaa !5
  %74 = load ptr, ptr %12, align 8, !tbaa !5
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  %76 = ptrtoint ptr %74 to i64
  %77 = ptrtoint ptr %75 to i64
  %78 = sub i64 %76, %77
  %79 = trunc i64 %78 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret i32 %79
}

; 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

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
define dso_local i32 @XDmaPs_ConstructSingleLoop(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = tail call i32 @XDmaPs_Instr_DMALP(ptr noundef %2, i32 noundef 0, i32 noundef %3)
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds i8, ptr %2, i64 %6
  %8 = icmp sgt i32 %1, 0
  br i1 %8, label %9, label %.loopexit

9:                                                ; preds = %4
  %10 = ptrtoint ptr %7 to i64
  %11 = ptrtoint ptr %0 to i64
  %12 = sub i64 %10, %11
  %13 = trunc i64 %12 to i32
  %14 = add nsw i32 %13, 3
  %15 = sdiv i32 %13, %1
  %16 = srem i32 %13, %1
  %17 = sdiv i32 %14, %1
  %.not = icmp eq i32 %15, %17
  %18 = sub nsw i32 %1, %16
  %.not2829 = icmp eq i32 %18, 0
  %or.cond = select i1 %.not, i1 true, i1 %.not2829
  br i1 %or.cond, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %9, %.lr.ph
  %.031 = phi ptr [ %22, %.lr.ph ], [ %7, %9 ]
  %.02630 = phi i32 [ %19, %.lr.ph ], [ %18, %9 ]
  %19 = add nsw i32 %.02630, -1
  %20 = tail call i32 @XDmaPs_Instr_DMANOP(ptr noundef %.031)
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %.031, i64 %21
  %.not28 = icmp eq i32 %19, 0
  br i1 %.not28, label %.loopexit, label %.lr.ph, !llvm.loop !5

.loopexit:                                        ; preds = %.lr.ph, %9, %4
  %.1 = phi ptr [ %7, %9 ], [ %7, %4 ], [ %22, %.lr.ph ]
  %23 = tail call i32 @XDmaPs_Instr_DMALD(ptr noundef %.1)
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %.1, i64 %24
  %26 = tail call i32 @XDmaPs_Instr_DMAST(ptr noundef %25)
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %25, i64 %27
  %29 = getelementptr inbounds i8, ptr %28, i64 -2
  %30 = tail call i32 @XDmaPs_Instr_DMALPEND(ptr noundef %28, ptr noundef nonnull %29, i32 noundef 0)
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i8, ptr %28, i64 %31
  %33 = ptrtoint ptr %32 to i64
  %34 = ptrtoint ptr %2 to i64
  %35 = sub i64 %33, %34
  %36 = trunc i64 %35 to i32
  ret i32 %36
}

; 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

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: DivRemPairsPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass