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

; 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.GUID = type { i32, i16, i16, [8 x i8] }
%struct.EFI_BOOT_SERVICES = type { %struct.EFI_TABLE_HEADER, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.EFI_TABLE_HEADER = type { i64, i32, i32, i32, i32 }
%struct._EFI_DRIVER_BINDING_PROTOCOL = type { ptr, ptr, ptr, i32, ptr, ptr }

@mDebugPortDevice = external dso_local global %struct.DEBUGPORT_DEVICE, align 8
@gBS = external global ptr, align 8
@gEfiDebugPortProtocolGuid = external global %struct.GUID, align 4
@gEfiSerialIoProtocolGuid = external global %struct.GUID, align 4
@gEfiDevicePathProtocolGuid = external global %struct.GUID, align 4

; Function Attrs: nounwind uwtable
define dso_local i64 @DebugPortStop(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %12 = load i64, ptr %8, align 8, !tbaa !9
  %13 = icmp eq i64 %12, 0
  br i1 %13, label %14, label %34

14:                                               ; preds = %4
  %15 = load ptr, ptr @gBS, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %15, i32 0, i32 34
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct._EFI_DRIVER_BINDING_PROTOCOL, ptr %19, i32 0, i32 5
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = call i64 %17(ptr noundef %18, ptr noundef @gEfiSerialIoProtocolGuid, ptr noundef %21, ptr noundef %22)
  store ptr null, ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 6), align 8, !tbaa !17
  %24 = load ptr, ptr @gBS, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %24, i32 0, i32 34
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct._EFI_DRIVER_BINDING_PROTOCOL, ptr %28, i32 0, i32 5
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = call i64 %26(ptr noundef %27, ptr noundef @gEfiDevicePathProtocolGuid, ptr noundef %30, ptr noundef %31)
  %33 = load ptr, ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 3), align 8, !tbaa !20
  call void @FreePool(ptr noundef %33)
  store i64 0, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %71

34:                                               ; preds = %4
  %35 = load ptr, ptr @gBS, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %35, i32 0, i32 34
  %37 = load ptr, ptr %36, align 8, !tbaa !11
  %38 = load ptr, ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 5), align 8, !tbaa !21
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct._EFI_DRIVER_BINDING_PROTOCOL, ptr %39, i32 0, i32 5
  %41 = load ptr, ptr %40, align 8, !tbaa !15
  %42 = load ptr, ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 2), align 8, !tbaa !22
  %43 = call i64 %37(ptr noundef %38, ptr noundef @gEfiSerialIoProtocolGuid, ptr noundef %41, ptr noundef %42)
  store i64 %43, ptr %10, align 8, !tbaa !9
  %44 = load i64, ptr %10, align 8, !tbaa !9
  %45 = icmp slt i64 %44, 0
  br i1 %45, label %46, label %48

46:                                               ; preds = %34
  %47 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %47, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %71

48:                                               ; preds = %34
  %49 = load ptr, ptr @gBS, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %49, i32 0, i32 40
  %51 = load ptr, ptr %50, align 8, !tbaa !23
  %52 = load ptr, ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 2), align 8, !tbaa !22
  %53 = load ptr, ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 3), align 8, !tbaa !20
  %54 = call i64 (ptr, ...) %51(ptr noundef %52, ptr noundef @gEfiDevicePathProtocolGuid, ptr noundef %53, ptr noundef @gEfiDebugPortProtocolGuid, ptr noundef getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 4), ptr noundef null)
  store i64 %54, ptr %10, align 8, !tbaa !9
  %55 = load i64, ptr %10, align 8, !tbaa !9
  %56 = icmp slt i64 %55, 0
  br i1 %56, label %57, label %67

57:                                               ; preds = %48
  %58 = load ptr, ptr @gBS, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %58, i32 0, i32 33
  %60 = load ptr, ptr %59, align 8, !tbaa !24
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct._EFI_DRIVER_BINDING_PROTOCOL, ptr %62, i32 0, i32 5
  %64 = load ptr, ptr %63, align 8, !tbaa !15
  %65 = load ptr, ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 2), align 8, !tbaa !22
  %66 = call i64 %60(ptr noundef %61, ptr noundef @gEfiSerialIoProtocolGuid, ptr noundef getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 6), ptr noundef %64, ptr noundef %65, i32 noundef 8)
  br label %68

67:                                               ; preds = %48
  store ptr null, ptr getelementptr inbounds (%struct.DEBUGPORT_DEVICE, ptr @mDebugPortDevice, i32 0, i32 2), align 8, !tbaa !22
  br label %68

68:                                               ; preds = %67, %57
  br label %69

69:                                               ; preds = %68
  %70 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %70, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %71

71:                                               ; preds = %69, %46, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %72 = load i64, ptr %5, align 8
  ret i64 %72
}

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

