; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/NetworkPkg/Mtftp6Dxe (Mtftp6CreateService)

; 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._EFI_SERVICE_BINDING_PROTOCOL = type { ptr, ptr }
%struct._MTFTP6_SERVICE = type { i32, %struct._EFI_SERVICE_BINDING_PROTOCOL, ptr, ptr, i16, %struct._LIST_ENTRY, ptr, ptr }
%struct._LIST_ENTRY = type { ptr, ptr }
%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 }

@gMtftp6ServiceBindingTemplate = external dso_local global %struct._EFI_SERVICE_BINDING_PROTOCOL, align 8
@.str = external hidden unnamed_addr constant [137 x i8], align 1
@gBS = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1

declare zeroext i8 @DebugAssertEnabled() #0

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

declare void @FreePool(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i64 @Mtftp6CreateService(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = 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 8, ptr %9) #3
  br label %11

11:                                               ; preds = %3
  %12 = call zeroext i8 @DebugAssertEnabled()
  %13 = icmp ne i8 %12, 0
  br i1 %13, label %14, label %19

14:                                               ; preds = %11
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  call void @DebugAssert(ptr noundef @.str, i64 noundef 89, ptr noundef @.str.2)
  br label %18

18:                                               ; preds = %17, %14
  br label %19

19:                                               ; preds = %18, %11
  br label %20

20:                                               ; preds = %19
  br label %21

21:                                               ; preds = %20
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr null, ptr %22, align 8, !tbaa !5
  %23 = call ptr @AllocateZeroPool(i64 noundef 80)
  store ptr %23, ptr %8, align 8, !tbaa !5
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = icmp eq ptr %24, null
  br i1 %25, label %26, label %27

26:                                               ; preds = %21
  store i64 -9223372036854775799, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %78

27:                                               ; preds = %21
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %28, i32 0, i32 0
  store i32 1396065869, ptr %29, align 8, !tbaa !9
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %31, i32 0, i32 2
  store ptr %30, ptr %32, align 8, !tbaa !15
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %34, i32 0, i32 3
  store ptr %33, ptr %35, align 8, !tbaa !16
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %36, i32 0, i32 4
  store i16 0, ptr %37, align 8, !tbaa !17
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %38, i32 0, i32 1
  %40 = call ptr @CopyMem(ptr noundef %39, ptr noundef @gMtftp6ServiceBindingTemplate, i64 noundef 16)
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %41, i32 0, i32 5
  %43 = call ptr @InitializeListHead(ptr noundef %42)
  %44 = load ptr, ptr @gBS, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %44, i32 0, i32 8
  %46 = load ptr, ptr %45, align 8, !tbaa !18
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %48, i32 0, i32 6
  %50 = call i64 %46(i32 noundef -2147483136, i64 noundef 8, ptr noundef @Mtftp6OnTimerTick, ptr noundef %47, ptr noundef %49)
  store i64 %50, ptr %9, align 8, !tbaa !22
  %51 = load i64, ptr %9, align 8, !tbaa !22
  %52 = icmp slt i64 %51, 0
  br i1 %52, label %53, label %56

53:                                               ; preds = %27
  %54 = load ptr, ptr %8, align 8, !tbaa !5
  call void @FreePool(ptr noundef %54)
  %55 = load i64, ptr %9, align 8, !tbaa !22
  store i64 %55, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %78

56:                                               ; preds = %27
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = call ptr @UdpIoCreateIo(ptr noundef %57, ptr noundef %58, ptr noundef @Mtftp6ConfigDummyUdpIo, i8 noundef zeroext 6, ptr noundef null)
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %60, i32 0, i32 7
  store ptr %59, ptr %61, align 8, !tbaa !23
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %62, i32 0, i32 7
  %64 = load ptr, ptr %63, align 8, !tbaa !23
  %65 = icmp eq ptr %64, null
  br i1 %65, label %66, label %75

66:                                               ; preds = %56
  %67 = load ptr, ptr @gBS, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %67, i32 0, i32 12
  %69 = load ptr, ptr %68, align 8, !tbaa !24
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct._MTFTP6_SERVICE, ptr %70, i32 0, i32 6
  %72 = load ptr, ptr %71, align 8, !tbaa !25
  %73 = call i64 %69(ptr noundef %72)
  %74 = load ptr, ptr %8, align 8, !tbaa !5
  call void @FreePool(ptr noundef %74)
  store i64 -9223372036854775801, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %78

75:                                               ; preds = %56
  %76 = load ptr, ptr %8, align 8, !tbaa !5
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %76, ptr %77, align 8, !tbaa !5
  store i64 0, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %78

78:                                               ; preds = %75, %66, %53, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %79 = load i64, ptr %4, align 8
  ret i64 %79
}

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

declare ptr @AllocateZeroPool(i64 noundef) #0

declare ptr @CopyMem(ptr noundef, ptr noundef, i64 noundef) #0

declare ptr @InitializeListHead(ptr noundef) #0

declare void @Mtftp6OnTimerTick(ptr noundef, ptr noundef) #0

declare ptr @UdpIoCreateIo(ptr noundef, ptr noundef, ptr noundef, i8 noundef zeroext, ptr noundef) #0

declare i64 @Mtftp6ConfigDummyUdpIo(ptr noundef, ptr noundef) #0

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

