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

; 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.SCRATCH_DATA = type { ptr, ptr, i32, i32, i16, i32, i32, i16, i32, i32, i16, [1019 x i16], [1019 x i16], [510 x i8], [31 x i8], [4096 x i16], [256 x i16], i8 }

; Function Attrs: nounwind uwtable
declare dso_local void @FillBuf(ptr noundef, i16 noundef zeroext) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @GetBits(ptr noundef, i16 noundef zeroext) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @DecodeP(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i16, align 2
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.SCRATCH_DATA, ptr %6, i32 0, i32 16
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.SCRATCH_DATA, ptr %8, i32 0, i32 5
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = lshr i32 %10, 24
  %12 = zext i32 %11 to i64
  %13 = getelementptr inbounds [256 x i16], ptr %7, i64 0, i64 %12
  %14 = load i16, ptr %13, align 2, !tbaa !13
  store i16 %14, ptr %3, align 2, !tbaa !13
  %15 = load i16, ptr %3, align 2, !tbaa !13
  %16 = zext i16 %15 to i32
  %17 = icmp uge i32 %16, 31
  br i1 %17, label %18, label %48

18:                                               ; preds = %1
  store i32 8388608, ptr %4, align 4, !tbaa !14
  br label %19

19:                                               ; preds = %43, %18
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.SCRATCH_DATA, ptr %20, i32 0, i32 5
  %22 = load i32, ptr %21, align 4, !tbaa !9
  %23 = load i32, ptr %4, align 4, !tbaa !14
  %24 = and i32 %22, %23
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %33

26:                                               ; preds = %19
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.SCRATCH_DATA, ptr %27, i32 0, i32 12
  %29 = load i16, ptr %3, align 2, !tbaa !13
  %30 = zext i16 %29 to i64
  %31 = getelementptr inbounds [1019 x i16], ptr %28, i64 0, i64 %30
  %32 = load i16, ptr %31, align 2, !tbaa !13
  store i16 %32, ptr %3, align 2, !tbaa !13
  br label %40

33:                                               ; preds = %19
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.SCRATCH_DATA, ptr %34, i32 0, i32 11
  %36 = load i16, ptr %3, align 2, !tbaa !13
  %37 = zext i16 %36 to i64
  %38 = getelementptr inbounds [1019 x i16], ptr %35, i64 0, i64 %37
  %39 = load i16, ptr %38, align 2, !tbaa !13
  store i16 %39, ptr %3, align 2, !tbaa !13
  br label %40

40:                                               ; preds = %33, %26
  %41 = load i32, ptr %4, align 4, !tbaa !14
  %42 = lshr i32 %41, 1
  store i32 %42, ptr %4, align 4, !tbaa !14
  br label %43

43:                                               ; preds = %40
  %44 = load i16, ptr %3, align 2, !tbaa !13
  %45 = zext i16 %44 to i32
  %46 = icmp uge i32 %45, 31
  br i1 %46, label %19, label %47, !llvm.loop !15

47:                                               ; preds = %43
  br label %48

48:                                               ; preds = %47, %1
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.SCRATCH_DATA, ptr %50, i32 0, i32 14
  %52 = load i16, ptr %3, align 2, !tbaa !13
  %53 = zext i16 %52 to i64
  %54 = getelementptr inbounds [31 x i8], ptr %51, i64 0, i64 %53
  %55 = load i8, ptr %54, align 1, !tbaa !17
  %56 = zext i8 %55 to i16
  call void @FillBuf(ptr noundef %49, i16 noundef zeroext %56)
  %57 = load i16, ptr %3, align 2, !tbaa !13
  %58 = zext i16 %57 to i32
  store i32 %58, ptr %5, align 4, !tbaa !14
  %59 = load i16, ptr %3, align 2, !tbaa !13
  %60 = zext i16 %59 to i32
  %61 = icmp sgt i32 %60, 1
  br i1 %61, label %62, label %74

62:                                               ; preds = %48
  %63 = load i16, ptr %3, align 2, !tbaa !13
  %64 = zext i16 %63 to i32
  %65 = sub nsw i32 %64, 1
  %66 = shl i32 1, %65
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = load i16, ptr %3, align 2, !tbaa !13
  %69 = zext i16 %68 to i32
  %70 = sub nsw i32 %69, 1
  %71 = trunc i32 %70 to i16
  %72 = call i32 @GetBits(ptr noundef %67, i16 noundef zeroext %71)
  %73 = add i32 %66, %72
  store i32 %73, ptr %5, align 4, !tbaa !14
  br label %74

74:                                               ; preds = %62, %48
  %75 = load i32, ptr %5, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #2
  ret i32 %75
}

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 = { 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 28}
!10 = !{!"", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 20, !12, i64 24, !11, i64 28, !11, i64 32, !12, i64 36, !11, i64 40, !11, i64 44, !12, i64 48, !7, i64 50, !7, i64 2088, !7, i64 4126, !7, i64 4636, !7, i64 4668, !7, i64 12860, !7, i64 13372}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!11, !11, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!7, !7, i64 0}


