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

; 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.UNICODE_WIDTH_ENTRY = type { i16, i32 }

@mUnicodeWidthTable = external dso_local constant [18 x %struct.UNICODE_WIDTH_ENTRY], align 16

; Function Attrs: nounwind uwtable
define dso_local i64 @GetGlyphWidth(i16 noundef zeroext %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i16, align 2
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store i16 %0, ptr %3, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store ptr null, ptr %7, align 8, !tbaa !9
  store i64 0, ptr %5, align 8, !tbaa !11
  store i64 17, ptr %6, align 8, !tbaa !11
  br label %9

9:                                                ; preds = %58, %1
  %10 = load i64, ptr %5, align 8, !tbaa !11
  %11 = load i64, ptr %6, align 8, !tbaa !11
  %12 = icmp ule i64 %10, %11
  br i1 %12, label %13, label %59

13:                                               ; preds = %9
  %14 = load i64, ptr %5, align 8, !tbaa !11
  %15 = load i64, ptr %6, align 8, !tbaa !11
  %16 = add i64 %14, %15
  %17 = lshr i64 %16, 1
  store i64 %17, ptr %4, align 8, !tbaa !11
  %18 = load i64, ptr %4, align 8, !tbaa !11
  %19 = getelementptr inbounds [18 x %struct.UNICODE_WIDTH_ENTRY], ptr @mUnicodeWidthTable, i64 0, i64 %18
  store ptr %19, ptr %7, align 8, !tbaa !9
  %20 = load i64, ptr %4, align 8, !tbaa !11
  %21 = icmp eq i64 %20, 0
  br i1 %21, label %22, label %32

22:                                               ; preds = %13
  %23 = load i16, ptr %3, align 2, !tbaa !5
  %24 = zext i16 %23 to i32
  %25 = load ptr, ptr %7, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.UNICODE_WIDTH_ENTRY, ptr %25, i32 0, i32 0
  %27 = load i16, ptr %26, align 4, !tbaa !13
  %28 = zext i16 %27 to i32
  %29 = icmp sle i32 %24, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %22
  br label %59

31:                                               ; preds = %22
  store i64 0, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %69

32:                                               ; preds = %13
  %33 = load i16, ptr %3, align 2, !tbaa !5
  %34 = zext i16 %33 to i32
  %35 = load ptr, ptr %7, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.UNICODE_WIDTH_ENTRY, ptr %35, i32 0, i32 0
  %37 = load i16, ptr %36, align 4, !tbaa !13
  %38 = zext i16 %37 to i32
  %39 = icmp sgt i32 %34, %38
  br i1 %39, label %40, label %43

40:                                               ; preds = %32
  %41 = load i64, ptr %4, align 8, !tbaa !11
  %42 = add i64 %41, 1
  store i64 %42, ptr %5, align 8, !tbaa !11
  br label %58

43:                                               ; preds = %32
  %44 = load i16, ptr %3, align 2, !tbaa !5
  %45 = zext i16 %44 to i32
  %46 = load i64, ptr %4, align 8, !tbaa !11
  %47 = sub i64 %46, 1
  %48 = getelementptr inbounds [18 x %struct.UNICODE_WIDTH_ENTRY], ptr @mUnicodeWidthTable, i64 0, i64 %47
  %49 = getelementptr inbounds %struct.UNICODE_WIDTH_ENTRY, ptr %48, i32 0, i32 0
  %50 = load i16, ptr %49, align 8, !tbaa !13
  %51 = zext i16 %50 to i32
  %52 = icmp sle i32 %45, %51
  br i1 %52, label %53, label %56

53:                                               ; preds = %43
  %54 = load i64, ptr %4, align 8, !tbaa !11
  %55 = sub i64 %54, 1
  store i64 %55, ptr %6, align 8, !tbaa !11
  br label %57

56:                                               ; preds = %43
  br label %59

57:                                               ; preds = %53
  br label %58

58:                                               ; preds = %57, %40
  br label %9, !llvm.loop !16

59:                                               ; preds = %56, %30, %9
  %60 = load i64, ptr %5, align 8, !tbaa !11
  %61 = load i64, ptr %6, align 8, !tbaa !11
  %62 = icmp ule i64 %60, %61
  br i1 %62, label %63, label %68

63:                                               ; preds = %59
  %64 = load ptr, ptr %7, align 8, !tbaa !9
  %65 = getelementptr inbounds %struct.UNICODE_WIDTH_ENTRY, ptr %64, i32 0, i32 1
  %66 = load i32, ptr %65, align 4, !tbaa !18
  %67 = zext i32 %66 to i64
  store i64 %67, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %69

68:                                               ; preds = %59
  store i64 0, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %69

69:                                               ; preds = %68, %63, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %70 = load i64, ptr %2, align 8
  ret i64 %70
}

; 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

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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"", !6, i64 0, !15, i64 4}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!14, !15, 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"

%struct.UNICODE_WIDTH_ENTRY = type { i16, i32 }

@mUnicodeWidthTable = external dso_local local_unnamed_addr constant [18 x %struct.UNICODE_WIDTH_ENTRY], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local range(i64 0, 4294967296) i64 @GetGlyphWidth(i16 noundef zeroext %0) local_unnamed_addr #0 {
  br label %2

2:                                                ; preds = %1, %18
  %.01731 = phi i64 [ 17, %1 ], [ %.118, %18 ]
  %.01930 = phi i64 [ 0, %1 ], [ %.120, %18 ]
  %3 = add i64 %.01731, %.01930
  %4 = lshr i64 %3, 1
  %5 = getelementptr inbounds [18 x %struct.UNICODE_WIDTH_ENTRY], ptr @mUnicodeWidthTable, i64 0, i64 %4
  %6 = icmp ult i64 %3, 2
  br i1 %6, label %7, label %9

7:                                                ; preds = %2
  %8 = load i16, ptr %5, align 4, !tbaa !5
  %.not25 = icmp ult i16 %8, %0
  br i1 %.not25, label %.loopexit, label %.loopexit28

9:                                                ; preds = %2
  %10 = load i16, ptr %5, align 8, !tbaa !5
  %11 = icmp ult i16 %10, %0
  br i1 %11, label %12, label %14

12:                                               ; preds = %9
  %13 = add nuw i64 %4, 1
  br label %18

14:                                               ; preds = %9
  %15 = add nsw i64 %4, -1
  %16 = getelementptr inbounds [18 x %struct.UNICODE_WIDTH_ENTRY], ptr @mUnicodeWidthTable, i64 0, i64 %15
  %17 = load i16, ptr %16, align 8, !tbaa !5
  %.not24 = icmp ult i16 %17, %0
  br i1 %.not24, label %.loopexit28, label %18

18:                                               ; preds = %14, %12
  %.120 = phi i64 [ %13, %12 ], [ %.01930, %14 ]
  %.118 = phi i64 [ %.01731, %12 ], [ %15, %14 ]
  %.not = icmp ugt i64 %.120, %.118
  br i1 %.not, label %.loopexit, label %2, !llvm.loop !11

.loopexit28:                                      ; preds = %14, %7
  %19 = getelementptr inbounds i8, ptr %5, i64 4
  %20 = load i32, ptr %19, align 4, !tbaa !13
  %21 = zext i32 %20 to i64
  br label %.loopexit

.loopexit:                                        ; preds = %18, %7, %.loopexit28
  %.021 = phi i64 [ %21, %.loopexit28 ], [ 0, %7 ], [ 0, %18 ]
  ret i64 %.021
}

attributes #0 = { nofree norecurse nosync nounwind memory(none) 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 4}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!6, !10, i64 4}


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