; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/OptionRomPkg (PxeUpdate)

; 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.s_pxe_sw_undi = type { i32, i8, i8, i8, i8, i8, i8, i8, i8, i32, i64, [3 x i8], i8, [1 x i32] }

; 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

; Function Attrs: nounwind uwtable
define dso_local void @PxeUpdate(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i16, align 2
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #3
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.s_pxe_sw_undi, ptr %6, i32 0, i32 4
  %8 = load i8, ptr %7, align 1, !tbaa !9
  %9 = zext i8 %8 to i32
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.s_pxe_sw_undi, ptr %10, i32 0, i32 7
  %12 = load i8, ptr %11, align 1, !tbaa !13
  %13 = zext i8 %12 to i32
  %14 = shl i32 %13, 8
  %15 = or i32 %9, %14
  %16 = trunc i32 %15 to i16
  store i16 %16, ptr %5, align 2, !tbaa !14
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %27

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

23:                                               ; preds = %19
  %24 = load i16, ptr %5, align 2, !tbaa !14
  %25 = add i16 %24, -1
  store i16 %25, ptr %5, align 2, !tbaa !14
  br label %26

26:                                               ; preds = %23, %19
  br label %30

27:                                               ; preds = %2
  %28 = load i16, ptr %5, align 2, !tbaa !14
  %29 = add i16 %28, 1
  store i16 %29, ptr %5, align 2, !tbaa !14
  br label %30

30:                                               ; preds = %27, %26
  %31 = load i16, ptr %5, align 2, !tbaa !14
  %32 = zext i16 %31 to i32
  %33 = and i32 %32, 255
  %34 = trunc i32 %33 to i8
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.s_pxe_sw_undi, ptr %35, i32 0, i32 4
  store i8 %34, ptr %36, align 1, !tbaa !9
  %37 = load i16, ptr %5, align 2, !tbaa !14
  %38 = zext i16 %37 to i32
  %39 = and i32 %38, 65280
  %40 = ashr i32 %39, 8
  %41 = trunc i32 %40 to i8
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.s_pxe_sw_undi, ptr %42, i32 0, i32 7
  store i8 %41, ptr %43, align 1, !tbaa !13
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.s_pxe_sw_undi, ptr %44, i32 0, i32 2
  %46 = load i8, ptr %45, align 1, !tbaa !16
  %47 = zext i8 %46 to i32
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.s_pxe_sw_undi, ptr %49, i32 0, i32 1
  %51 = load i8, ptr %50, align 1, !tbaa !17
  %52 = zext i8 %51 to i64
  %53 = call zeroext i8 @CalculateSum8(ptr noundef %48, i64 noundef %52)
  %54 = zext i8 %53 to i32
  %55 = sub nsw i32 %47, %54
  %56 = trunc i32 %55 to i8
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.s_pxe_sw_undi, ptr %57, i32 0, i32 2
  store i8 %56, ptr %58, align 1, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #3
  ret void
}

declare zeroext i8 @CalculateSum8(ptr noundef, i64 noundef) #2

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


; 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: nounwind uwtable
define dso_local void @PxeUpdate(ptr noundef readnone %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %1, i64 7
  %4 = load i8, ptr %3, align 1, !tbaa !5
  %5 = zext i8 %4 to i16
  %6 = getelementptr inbounds i8, ptr %1, i64 10
  %7 = load i8, ptr %6, align 1, !tbaa !11
  %8 = zext i8 %7 to i16
  %9 = shl nuw i16 %8, 8
  %10 = or disjoint i16 %9, %5
  %11 = icmp eq ptr %0, null
  %12 = add i16 %10, 1
  %spec.select = tail call i16 @llvm.usub.sat.i16(i16 %10, i16 1)
  %.0 = select i1 %11, i16 %spec.select, i16 %12
  %13 = trunc i16 %.0 to i8
  store i8 %13, ptr %3, align 1, !tbaa !5
  %14 = lshr i16 %.0, 8
  %15 = trunc nuw i16 %14 to i8
  store i8 %15, ptr %6, align 1, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %1, i64 5
  %17 = load i8, ptr %16, align 1, !tbaa !12
  %18 = getelementptr inbounds i8, ptr %1, i64 4
  %19 = load i8, ptr %18, align 1, !tbaa !13
  %20 = zext i8 %19 to i64
  %21 = tail call zeroext i8 @CalculateSum8(ptr noundef %1, i64 noundef %20) #3
  %22 = sub i8 %17, %21
  store i8 %22, ptr %16, align 1, !tbaa !12
  ret void
}

declare zeroext i8 @CalculateSum8(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.usub.sat.i16(i16, i16) #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 = { "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 speculatable willreturn memory(none) }
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, !8, i64 7}
!6 = !{!"s_pxe_sw_undi", !7, i64 0, !8, i64 4, !8, i64 5, !8, i64 6, !8, i64 7, !8, i64 8, !8, i64 9, !8, i64 10, !8, i64 11, !7, i64 12, !10, i64 16, !8, i64 24, !8, i64 27, !8, i64 28}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!6, !8, i64 10}
!12 = !{!6, !8, i64 5}
!13 = !{!6, !8, i64 4}


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