; 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
declare dso_local void @FillBuf(ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @GetBits(ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @DecodeP(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 12860
  %3 = getelementptr inbounds i8, ptr %0, i64 28
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = lshr i32 %4, 24
  %6 = zext nneg i32 %5 to i64
  %7 = getelementptr inbounds [256 x i16], ptr %2, i64 0, i64 %6
  %8 = load i16, ptr %7, align 2, !tbaa !12
  %9 = icmp ugt i16 %8, 30
  br i1 %9, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %1
  %10 = getelementptr inbounds i8, ptr %0, i64 2088
  %11 = getelementptr inbounds i8, ptr %0, i64 50
  br label %12

12:                                               ; preds = %.preheader, %12
  %.020 = phi i16 [ %.1, %12 ], [ %8, %.preheader ]
  %.019 = phi i32 [ %17, %12 ], [ 8388608, %.preheader ]
  %13 = and i32 %.019, %4
  %.not = icmp eq i32 %13, 0
  %14 = zext i16 %.020 to i64
  %15 = getelementptr inbounds [1019 x i16], ptr %10, i64 0, i64 %14
  %16 = getelementptr inbounds [1019 x i16], ptr %11, i64 0, i64 %14
  %.1.in = select i1 %.not, ptr %16, ptr %15
  %.1 = load i16, ptr %.1.in, align 2, !tbaa !12
  %17 = lshr i32 %.019, 1
  %18 = icmp ugt i16 %.1, 30
  br i1 %18, label %12, label %.loopexit, !llvm.loop !13

.loopexit:                                        ; preds = %12, %1
  %.2 = phi i16 [ %8, %1 ], [ %.1, %12 ]
  %19 = getelementptr inbounds i8, ptr %0, i64 4636
  %20 = zext nneg i16 %.2 to i64
  %21 = getelementptr inbounds [31 x i8], ptr %19, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !15
  %23 = zext i8 %22 to i16
  tail call void @FillBuf(ptr noundef %0, i16 noundef zeroext %23)
  %24 = zext nneg i16 %.2 to i32
  %25 = icmp ugt i16 %.2, 1
  br i1 %25, label %26, label %32

26:                                               ; preds = %.loopexit
  %27 = add nsw i32 %24, -1
  %28 = shl nuw nsw i32 1, %27
  %29 = trunc nuw nsw i32 %27 to i16
  %30 = tail call i32 @GetBits(ptr noundef nonnull %0, i16 noundef zeroext %29)
  %31 = add i32 %30, %28
  br label %32

32:                                               ; preds = %26, %.loopexit
  %.0 = phi i32 [ %31, %26 ], [ %24, %.loopexit ]
  ret i32 %.0
}

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

!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, !10, i64 28}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !11, i64 24, !10, i64 28, !10, i64 32, !11, i64 36, !10, i64 40, !10, i64 44, !11, i64 48, !8, i64 50, !8, i64 2088, !8, i64 4126, !8, i64 4636, !8, i64 4668, !8, i64 12860, !8, i64 13372}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!11, !11, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!8, !8, i64 0}


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