; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Bus/Ufs (UfsAllocateAlignCommonBuffer)

; 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._UFS_PASS_THRU_PRIVATE_DATA = type { i32, ptr, %struct.EFI_EXT_SCSI_PASS_THRU_MODE, %struct._EFI_EXT_SCSI_PASS_THRU_PROTOCOL, ptr, i64, i32, i8, ptr, i8, ptr, ptr, i8, ptr, %struct.UFS_EXPOSED_LUNS }
%struct.EFI_EXT_SCSI_PASS_THRU_MODE = type { i32, i32, i32 }
%struct._EFI_EXT_SCSI_PASS_THRU_PROTOCOL = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.UFS_EXPOSED_LUNS = type { [12 x i8], i16 }
%struct._EDKII_UFS_HOST_CONTROLLER_PROTOCOL = type { ptr, ptr, ptr, ptr, ptr, ptr }

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

22:                                               ; preds = %5
  store i8 1, ptr %14, align 1, !tbaa !18
  br label %24

23:                                               ; preds = %5
  store i8 0, ptr %14, align 1, !tbaa !18
  br label %24

24:                                               ; preds = %23, %22
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct._UFS_PASS_THRU_PRIVATE_DATA, ptr %25, i32 0, i32 4
  %27 = load ptr, ptr %26, align 8, !tbaa !19
  store ptr %27, ptr %15, align 8, !tbaa !5
  %28 = load ptr, ptr %15, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct._EDKII_UFS_HOST_CONTROLLER_PROTOCOL, ptr %28, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !20
  %31 = load ptr, ptr %15, align 8, !tbaa !5
  %32 = load i64, ptr %8, align 8, !tbaa !9
  %33 = lshr i64 %32, 12
  %34 = load i64, ptr %8, align 8, !tbaa !9
  %35 = and i64 %34, 4095
  %36 = icmp ne i64 %35, 0
  %37 = zext i1 %36 to i64
  %38 = select i1 %36, i32 1, i32 0
  %39 = sext i32 %38 to i64
  %40 = add i64 %33, %39
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = call i64 %30(ptr noundef %31, i32 noundef 0, i32 noundef 4, i64 noundef %40, ptr noundef %41, i64 noundef 0)
  store i64 %42, ptr %12, align 8, !tbaa !9
  %43 = load i64, ptr %12, align 8, !tbaa !9
  %44 = icmp slt i64 %43, 0
  br i1 %44, label %45, label %49

45:                                               ; preds = %24
  %46 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr null, ptr %46, align 8, !tbaa !5
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr null, ptr %47, align 8, !tbaa !5
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  store i64 0, ptr %48, align 8, !tbaa !9
  store i64 -9223372036854775799, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %152

49:                                               ; preds = %24
  %50 = load i64, ptr %8, align 8, !tbaa !9
  %51 = lshr i64 %50, 12
  %52 = load i64, ptr %8, align 8, !tbaa !9
  %53 = and i64 %52, 4095
  %54 = icmp ne i64 %53, 0
  %55 = zext i1 %54 to i64
  %56 = select i1 %54, i32 1, i32 0
  %57 = sext i32 %56 to i64
  %58 = add i64 %51, %57
  %59 = shl i64 %58, 12
  store i64 %59, ptr %13, align 8, !tbaa !9
  %60 = load ptr, ptr %15, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct._EDKII_UFS_HOST_CONTROLLER_PROTOCOL, ptr %60, i32 0, i32 3
  %62 = load ptr, ptr %61, align 8, !tbaa !22
  %63 = load ptr, ptr %15, align 8, !tbaa !5
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = load ptr, ptr %64, align 8, !tbaa !5
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = load ptr, ptr %11, align 8, !tbaa !5
  %68 = call i64 %62(ptr noundef %63, i32 noundef 2, ptr noundef %65, ptr noundef %13, ptr noundef %66, ptr noundef %67)
  store i64 %68, ptr %12, align 8, !tbaa !9
  %69 = load i64, ptr %12, align 8, !tbaa !9
  %70 = icmp slt i64 %69, 0
  br i1 %70, label %84, label %71

