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

; 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 @DebugPortWrite(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 i64, align 8
  %13 = alloca i32, align 4
  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
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i64 0, ptr %12, align 8, !tbaa !11
  %14 = call zeroext i8 @DebugAssertEnabled()
  %15 = zext i8 %14 to i32
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %27

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

25:                                               ; preds = %17
  call void @DebugAssert(ptr noundef @.str.2, i64 noundef 608, ptr noundef @.str.4)
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  br label %32

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

32:                                               ; preds = %27, %25
  %33 = phi ptr [ %26, %25 ], [ %31, %27 ]
  store ptr %33, ptr %9, align 8, !tbaa !5
  store i64 8, ptr %11, align 8, !tbaa !11
  store i64 0, ptr %10, align 8, !tbaa !11
  br label %34

34:                                               ; preds = %78, %32
  %35 = load i64, ptr %10, align 8, !tbaa !11
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load i64, ptr %36, align 8, !tbaa !11
  %38 = icmp ult i64 %35, %37
  br i1 %38, label %39, label %43

39:                                               ; preds = %34
  %40 = load i64, ptr %12, align 8, !tbaa !11
  %41 = icmp slt i64 %40, 0
  %42 = xor i1 %41, true
  br label %43

43:                                               ; preds = %39, %34
  %44 = phi i1 [ false, %34 ], [ %42, %39 ]
  br i1 %44, label %45, label %82

45:                                               ; preds = %43
  %46 = load ptr, ptr %9, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.DEBUGPORT_DEVICE, ptr %46, i32 0, i32 6
  %48 = load ptr, ptr %47, align 8, !tbaa !16
  %49 = getelementptr inbounds %struct._EFI_SERIAL_IO_PROTOCOL, ptr %48, i32 0, i32 4
  %50 = load ptr, ptr %49, align 8, !tbaa !17
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.DEBUGPORT_DEVICE, ptr %51, i32 0, i32 6
  %53 = load ptr, ptr %52, align 8, !tbaa !16
  %54 = call i64 %50(ptr noundef %53, ptr noundef %13)
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = load i64, ptr %55, align 8, !tbaa !11
  %57 = load i64, ptr %10, align 8, !tbaa !11
  %58 = sub i64 %56, %57
  %59 = icmp ult i64 %58, 8
  br i1 %59, label %60, label %65

60:                                               ; preds = %45
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = load i64, ptr %61, align 8, !tbaa !11
  %63 = load i64, ptr %10, align 8, !tbaa !11
  %64 = sub i64 %62, %63
  store i64 %64, ptr %11, align 8, !tbaa !11
  br label %65

65:                                               ; preds = %60, %45
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.DEBUGPORT_DEVICE, ptr %66, i32 0, i32 6
  %68 = load ptr, ptr %67, align 8, !tbaa !16
  %69 = getelementptr inbounds %struct._EFI_SERIAL_IO_PROTOCOL, ptr %68, i32 0, i32 5
  %70 = load ptr, ptr %69, align 8, !tbaa !19
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.DEBUGPORT_DEVICE, ptr %71, i32 0, i32 6
  %73 = load ptr, ptr %72, align 8, !tbaa !16
  %74 = load ptr, ptr %8, align 8, !tbaa !5
  %75 = load i64, ptr %10, align 8, !tbaa !11
  %76 = getelementptr inbounds i8, ptr %74, i64 %75
  %77 = call i64 %70(ptr noundef %73, ptr noundef %11, ptr noundef %76)
  store i64 %77, ptr %12, align 8, !tbaa !11
  br label %78

78:                                               ; preds = %65
  %79 = load i64, ptr %11, align 8, !tbaa !11
  %80 = load i64, ptr %10, align 8, !tbaa !11
  %81 = add i64 %80, %79
  store i64 %81, ptr %10, align 8, !tbaa !11
  br label %34, !llvm.loop !20

82:                                               ; preds = %43
  %83 = load i64, ptr %10, align 8, !tbaa !11
  %84 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %83, ptr %84, align 8, !tbaa !11
  %85 = load i64, ptr %12, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  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 %85
}