declare void @FreePool(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 288}
!12 = !{!"", !13, i64 0, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352, !6, i64 360, !6, i64 368}
!13 = !{!"", !10, i64 0, !14, i64 8, !14, i64 12, !14, i64 16, !14, i64 20}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !6, i64 40}
!16 = !{!"_EFI_DRIVER_BINDING_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !14, i64 24, !6, i64 32, !6, i64 40}
!17 = !{!18, !6, i64 72}
!18 = !{!"", !14, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !19, i64 32, !6, i64 64, !6, i64 72, !10, i64 80, !14, i64 88, !14, i64 92, !14, i64 96, !7, i64 100, !14, i64 104}
!19 = !{!"_EFI_DEBUGPORT_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!20 = !{!18, !6, i64 24}
!21 = !{!18, !6, i64 64}
!22 = !{!18, !6, i64 16}
!23 = !{!12, !6, i64 336}
!24 = !{!12, !6, i64 280}


; 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"

%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.GUID = type { i32, i16, i16, [8 x i8] }

@mDebugPortDevice = external dso_local global %struct.DEBUGPORT_DEVICE, align 8
@gBS = external local_unnamed_addr global ptr, align 8
@gEfiDebugPortProtocolGuid = external global %struct.GUID, align 4
@gEfiSerialIoProtocolGuid = external global %struct.GUID, align 4
@gEfiDevicePathProtocolGuid = external global %struct.GUID, align 4

; Function Attrs: nounwind uwtable
define dso_local i64 @DebugPortStop(ptr nocapture noundef readonly %0, ptr noundef %1, i64 noundef %2, ptr nocapture noundef readnone %3) local_unnamed_addr #0 {
  %5 = icmp eq i64 %2, 0
  %6 = load ptr, ptr @gBS, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 288
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  br i1 %5, label %9, label %19

9:                                                ; preds = %4
  %10 = getelementptr inbounds i8, ptr %0, i64 40
  %11 = load ptr, ptr %10, align 8, !tbaa !14
  %12 = tail call i64 %8(ptr noundef %1, ptr noundef nonnull @gEfiSerialIoProtocolGuid, ptr noundef %11, ptr noundef %1) #2
  store ptr null, ptr getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 72), align 8, !tbaa !16
  %13 = load ptr, ptr @gBS, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 288
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = load ptr, ptr %10, align 8, !tbaa !14
  %17 = tail call i64 %15(ptr noundef %1, ptr noundef nonnull @gEfiDevicePathProtocolGuid, ptr noundef %16, ptr noundef %1) #2
  %18 = load ptr, ptr getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 24), align 8, !tbaa !19
  tail call void @FreePool(ptr noundef %18) #2
  br label %42

19:                                               ; preds = %4
  %20 = load ptr, ptr getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 64), align 8, !tbaa !20
  %21 = getelementptr inbounds i8, ptr %0, i64 40
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = load ptr, ptr getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 16), align 8, !tbaa !21
  %24 = tail call i64 %8(ptr noundef %20, ptr noundef nonnull @gEfiSerialIoProtocolGuid, ptr noundef %22, ptr noundef %23) #2
  %25 = icmp slt i64 %24, 0
  br i1 %25, label %42, label %26

26:                                               ; preds = %19
  %27 = load ptr, ptr @gBS, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %27, i64 336
  %29 = load ptr, ptr %28, align 8, !tbaa !22
  %30 = load ptr, ptr getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 16), align 8, !tbaa !21
  %31 = load ptr, ptr getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 24), align 8, !tbaa !19
  %32 = tail call i64 (ptr, ...) %29(ptr noundef %30, ptr noundef nonnull @gEfiDevicePathProtocolGuid, ptr noundef %31, ptr noundef nonnull @gEfiDebugPortProtocolGuid, ptr noundef nonnull getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 32), ptr noundef null) #2
  %33 = icmp slt i64 %32, 0
  br i1 %33, label %34, label %41

34:                                               ; preds = %26
  %35 = load ptr, ptr @gBS, align 8, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %35, i64 280
  %37 = load ptr, ptr %36, align 8, !tbaa !23
  %38 = load ptr, ptr %21, align 8, !tbaa !14
  %39 = load ptr, ptr getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 16), align 8, !tbaa !21
  %40 = tail call i64 %37(ptr noundef %1, ptr noundef nonnull @gEfiSerialIoProtocolGuid, ptr noundef nonnull getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 72), ptr noundef %38, ptr noundef %39, i32 noundef 8) #2
  br label %42

41:                                               ; preds = %26
  store ptr null, ptr getelementptr inbounds (i8, ptr @mDebugPortDevice, i64 16), align 8, !tbaa !21
  br label %42

42:                                               ; preds = %41, %34, %19, %9
  %.0 = phi i64 [ 0, %9 ], [ %24, %19 ], [ %32, %34 ], [ %32, %41 ]
  ret i64 %.0
}

declare void @FreePool(ptr 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 = { "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 = { 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, !6, i64 288}
!10 = !{!"", !11, i64 0, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352, !6, i64 360, !6, i64 368}
!11 = !{!"", !12, i64 0, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !6, i64 40}
!15 = !{!"_EFI_DRIVER_BINDING_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !13, i64 24, !6, i64 32, !6, i64 40}
!16 = !{!17, !6, i64 72}
!17 = !{!"", !13, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !18, i64 32, !6, i64 64, !6, i64 72, !12, i64 80, !13, i64 88, !13, i64 92, !13, i64 96, !7, i64 100, !13, i64 104}
!18 = !{!"_EFI_DEBUGPORT_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!19 = !{!17, !6, i64 24}
!20 = !{!17, !6, i64 64}
!21 = !{!17, !6, i64 16}
!22 = !{!10, !6, i64 336}
!23 = !{!10, !6, i64 280}


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