71:                                               ; preds = %49
  %72 = load i64, ptr %13, align 8, !tbaa !9
  %73 = load i64, ptr %8, align 8, !tbaa !9
  %74 = lshr i64 %73, 12
  %75 = load i64, ptr %8, align 8, !tbaa !9
  %76 = and i64 %75, 4095
  %77 = icmp ne i64 %76, 0
  %78 = zext i1 %77 to i64
  %79 = select i1 %77, i32 1, i32 0
  %80 = sext i32 %79 to i64
  %81 = add i64 %74, %80
  %82 = shl i64 %81, 12
  %83 = icmp ne i64 %72, %82
  br i1 %83, label %84, label %103

84:                                               ; preds = %71, %49
  %85 = load ptr, ptr %15, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct._EDKII_UFS_HOST_CONTROLLER_PROTOCOL, ptr %85, i32 0, i32 2
  %87 = load ptr, ptr %86, align 8, !tbaa !23
  %88 = load ptr, ptr %15, align 8, !tbaa !5
  %89 = load i64, ptr %8, align 8, !tbaa !9
  %90 = lshr i64 %89, 12
  %91 = load i64, ptr %8, align 8, !tbaa !9
  %92 = and i64 %91, 4095
  %93 = icmp ne i64 %92, 0
  %94 = zext i1 %93 to i64
  %95 = select i1 %93, i32 1, i32 0
  %96 = sext i32 %95 to i64
  %97 = add i64 %90, %96
  %98 = shl i64 %97, 12
  %99 = load ptr, ptr %9, align 8, !tbaa !5
  %100 = load ptr, ptr %99, align 8, !tbaa !5
  %101 = call i64 %87(ptr noundef %88, i64 noundef %98, ptr noundef %100)
  %102 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr null, ptr %102, align 8, !tbaa !5
  store i64 -9223372036854775799, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %152

103:                                              ; preds = %71
  %104 = load i8, ptr %14, align 1, !tbaa !18
  %105 = zext i8 %104 to i32
  %106 = icmp ne i32 %105, 0
  br i1 %106, label %107, label %138

107:                                              ; preds = %103
  %108 = load ptr, ptr %10, align 8, !tbaa !5
  %109 = load i64, ptr %108, align 8, !tbaa !9
  %110 = icmp ugt i64 %109, 4294967296
  br i1 %110, label %111, label %138

111:                                              ; preds = %107
  %112 = load ptr, ptr %15, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct._EDKII_UFS_HOST_CONTROLLER_PROTOCOL, ptr %112, i32 0, i32 4
  %114 = load ptr, ptr %113, align 8, !tbaa !24
  %115 = load ptr, ptr %15, align 8, !tbaa !5
  %116 = load ptr, ptr %11, align 8, !tbaa !5
  %117 = load ptr, ptr %116, align 8, !tbaa !5
  %118 = call i64 %114(ptr noundef %115, ptr noundef %117)
  %119 = load ptr, ptr %15, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct._EDKII_UFS_HOST_CONTROLLER_PROTOCOL, ptr %119, i32 0, i32 2
  %121 = load ptr, ptr %120, align 8, !tbaa !23
  %122 = load ptr, ptr %15, align 8, !tbaa !5
  %123 = load i64, ptr %8, align 8, !tbaa !9
  %124 = lshr i64 %123, 12
  %125 = load i64, ptr %8, align 8, !tbaa !9
  %126 = and i64 %125, 4095
  %127 = icmp ne i64 %126, 0
  %128 = zext i1 %127 to i64
  %129 = select i1 %127, i32 1, i32 0
  %130 = sext i32 %129 to i64
  %131 = add i64 %124, %130
  %132 = shl i64 %131, 12
  %133 = load ptr, ptr %9, align 8, !tbaa !5
  %134 = load ptr, ptr %133, align 8, !tbaa !5
  %135 = call i64 %121(ptr noundef %122, i64 noundef %132, ptr noundef %134)
  %136 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr null, ptr %136, align 8, !tbaa !5
  %137 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr null, ptr %137, align 8, !tbaa !5
  store i64 -9223372036854775801, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %152