attributes #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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !11, i64 0}
!10 = !{!"_MTFTP6_SERVICE", !11, i64 0, !12, i64 8, !6, i64 24, !6, i64 32, !13, i64 40, !14, i64 48, !6, i64 64, !6, i64 72}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_EFI_SERVICE_BINDING_PROTOCOL", !6, i64 0, !6, i64 8}
!13 = !{!"short", !7, i64 0}
!14 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!15 = !{!10, !6, i64 24}
!16 = !{!10, !6, i64 32}
!17 = !{!10, !13, i64 40}
!18 = !{!19, !6, i64 80}
!19 = !{!"", !20, 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}
!20 = !{!"", !21, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20}
!21 = !{!"long long", !7, i64 0}
!22 = !{!21, !21, i64 0}
!23 = !{!10, !6, i64 72}
!24 = !{!19, !6, i64 112}
!25 = !{!10, !6, i64 64}


; 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._EFI_SERVICE_BINDING_PROTOCOL = type { ptr, ptr }

@gMtftp6ServiceBindingTemplate = external dso_local global %struct._EFI_SERVICE_BINDING_PROTOCOL, align 8
@.str = external hidden unnamed_addr constant [137 x i8], align 1
@gBS = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #0

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

declare void @FreePool(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775808, 1) i64 @Mtftp6CreateService(ptr noundef %0, ptr noundef %1, ptr noundef writeonly %2) local_unnamed_addr #1 {
  %4 = tail call zeroext i8 @DebugAssertEnabled() #2
  %5 = icmp eq i8 %4, 0
  %6 = icmp ne ptr %2, null
  %or.cond = or i1 %6, %5
  br i1 %or.cond, label %8, label %7

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

8:                                                ; preds = %7, %3
  store ptr null, ptr %2, align 8, !tbaa !5
  %9 = tail call ptr @AllocateZeroPool(i64 noundef 80) #2
  %10 = icmp eq ptr %9, null
  br i1 %10, label %37, label %11

11:                                               ; preds = %8
  store i32 1396065869, ptr %9, align 8, !tbaa !9
  %12 = getelementptr inbounds i8, ptr %9, i64 24
  store ptr %0, ptr %12, align 8, !tbaa !15
  %13 = getelementptr inbounds i8, ptr %9, i64 32
  store ptr %1, ptr %13, align 8, !tbaa !16
  %14 = getelementptr inbounds i8, ptr %9, i64 40
  store i16 0, ptr %14, align 8, !tbaa !17
  %15 = getelementptr inbounds i8, ptr %9, i64 8
  %16 = tail call ptr @CopyMem(ptr noundef nonnull %15, ptr noundef nonnull @gMtftp6ServiceBindingTemplate, i64 noundef 16) #2
  %17 = getelementptr inbounds i8, ptr %9, i64 48
  %18 = tail call ptr @InitializeListHead(ptr noundef nonnull %17) #2
  %19 = load ptr, ptr @gBS, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 80
  %21 = load ptr, ptr %20, align 8, !tbaa !18
  %22 = getelementptr inbounds i8, ptr %9, i64 64
  %23 = tail call i64 %21(i32 noundef -2147483136, i64 noundef 8, ptr noundef nonnull @Mtftp6OnTimerTick, ptr noundef nonnull %9, ptr noundef nonnull %22) #2
  %24 = icmp slt i64 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %11
  tail call void @FreePool(ptr noundef nonnull %9) #2
  br label %37

26:                                               ; preds = %11
  %27 = tail call ptr @UdpIoCreateIo(ptr noundef %0, ptr noundef %1, ptr noundef nonnull @Mtftp6ConfigDummyUdpIo, i8 noundef zeroext 6, ptr noundef null) #2
  %28 = getelementptr inbounds i8, ptr %9, i64 72
  store ptr %27, ptr %28, align 8, !tbaa !22
  %29 = icmp eq ptr %27, null
  br i1 %29, label %30, label %36

30:                                               ; preds = %26
  %31 = load ptr, ptr @gBS, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 112
  %33 = load ptr, ptr %32, align 8, !tbaa !23
  %34 = load ptr, ptr %22, align 8, !tbaa !24
  %35 = tail call i64 %33(ptr noundef %34) #2
  tail call void @FreePool(ptr noundef nonnull %9) #2
  br label %37

36:                                               ; preds = %26
  store ptr %9, ptr %2, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %8, %36, %30, %25
  %.0 = phi i64 [ %23, %25 ], [ -9223372036854775801, %30 ], [ 0, %36 ], [ -9223372036854775799, %8 ]
  ret i64 %.0
}

declare ptr @AllocateZeroPool(i64 noundef) local_unnamed_addr #0

declare ptr @CopyMem(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

declare ptr @InitializeListHead(ptr noundef) local_unnamed_addr #0

declare void @Mtftp6OnTimerTick(ptr noundef, ptr noundef) #0

declare ptr @UdpIoCreateIo(ptr noundef, ptr noundef, ptr noundef, i8 noundef zeroext, ptr noundef) local_unnamed_addr #0

declare i64 @Mtftp6ConfigDummyUdpIo(ptr noundef, ptr noundef) #0

attributes #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 = { 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 = { 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, !11, i64 0}
!10 = !{!"_MTFTP6_SERVICE", !11, i64 0, !12, i64 8, !6, i64 24, !6, i64 32, !13, i64 40, !14, i64 48, !6, i64 64, !6, i64 72}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_EFI_SERVICE_BINDING_PROTOCOL", !6, i64 0, !6, i64 8}
!13 = !{!"short", !7, i64 0}
!14 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!15 = !{!10, !6, i64 24}
!16 = !{!10, !6, i64 32}
!17 = !{!10, !13, i64 40}
!18 = !{!19, !6, i64 80}
!19 = !{!"", !20, 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}
!20 = !{!"", !21, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20}
!21 = !{!"long long", !7, i64 0}
!22 = !{!10, !6, i64 72}
!23 = !{!19, !6, i64 112}
!24 = !{!10, !6, i64 64}


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