; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/OptionRomPkg (ReadEdidData)

; 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.CIRRUS_LOGIC_5430_PRIVATE_DATA = type { i64, ptr, ptr, i64, %struct._EFI_UGA_DRAW_PROTOCOL, %struct._EFI_GRAPHICS_OUTPUT_PROTOCOL, %struct.EFI_EDID_DISCOVERED_PROTOCOL, %struct.EFI_EDID_ACTIVE_PROTOCOL, ptr, ptr, i64, i64, [3 x %struct.CIRRUS_LOGIC_5430_MODE_DATA], ptr, i8 }
%struct._EFI_UGA_DRAW_PROTOCOL = type { ptr, ptr, ptr }
%struct._EFI_GRAPHICS_OUTPUT_PROTOCOL = type { ptr, ptr, ptr, ptr }
%struct.EFI_EDID_DISCOVERED_PROTOCOL = type { i32, ptr }
%struct.EFI_EDID_ACTIVE_PROTOCOL = type { i32, ptr }
%struct.CIRRUS_LOGIC_5430_MODE_DATA = type { i32, i32, i32, i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local i64 @ReadEdidData(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca [256 x i8], align 16
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 256, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %13

13:                                               ; preds = %25, %3
  %14 = load i64, ptr %8, align 8, !tbaa !9
  %15 = icmp ult i64 %14, 256
  br i1 %15, label %16, label %28

16:                                               ; preds = %13
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.CIRRUS_LOGIC_5430_PRIVATE_DATA, ptr %17, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %20 = load i64, ptr %8, align 8, !tbaa !9
  %21 = trunc i64 %20 to i8
  %22 = load i64, ptr %8, align 8, !tbaa !9
  %23 = getelementptr inbounds [256 x i8], ptr %9, i64 0, i64 %22
  %24 = call i64 @I2cReadByte(ptr noundef %19, i8 noundef zeroext -96, i8 noundef zeroext %21, ptr noundef %23)
  br label %25

25:                                               ; preds = %16
  %26 = load i64, ptr %8, align 8, !tbaa !9
  %27 = add i64 %26, 1
  store i64 %27, ptr %8, align 8, !tbaa !9
  br label %13, !llvm.loop !17

28:                                               ; preds = %13
  %29 = getelementptr inbounds [256 x i8], ptr %9, i64 0, i64 0
  store ptr %29, ptr %10, align 8, !tbaa !5
  store i64 72057594037927680, ptr %11, align 8, !tbaa !9
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %39, %28
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = icmp ult i64 %31, 256
  br i1 %32, label %33, label %44

33:                                               ; preds = %30
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = call i64 @CompareMem(ptr noundef %34, ptr noundef %11, i64 noundef 8)
  %36 = icmp eq i64 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %33
  br label %44

38:                                               ; preds = %33
  br label %39

39:                                               ; preds = %38
  %40 = load i64, ptr %8, align 8, !tbaa !9
  %41 = add i64 %40, 1
  store i64 %41, ptr %8, align 8, !tbaa !9
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i32 1
  store ptr %43, ptr %10, align 8, !tbaa !5
  br label %30, !llvm.loop !19

44:                                               ; preds = %37, %30
  %45 = load i64, ptr %8, align 8, !tbaa !9
  %46 = icmp eq i64 %45, 256
  br i1 %46, label %47, label %48

47:                                               ; preds = %44
  store i64 -9223372036854775805, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %58

48:                                               ; preds = %44
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = call ptr @AllocateCopyPool(i64 noundef 128, ptr noundef %49)
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %50, ptr %51, align 8, !tbaa !5
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = load ptr, ptr %52, align 8, !tbaa !5
  %54 = icmp eq ptr %53, null
  br i1 %54, label %55, label %56

55:                                               ; preds = %48
  store i64 -9223372036854775799, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %58

56:                                               ; preds = %48
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 128, ptr %57, align 8, !tbaa !9
  store i64 0, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %58

58:                                               ; preds = %56, %55, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 256, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %59 = load i64, ptr %4, align 8
  ret i64 %59
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare i64 @I2cReadByte(ptr noundef, i8 noundef zeroext, i8 noundef zeroext, ptr noundef) #2

declare i64 @CompareMem(ptr noundef, ptr noundef, i64 noundef) #2

declare ptr @AllocateCopyPool(i64 noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 16}
!12 = !{!"", !10, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !13, i64 32, !14, i64 56, !15, i64 88, !15, i64 104, !6, i64 120, !6, i64 128, !10, i64 136, !10, i64 144, !7, i64 152, !6, i64 216, !7, i64 224}
!13 = !{!"_EFI_UGA_DRAW_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16}
!14 = !{!"_EFI_GRAPHICS_OUTPUT_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!15 = !{!"", !16, i64 0, !6, i64 8}
!16 = !{!"int", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


; 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 range(i64 -9223372036854775805, 1) i64 @ReadEdidData(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = alloca [256 x i8], align 16
  %5 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 256, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  br label %7

7:                                                ; preds = %3, %7
  %.01419 = phi i64 [ 0, %3 ], [ %12, %7 ]
  %8 = load ptr, ptr %6, align 8, !tbaa !5
  %9 = trunc nuw i64 %.01419 to i8
  %10 = getelementptr inbounds [256 x i8], ptr %4, i64 0, i64 %.01419
  %11 = call i64 @I2cReadByte(ptr noundef %8, i8 noundef zeroext -96, i8 noundef zeroext %9, ptr noundef nonnull %10) #3
  %12 = add nuw nsw i64 %.01419, 1
  %exitcond.not = icmp eq i64 %12, 256
  br i1 %exitcond.not, label %13, label %7, !llvm.loop !15

13:                                               ; preds = %7
  store i64 72057594037927680, ptr %5, align 8, !tbaa !17
  br label %14

14:                                               ; preds = %13, %17
  %.021.idx = phi i64 [ 0, %13 ], [ %.021.add, %17 ]
  %.021.ptr = getelementptr inbounds i8, ptr %4, i64 %.021.idx
  %15 = call i64 @CompareMem(ptr noundef nonnull %.021.ptr, ptr noundef nonnull %5, i64 noundef 8) #3
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %.thread, label %17

17:                                               ; preds = %14
  %.021.add = add nuw nsw i64 %.021.idx, 1
  %exitcond24.not = icmp eq i64 %.021.idx, 255
  br i1 %exitcond24.not, label %.loopexit, label %14, !llvm.loop !18

.thread:                                          ; preds = %14
  %18 = call ptr @AllocateCopyPool(i64 noundef 128, ptr noundef nonnull %.021.ptr) #3
  store ptr %18, ptr %1, align 8, !tbaa !19
  %19 = icmp eq ptr %18, null
  br i1 %19, label %.loopexit, label %20

20:                                               ; preds = %.thread
  store i64 128, ptr %2, align 8, !tbaa !17
  br label %.loopexit

.loopexit:                                        ; preds = %17, %.thread, %20
  %.015 = phi i64 [ 0, %20 ], [ -9223372036854775799, %.thread ], [ -9223372036854775805, %17 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 256, ptr nonnull %4) #3
  ret i64 %.015
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

declare i64 @I2cReadByte(ptr noundef, i8 noundef zeroext, i8 noundef zeroext, ptr noundef) local_unnamed_addr #2

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

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 16}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !11, i64 32, !12, i64 56, !13, i64 88, !13, i64 104, !10, i64 120, !10, i64 128, !7, i64 136, !7, i64 144, !8, i64 152, !10, i64 216, !8, i64 224}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_EFI_UGA_DRAW_PROTOCOL", !10, i64 0, !10, i64 8, !10, i64 16}
!12 = !{!"_EFI_GRAPHICS_OUTPUT_PROTOCOL", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!"", !14, i64 0, !10, i64 8}
!14 = !{!"int", !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !16}
!19 = !{!10, !10, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass