; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal (DebugPortRead)

; 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.DEBUGPORT_DEVICE = type { i32, ptr, ptr, ptr, %struct._EFI_DEBUGPORT_PROTOCOL, ptr, ptr, i64, i32, i32, i32, i8, i32 }
%struct._EFI_DEBUGPORT_PROTOCOL = type { ptr, ptr, ptr, ptr }
%struct._EFI_SERIAL_IO_PROTOCOL = type { i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@.str.2 = external hidden unnamed_addr constant [149 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i64 @DebugPortRead(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %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 ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %13 = call zeroext i8 @DebugAssertEnabled()
  %14 = zext i8 %13 to i32
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %26

16:                                               ; preds = %4
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = ptrtoint ptr %17 to i64
  %19 = sub i64 %18, ptrtoint (ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr null, i32 0, i32 4) to i64)
  %20 = inttoptr i64 %19 to ptr
  %21 = getelementptr inbounds %struct.DEBUGPORT_DEVICE, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = icmp ne i32 %22, 1346847300
  br i1 %23, label %24, label %26

24:                                               ; preds = %16
  call void @DebugAssert(ptr noundef @.str.2, i64 noundef 548, ptr noundef @.str.4)
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  br label %31

26:                                               ; preds = %16, %4
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = ptrtoint ptr %27 to i64
  %29 = sub i64 %28, ptrtoint (ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr null, i32 0, i32 4) to i64)
  %30 = inttoptr i64 %29 to ptr
  br label %31

31:                                               ; preds = %26, %24
  %32 = phi ptr [ %25, %24 ], [ %30, %26 ]
  store ptr %32, ptr %9, align 8, !tbaa !5
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %33, ptr %12, align 8, !tbaa !5
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = load i64, ptr %34, align 8, !tbaa !15
  store i64 %35, ptr %10, align 8, !tbaa !15
  br label %36

36:                                               ; preds = %80, %31
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.DEBUGPORT_DEVICE, ptr %37, i32 0, i32 6
  %39 = load ptr, ptr %38, align 8, !tbaa !16
  %40 = getelementptr inbounds %struct._EFI_SERIAL_IO_PROTOCOL, ptr %39, i32 0, i32 6
  %41 = load ptr, ptr %40, align 8, !tbaa !17
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.DEBUGPORT_DEVICE, ptr %42, i32 0, i32 6
  %44 = load ptr, ptr %43, align 8, !tbaa !16
  %45 = load ptr, ptr %12, align 8, !tbaa !5
  %46 = call i64 %41(ptr noundef %44, ptr noundef %10, ptr noundef %45)
  store i64 %46, ptr %11, align 8, !tbaa !15
  %47 = load i64, ptr %11, align 8, !tbaa !15
  %48 = icmp eq i64 %47, -9223372036854775790
  br i1 %48, label %49, label %57

49:                                               ; preds = %36
  %50 = load i32, ptr %6, align 4, !tbaa !9
  %51 = icmp ugt i32 %50, 50000
  br i1 %51, label %52, label %55

52:                                               ; preds = %49
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = sub i32 %53, 50000
  store i32 %54, ptr %6, align 4, !tbaa !9
  br label %56

55:                                               ; preds = %49
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %56

56:                                               ; preds = %55, %52
  br label %62

57:                                               ; preds = %36
  %58 = load i64, ptr %11, align 8, !tbaa !15
  %59 = icmp slt i64 %58, 0
  br i1 %59, label %60, label %61

60:                                               ; preds = %57
  br label %82

61:                                               ; preds = %57
  br label %62

62:                                               ; preds = %61, %56
  %63 = load i64, ptr %10, align 8, !tbaa !15
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  %65 = getelementptr inbounds i8, ptr %64, i64 %63
  store ptr %65, ptr %12, align 8, !tbaa !5
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = load i64, ptr %66, align 8, !tbaa !15
  %68 = load ptr, ptr %12, align 8, !tbaa !5
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = ptrtoint ptr %68 to i64
  %71 = ptrtoint ptr %69 to i64
  %72 = sub i64 %70, %71
  %73 = sub i64 %67, %72
  store i64 %73, ptr %10, align 8, !tbaa !15
  br label %74

74:                                               ; preds = %62
  %75 = load i64, ptr %10, align 8, !tbaa !15
  %76 = icmp ne i64 %75, 0
  br i1 %76, label %77, label %80

77:                                               ; preds = %74
  %78 = load i32, ptr %6, align 4, !tbaa !9
  %79 = icmp ugt i32 %78, 0
  br label %80

80:                                               ; preds = %77, %74
  %81 = phi i1 [ false, %74 ], [ %79, %77 ]
  br i1 %81, label %36, label %82, !llvm.loop !19

82:                                               ; preds = %80, %60
  %83 = load ptr, ptr %12, align 8, !tbaa !5
  %84 = load ptr, ptr %8, align 8, !tbaa !5
  %85 = ptrtoint ptr %83 to i64
  %86 = ptrtoint ptr %84 to i64
  %87 = sub i64 %85, %86
  %88 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %87, ptr %88, align 8, !tbaa !15
  %89 = load i64, ptr %11, align 8, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  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 8, ptr %9) #3
  ret i64 %89
}