138:                                              ; preds = %107, %103
  %139 = load ptr, ptr %9, align 8, !tbaa !5
  %140 = load ptr, ptr %139, align 8, !tbaa !5
  %141 = load i64, ptr %8, align 8, !tbaa !9
  %142 = lshr i64 %141, 12
  %143 = load i64, ptr %8, align 8, !tbaa !9
  %144 = and i64 %143, 4095
  %145 = icmp ne i64 %144, 0
  %146 = zext i1 %145 to i64
  %147 = select i1 %145, i32 1, i32 0
  %148 = sext i32 %147 to i64
  %149 = add i64 %142, %148
  %150 = shl i64 %149, 12
  %151 = call ptr @ZeroMem(ptr noundef %140, i64 noundef %150)
  store i64 0, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %152

152:                                              ; preds = %138, %111, %84, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %153 = load i64, ptr %6, align 8
  ret i64 %153
}

declare ptr @ZeroMem(ptr noundef, i64 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 = { "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 = !{!"long long", !7, i64 0}
!11 = !{!12, !13, i64 112}
!12 = !{!"_UFS_PASS_THRU_PRIVATE_DATA", !13, i64 0, !6, i64 8, !14, i64 16, !15, i64 32, !6, i64 96, !10, i64 104, !13, i64 112, !7, i64 116, !6, i64 120, !7, i64 128, !6, i64 136, !6, i64 144, !7, i64 152, !6, i64 160, !16, i64 168}
!13 = !{!"int", !7, i64 0}
!14 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8}
!15 = !{!"_EFI_EXT_SCSI_PASS_THRU_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!16 = !{!"", !7, i64 0, !17, i64 12, !17, i64 13}
!17 = !{!"short", !7, i64 0}
!18 = !{!7, !7, i64 0}
!19 = !{!12, !6, i64 96}
!20 = !{!21, !6, i64 8}
!21 = !{!"_EDKII_UFS_HOST_CONTROLLER_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!22 = !{!21, !6, i64 24}
!23 = !{!21, !6, i64 16}
!24 = !{!21, !6, i64 32}


; 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: 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: nounwind uwtable
define dso_local range(i64 -9223372036854775801, 1) i64 @UfsAllocateAlignCommonBuffer(ptr nocapture noundef readonly %0, i64 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) local_unnamed_addr #1 {
  %6 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #3
  %7 = getelementptr inbounds i8, ptr %0, i64 112
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %9 = and i32 %8, 16777216
  %.not = icmp eq i32 %9, 0
  %10 = getelementptr inbounds i8, ptr %0, i64 96
  %11 = load ptr, ptr %10, align 8, !tbaa !16
  %12 = getelementptr inbounds i8, ptr %11, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !17
  %14 = lshr i64 %1, 12
  %15 = and i64 %1, 4095
  %.not44 = icmp ne i64 %15, 0
  %16 = zext i1 %.not44 to i64
  %17 = add nuw nsw i64 %14, %16
  %18 = tail call i64 %13(ptr noundef %11, i32 noundef 0, i32 noundef 4, i64 noundef %17, ptr noundef %2, i64 noundef 0) #3
  %19 = icmp slt i64 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %5
  store ptr null, ptr %4, align 8, !tbaa !19
  store ptr null, ptr %2, align 8, !tbaa !19
  store i64 0, ptr %3, align 8, !tbaa !20
  br label %50

21:                                               ; preds = %5
  %22 = shl i64 %17, 12
  store i64 %22, ptr %6, align 8, !tbaa !20
  %23 = getelementptr inbounds i8, ptr %11, i64 24
  %24 = load ptr, ptr %23, align 8, !tbaa !21
  %25 = load ptr, ptr %2, align 8, !tbaa !19
  %26 = call i64 %24(ptr noundef nonnull %11, i32 noundef 2, ptr noundef %25, ptr noundef nonnull %6, ptr noundef %3, ptr noundef %4) #3
  %27 = icmp sgt i64 %26, -1
  %28 = load i64, ptr %6, align 8
  %.not45 = icmp eq i64 %28, %22
  %or.cond = select i1 %27, i1 %.not45, i1 false
  br i1 %or.cond, label %34, label %29

29:                                               ; preds = %21
  %30 = getelementptr inbounds i8, ptr %11, i64 16
  %31 = load ptr, ptr %30, align 8, !tbaa !22
  %32 = load ptr, ptr %2, align 8, !tbaa !19
  %33 = call i64 %31(ptr noundef nonnull %11, i64 noundef %22, ptr noundef %32) #3
  store ptr null, ptr %2, align 8, !tbaa !19
  br label %50

34:                                               ; preds = %21
  br i1 %.not, label %47, label %35

35:                                               ; preds = %34
  %36 = load i64, ptr %3, align 8, !tbaa !20
  %37 = icmp ugt i64 %36, 4294967296
  br i1 %37, label %38, label %47

38:                                               ; preds = %35
  %39 = getelementptr inbounds i8, ptr %11, i64 32
  %40 = load ptr, ptr %39, align 8, !tbaa !23
  %41 = load ptr, ptr %4, align 8, !tbaa !19
  %42 = call i64 %40(ptr noundef nonnull %11, ptr noundef %41) #3
  %43 = getelementptr inbounds i8, ptr %11, i64 16
  %44 = load ptr, ptr %43, align 8, !tbaa !22
  %45 = load ptr, ptr %2, align 8, !tbaa !19
  %46 = call i64 %44(ptr noundef nonnull %11, i64 noundef %22, ptr noundef %45) #3
  store ptr null, ptr %4, align 8, !tbaa !19
  store ptr null, ptr %2, align 8, !tbaa !19
  br label %50

47:                                               ; preds = %35, %34
  %48 = load ptr, ptr %2, align 8, !tbaa !19
  %49 = call ptr @ZeroMem(ptr noundef %48, i64 noundef %22) #3
  br label %50

50:                                               ; preds = %47, %38, %29, %20
  %.0 = phi i64 [ -9223372036854775799, %20 ], [ -9223372036854775799, %29 ], [ -9223372036854775801, %38 ], [ 0, %47 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #3
  ret i64 %.0
}

declare ptr @ZeroMem(ptr noundef, i64 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { "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, !7, i64 112}
!6 = !{!"_UFS_PASS_THRU_PRIVATE_DATA", !7, i64 0, !10, i64 8, !11, i64 16, !12, i64 32, !10, i64 96, !13, i64 104, !7, i64 112, !8, i64 116, !10, i64 120, !8, i64 128, !10, i64 136, !10, i64 144, !8, i64 152, !10, i64 160, !14, i64 168}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8}
!12 = !{!"_EFI_EXT_SCSI_PASS_THRU_PROTOCOL", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56}
!13 = !{!"long long", !8, i64 0}
!14 = !{!"", !8, i64 0, !15, i64 12, !15, i64 13}
!15 = !{!"short", !8, i64 0}
!16 = !{!6, !10, i64 96}
!17 = !{!18, !10, i64 8}
!18 = !{!"_EDKII_UFS_HOST_CONTROLLER_PROTOCOL", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!19 = !{!10, !10, i64 0}
!20 = !{!13, !13, i64 0}
!21 = !{!18, !10, i64 24}
!22 = !{!18, !10, i64 16}
!23 = !{!18, !10, i64 32}


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