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

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

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

declare zeroext i16 @SwapBytes16(i16 noundef zeroext) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @PxeBcExtractBootFileParam(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = call ptr @CopyMem(ptr noundef %6, ptr noundef %11, i64 noundef 2)
  %13 = load i16, ptr %6, align 2, !tbaa !9
  %14 = call zeroext i16 @SwapBytes16(i16 noundef zeroext %13)
  store i16 %14, ptr %6, align 2, !tbaa !9
  %15 = load i16, ptr %6, align 2, !tbaa !9
  %16 = zext i16 %15 to i32
  %17 = icmp slt i32 %16, 1
  br i1 %17, label %22, label %18

18:                                               ; preds = %2
  %19 = load i16, ptr %6, align 2, !tbaa !9
  %20 = zext i16 %19 to i32
  %21 = icmp sgt i32 %20, 5
  br i1 %21, label %22, label %23

22:                                               ; preds = %18, %2
  store i64 -9223372036854775794, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %62

23:                                               ; preds = %18
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i64 2
  store ptr %25, ptr %4, align 8, !tbaa !5
  store i32 0, ptr %9, align 4, !tbaa !11
  store i8 0, ptr %7, align 1, !tbaa !13
  br label %26

26:                                               ; preds = %49, %23
  %27 = load i8, ptr %7, align 1, !tbaa !13
  %28 = zext i8 %27 to i32
  %29 = load i16, ptr %6, align 2, !tbaa !9
  %30 = zext i16 %29 to i32
  %31 = icmp slt i32 %28, %30
  br i1 %31, label %32, label %52

32:                                               ; preds = %26
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load i8, ptr %7, align 1, !tbaa !13
  %35 = zext i8 %34 to i32
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i8, ptr %33, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !13
  %39 = sext i8 %38 to i16
  %40 = call i64 @PxeBcUniHexToUint8(ptr noundef %8, i16 noundef zeroext %39)
  %41 = icmp slt i64 %40, 0
  br i1 %41, label %42, label %43

42:                                               ; preds = %32
  store i64 -9223372036854775794, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %62

43:                                               ; preds = %32
  %44 = load i32, ptr %9, align 4, !tbaa !11
  %45 = load i8, ptr %8, align 1, !tbaa !13
  %46 = zext i8 %45 to i32
  %47 = add i32 %44, %46
  %48 = mul i32 %47, 10
  store i32 %48, ptr %9, align 4, !tbaa !11
  br label %49

49:                                               ; preds = %43
  %50 = load i8, ptr %7, align 1, !tbaa !13
  %51 = add i8 %50, 1
  store i8 %51, ptr %7, align 1, !tbaa !13
  br label %26, !llvm.loop !14

52:                                               ; preds = %26
  %53 = load i32, ptr %9, align 4, !tbaa !11
  %54 = udiv i32 %53, 10
  store i32 %54, ptr %9, align 4, !tbaa !11
  %55 = load i32, ptr %9, align 4, !tbaa !11
  %56 = icmp ugt i32 %55, 65535
  br i1 %56, label %57, label %58

57:                                               ; preds = %52
  store i64 -9223372036854775794, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %62

58:                                               ; preds = %52
  %59 = load i32, ptr %9, align 4, !tbaa !11
  %60 = trunc i32 %59 to i16
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  store i16 %60, ptr %61, align 2, !tbaa !9
  store i64 0, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %62

62:                                               ; preds = %58, %57, %42, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  %63 = load i64, ptr %3, align 8
  ret i64 %63
}

declare i64 @PxeBcUniHexToUint8(ptr noundef, i16 noundef zeroext) #1

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, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

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

declare zeroext i16 @SwapBytes16(i16 noundef zeroext) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775794, 1) i64 @PxeBcExtractBootFileParam(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #2 {
  %3 = alloca i16, align 2
  %4 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %4) #3
  %5 = call ptr @CopyMem(ptr noundef nonnull %3, ptr noundef %0, i64 noundef 2) #3
  %6 = load i16, ptr %3, align 2, !tbaa !5
  %7 = call zeroext i16 @SwapBytes16(i16 noundef zeroext %6) #3
  store i16 %7, ptr %3, align 2, !tbaa !5
  %8 = add i16 %7, -6
  %or.cond = icmp ult i16 %8, -5
  br i1 %or.cond, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %9 = getelementptr inbounds i8, ptr %0, i64 2
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %16
  %.019 = phi i32 [ %20, %16 ], [ 0, %.lr.ph.preheader ]
  %.01318 = phi i8 [ %21, %16 ], [ 0, %.lr.ph.preheader ]
  %10 = zext i8 %.01318 to i64
  %11 = getelementptr inbounds i8, ptr %9, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !9
  %13 = sext i8 %12 to i16
  %14 = call i64 @PxeBcUniHexToUint8(ptr noundef nonnull %4, i16 noundef zeroext %13) #3
  %15 = icmp slt i64 %14, 0
  br i1 %15, label %.loopexit, label %16

16:                                               ; preds = %.lr.ph
  %17 = load i8, ptr %4, align 1, !tbaa !9
  %18 = zext i8 %17 to i32
  %19 = add i32 %.019, %18
  %20 = mul i32 %19, 10
  %21 = add i8 %.01318, 1
  %22 = load i16, ptr %3, align 2, !tbaa !5
  %23 = zext i8 %21 to i16
  %24 = icmp ugt i16 %22, %23
  br i1 %24, label %.lr.ph, label %._crit_edge, !llvm.loop !10

._crit_edge:                                      ; preds = %16
  %25 = icmp ugt i32 %20, 655359
  br i1 %25, label %.loopexit, label %26

26:                                               ; preds = %._crit_edge
  %27 = udiv i32 %20, 10
  %28 = trunc nuw i32 %27 to i16
  store i16 %28, ptr %1, align 2, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %._crit_edge, %2, %26
  %.014 = phi i64 [ 0, %26 ], [ -9223372036854775794, %2 ], [ -9223372036854775794, %._crit_edge ], [ -9223372036854775794, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %3) #3
  ret i64 %.014
}

declare i64 @PxeBcUniHexToUint8(ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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