; 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, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !15, i64 32, !6, i64 64, !6, i64 72, !12, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !7, i64 100, !10, i64 104}
!15 = !{!"_EFI_DEBUGPORT_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!16 = !{!14, !6, i64 72}
!17 = !{!18, !6, i64 32}
!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 = !{!18, !6, i64 40}
!20 = distinct !{!20, !21}
!21 = !{!"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 @DebugPortWrite(ptr noundef %0, i32 noundef %1, ptr nocapture noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #3
  %7 = tail call zeroext i8 @DebugAssertEnabled() #3
  %.not = icmp eq i8 %7, 0
  %.pre = ptrtoint ptr %0 to i64
  %.pre27 = add i64 %.pre, -32
  %.pre29 = inttoptr i64 %.pre27 to ptr
  br i1 %.not, label %._crit_edge26, label %8

8:                                                ; preds = %4
  %9 = load i32, ptr %.pre29, align 8, !tbaa !5
  %.not21 = icmp eq i32 %9, 1346847300
  br i1 %.not21, label %._crit_edge26, label %10

10:                                               ; preds = %8
  tail call void @DebugAssert(ptr noundef nonnull @.str.2, i64 noundef 608, ptr noundef nonnull @.str.4) #3
  br label %._crit_edge26

._crit_edge26:                                    ; preds = %4, %8, %10
  %11 = phi ptr [ %0, %10 ], [ %.pre29, %8 ], [ %.pre29, %4 ]
  store i64 8, ptr %5, align 8, !tbaa !13
  %12 = load i64, ptr %2, align 8, !tbaa !13
  %.not24 = icmp eq i64 %12, 0
  br i1 %.not24, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %._crit_edge26
  %13 = getelementptr inbounds i8, ptr %11, i64 72
  br label %14

14:                                               ; preds = %.lr.ph, %23
  %.01922 = phi i64 [ 0, %.lr.ph ], [ %30, %23 ]
  %15 = load ptr, ptr %13, align 8, !tbaa !14
  %16 = getelementptr inbounds i8, ptr %15, i64 32
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  %18 = call i64 %17(ptr noundef %15, ptr noundef nonnull %6) #3
  %19 = load i64, ptr %2, align 8, !tbaa !13
  %20 = sub i64 %19, %.01922
  %21 = icmp ult i64 %20, 8
  br i1 %21, label %22, label %23

22:                                               ; preds = %14
  store i64 %20, ptr %5, align 8, !tbaa !13
  br label %23

23:                                               ; preds = %22, %14
  %24 = load ptr, ptr %13, align 8, !tbaa !14
  %25 = getelementptr inbounds i8, ptr %24, i64 40
  %26 = load ptr, ptr %25, align 8, !tbaa !17
  %27 = getelementptr inbounds i8, ptr %3, i64 %.01922
  %28 = call i64 %26(ptr noundef %24, ptr noundef nonnull %5, ptr noundef %27) #3
  %29 = load i64, ptr %5, align 8, !tbaa !13
  %30 = add i64 %29, %.01922
  %31 = load i64, ptr %2, align 8, !tbaa !13
  %32 = icmp ult i64 %30, %31
  %33 = icmp sgt i64 %28, -1
  %34 = select i1 %32, i1 %33, i1 false
  br i1 %34, label %14, label %._crit_edge, !llvm.loop !18

._crit_edge:                                      ; preds = %23, %._crit_edge26
  %.019.lcssa = phi i64 [ 0, %._crit_edge26 ], [ %30, %23 ]
  %.0.lcssa = phi i64 [ 0, %._crit_edge26 ], [ %28, %23 ]
  store i64 %.019.lcssa, ptr %2, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  ret i64 %.0.lcssa
}

; 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

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, !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 32}
!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 = !{!16, !10, i64 40}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


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