; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/OvmfPkg/Library (SerializeVariablesToBuffer)

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

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

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

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

declare zeroext i8 @DebugAssertEnabled() #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @SerializeVariablesToBuffer(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %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 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
  %10 = call zeroext i8 @DebugAssertEnabled()
  %11 = zext i8 %10 to i32
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %23

13:                                               ; preds = %3
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = ptrtoint ptr %14 to i64
  %16 = sub i64 %15, 0
  %17 = inttoptr i64 %16 to ptr
  %18 = getelementptr inbounds %struct.SV_INSTANCE, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !9
  %20 = icmp ne i32 %19, 1380013651
  br i1 %20, label %21, label %23

21:                                               ; preds = %13
  call void @DebugAssert(ptr noundef @.str, i64 noundef 850, ptr noundef @.str.1)
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  br label %28

23:                                               ; preds = %13, %3
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = ptrtoint ptr %24 to i64
  %26 = sub i64 %25, 0
  %27 = inttoptr i64 %26 to ptr
  br label %28

28:                                               ; preds = %23, %21
  %29 = phi ptr [ %22, %21 ], [ %27, %23 ]
  store ptr %29, ptr %8, align 8, !tbaa !5
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = icmp eq ptr %30, null
  br i1 %31, label %32, label %33

32:                                               ; preds = %28
  store i64 -9223372036854775806, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %62

33:                                               ; preds = %28
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = load i64, ptr %34, align 8, !tbaa !13
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.SV_INSTANCE, ptr %36, i32 0, i32 3
  %38 = load i64, ptr %37, align 8, !tbaa !14
  %39 = icmp ult i64 %35, %38
  br i1 %39, label %40, label %45

40:                                               ; preds = %33
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.SV_INSTANCE, ptr %41, i32 0, i32 3
  %43 = load i64, ptr %42, align 8, !tbaa !14
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %43, ptr %44, align 8, !tbaa !13
  store i64 -9223372036854775803, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %62

45:                                               ; preds = %33
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = icmp eq ptr %46, null
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  store i64 -9223372036854775806, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %62

49:                                               ; preds = %45
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.SV_INSTANCE, ptr %50, i32 0, i32 3
  %52 = load i64, ptr %51, align 8, !tbaa !14
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %52, ptr %53, align 8, !tbaa !13
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.SV_INSTANCE, ptr %55, i32 0, i32 1
  %57 = load ptr, ptr %56, align 8, !tbaa !15
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.SV_INSTANCE, ptr %58, i32 0, i32 3
  %60 = load i64, ptr %59, align 8, !tbaa !14
  %61 = call ptr @CopyMem(ptr noundef %54, ptr noundef %57, i64 noundef %60)
  store i64 0, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %62

62:                                               ; preds = %49, %48, %40, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %63 = load i64, ptr %4, align 8
  ret i64 %63
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!"", !11, i64 0, !6, i64 8, !12, i64 16, !12, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!10, !12, i64 24}
!15 = !{!10, !6, i64 8}


; 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 = external hidden unnamed_addr constant [163 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [21 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 ptr @CopyMem(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775806, 1) i64 @SerializeVariablesToBuffer(ptr nocapture noundef readonly %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = tail call zeroext i8 @DebugAssertEnabled() #2
  %.not = icmp eq i8 %4, 0
  br i1 %.not, label %8, label %5

5:                                                ; preds = %3
  %6 = load i32, ptr %0, align 8, !tbaa !5
  %.not17 = icmp eq i32 %6, 1380013651
  br i1 %.not17, label %8, label %7

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

8:                                                ; preds = %3, %5, %7
  %9 = icmp eq ptr %2, null
  br i1 %9, label %23, label %10

10:                                               ; preds = %8
  %11 = load i64, ptr %2, align 8, !tbaa !12
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  %13 = load i64, ptr %12, align 8, !tbaa !13
  %14 = icmp ult i64 %11, %13
  br i1 %14, label %15, label %16

15:                                               ; preds = %10
  store i64 %13, ptr %2, align 8, !tbaa !12
  br label %23

16:                                               ; preds = %10
  %17 = icmp eq ptr %1, null
  br i1 %17, label %23, label %18

18:                                               ; preds = %16
  store i64 %13, ptr %2, align 8, !tbaa !12
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = load i64, ptr %12, align 8, !tbaa !13
  %22 = tail call ptr @CopyMem(ptr noundef nonnull %1, ptr noundef %20, i64 noundef %21) #2
  br label %23

23:                                               ; preds = %16, %8, %18, %15
  %.0 = phi i64 [ -9223372036854775803, %15 ], [ 0, %18 ], [ -9223372036854775806, %8 ], [ -9223372036854775806, %16 ]
  ret i64 %.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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !11, i64 16, !11, i64 24}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long long", !8, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!6, !11, i64 24}
!14 = !{!6, !10, i64 8}


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