; 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

declare zeroext i8 @DebugAssertEnabled() #2

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) #2

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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !13, i64 32, !6, i64 64, !6, i64 72, !14, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !7, i64 100, !10, i64 104}
!13 = !{!"_EFI_DEBUGPORT_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!14 = !{!"long long", !7, i64 0}
!15 = !{!14, !14, i64 0}
!16 = !{!12, !6, i64 72}
!17 = !{!18, !6, i64 48}
!18 = !{!"_EFI_SERIAL_IO_PROTOCOL", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!19 = distinct !{!19, !20}
!20 = !{!"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"

@.str.2 = external hidden unnamed_addr constant [149 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i64 @DebugPortRead(ptr noundef %0, i32 noundef %1, ptr nocapture noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  %6 = tail call zeroext i8 @DebugAssertEnabled() #4
  %.not = icmp eq i8 %6, 0
  %.pre = ptrtoint ptr %0 to i64
  %.pre25 = add i64 %.pre, -32
  %.pre27 = inttoptr i64 %.pre25 to ptr
  br i1 %.not, label %._crit_edge24, label %7

7:                                                ; preds = %4
  %8 = load i32, ptr %.pre27, align 8, !tbaa !5
  %.not23 = icmp eq i32 %8, 1346847300
  br i1 %.not23, label %._crit_edge24, label %9

9:                                                ; preds = %7
  tail call void @DebugAssert(ptr noundef nonnull @.str.2, i64 noundef 548, ptr noundef nonnull @.str.4) #4
  br label %._crit_edge24

._crit_edge24:                                    ; preds = %4, %7, %9
  %10 = phi ptr [ %0, %9 ], [ %.pre27, %7 ], [ %.pre27, %4 ]
  %11 = load i64, ptr %2, align 8, !tbaa !13
  store i64 %11, ptr %5, align 8, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %10, i64 72
  %13 = ptrtoint ptr %3 to i64
  br label %14

14:                                               ; preds = %23, %._crit_edge24
  %.020 = phi i32 [ %1, %._crit_edge24 ], [ %.121, %23 ]
  %.0 = phi ptr [ %3, %._crit_edge24 ], [ %25, %23 ]
  %15 = load ptr, ptr %12, align 8, !tbaa !14
  %16 = getelementptr inbounds i8, ptr %15, i64 48
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  %18 = call i64 %17(ptr noundef %15, ptr noundef nonnull %5, ptr noundef %.0) #4
  %19 = icmp eq i64 %18, -9223372036854775790
  br i1 %19, label %20, label %21

20:                                               ; preds = %14
  %spec.select = call i32 @llvm.usub.sat.i32(i32 %.020, i32 50000)
  br label %23

21:                                               ; preds = %14
  %22 = icmp slt i64 %18, 0
  br i1 %22, label %._crit_edge, label %23

._crit_edge:                                      ; preds = %21
  %.pre29 = ptrtoint ptr %.0 to i64
  br label %split

23:                                               ; preds = %20, %21
  %.121 = phi i32 [ %.020, %21 ], [ %spec.select, %20 ]
  %24 = load i64, ptr %5, align 8, !tbaa !13
  %25 = getelementptr inbounds i8, ptr %.0, i64 %24
  %26 = load i64, ptr %2, align 8, !tbaa !13
  %27 = ptrtoint ptr %25 to i64
  %.neg = sub i64 %13, %27
  %28 = add i64 %.neg, %26
  store i64 %28, ptr %5, align 8, !tbaa !13
  %29 = icmp ne i64 %28, 0
  %30 = icmp ne i32 %.121, 0
  %31 = select i1 %29, i1 %30, i1 false
  br i1 %31, label %14, label %split, !llvm.loop !17

split:                                            ; preds = %23, %._crit_edge
  %.pre-phi30 = phi i64 [ %.pre29, %._crit_edge ], [ %27, %23 ]
  %32 = sub i64 %.pre-phi30, %13
  store i64 %32, ptr %2, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  ret i64 %18
}

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

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

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #2

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.usub.sat.i32(i32, i32) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !11, i64 32, !10, i64 64, !10, i64 72, !12, i64 80, !7, i64 88, !7, i64 92, !7, i64 96, !8, i64 100, !7, i64 104}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_EFI_DEBUGPORT_PROTOCOL", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!12 = !{!"long long", !8, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!6, !10, i64 72}
!15 = !{!16, !10, i64 48}
!16 = !{!"_EFI_SERIAL_IO_PROTOCOL", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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