; LLVM IR from xing1357/SimpleOS :: src (vesa)

; 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.VBE20_INFOBLOCK = type <{ [4 x i8], i16, ptr, i32, ptr, i16, i16, i32, i32, i32, [222 x i8], [256 x i8] }>
%struct.VBE20_MODEINFOBLOCK = type { i16, i8, i8, i16, i16, i16, i16, ptr, i16, i16, i16, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i32, i32, i16, [206 x i8] }

@g_vbe_infoblock = external dso_local global %struct.VBE20_INFOBLOCK, align 1
@g_vbe_modeinfoblock = external dso_local global %struct.VBE20_MODEINFOBLOCK, align 8

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

; Function Attrs: nounwind uwtable
define dso_local i32 @vbe_find_mode(i32 noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i16, align 2
  %10 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %11 = load ptr, ptr getelementptr inbounds (%struct.VBE20_INFOBLOCK, ptr @g_vbe_infoblock, i32 0, i32 4), align 1, !tbaa !9
  store ptr %11, ptr %8, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  %12 = load ptr, ptr %8, align 8, !tbaa !13
  %13 = getelementptr inbounds i16, ptr %12, i32 1
  store ptr %13, ptr %8, align 8, !tbaa !13
  %14 = load i16, ptr %12, align 2, !tbaa !14
  store i16 %14, ptr %9, align 2, !tbaa !14
  br label %15

15:                                               ; preds = %38, %3
  %16 = load i16, ptr %9, align 2, !tbaa !14
  %17 = zext i16 %16 to i32
  %18 = icmp ne i32 %17, 65535
  br i1 %18, label %19, label %42

19:                                               ; preds = %15
  %20 = load i16, ptr %9, align 2, !tbaa !14
  call void @get_vbe_mode_info(i16 noundef zeroext %20, ptr noundef @g_vbe_modeinfoblock)
  %21 = load i16, ptr getelementptr inbounds (%struct.VBE20_MODEINFOBLOCK, ptr @g_vbe_modeinfoblock, i32 0, i32 9), align 2, !tbaa !15
  %22 = zext i16 %21 to i32
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = icmp eq i32 %22, %23
  br i1 %24, label %25, label %38

25:                                               ; preds = %19
  %26 = load i16, ptr getelementptr inbounds (%struct.VBE20_MODEINFOBLOCK, ptr @g_vbe_modeinfoblock, i32 0, i32 10), align 4, !tbaa !17
  %27 = zext i16 %26 to i32
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = icmp eq i32 %27, %28
  br i1 %29, label %30, label %38

30:                                               ; preds = %25
  %31 = load i8, ptr getelementptr inbounds (%struct.VBE20_MODEINFOBLOCK, ptr @g_vbe_modeinfoblock, i32 0, i32 14), align 1, !tbaa !18
  %32 = zext i8 %31 to i32
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = icmp eq i32 %32, %33
  br i1 %34, label %35, label %38

35:                                               ; preds = %30
  %36 = load i16, ptr %9, align 2, !tbaa !14
  %37 = zext i16 %36 to i32
  store i32 %37, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %43

38:                                               ; preds = %30, %25, %19
  %39 = load ptr, ptr %8, align 8, !tbaa !13
  %40 = getelementptr inbounds i16, ptr %39, i32 1
  store ptr %40, ptr %8, align 8, !tbaa !13
  %41 = load i16, ptr %39, align 2, !tbaa !14
  store i16 %41, ptr %9, align 2, !tbaa !14
  br label %15, !llvm.loop !19

42:                                               ; preds = %15
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %43

43:                                               ; preds = %42, %35
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %44 = load i32, ptr %4, align 4
  ret i32 %44
}

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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 18}
!10 = !{!"", !7, i64 0, !11, i64 4, !12, i64 6, !6, i64 14, !12, i64 18, !11, i64 26, !11, i64 28, !6, i64 30, !6, i64 34, !6, i64 38, !7, i64 42, !7, i64 264}
!11 = !{!"short", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!16, !11, i64 26}
!16 = !{!"", !11, i64 0, !7, i64 2, !7, i64 3, !11, i64 4, !11, i64 6, !11, i64 8, !11, i64 10, !12, i64 16, !11, i64 24, !11, i64 26, !11, i64 28, !7, i64 30, !7, i64 31, !7, i64 32, !7, i64 33, !7, i64 34, !7, i64 35, !7, i64 36, !7, i64 37, !7, i64 38, !7, i64 39, !7, i64 40, !7, i64 41, !7, i64 42, !7, i64 43, !7, i64 44, !7, i64 45, !7, i64 46, !7, i64 47, !6, i64 48, !6, i64 52, !11, i64 56, !7, i64 58}
!17 = !{!16, !11, i64 28}
!18 = !{!16, !7, i64 33}
!19 = distinct !{!19, !20}
!20 = !{!"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"

%struct.VBE20_INFOBLOCK = type <{ [4 x i8], i16, ptr, i32, ptr, i16, i16, i32, i32, i32, [222 x i8], [256 x i8] }>
%struct.VBE20_MODEINFOBLOCK = type { i16, i8, i8, i16, i16, i16, i16, ptr, i16, i16, i16, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8, i32, i32, i16, [206 x i8] }

@g_vbe_infoblock = external dso_local local_unnamed_addr global %struct.VBE20_INFOBLOCK, align 1
@g_vbe_modeinfoblock = external dso_local global %struct.VBE20_MODEINFOBLOCK, align 8

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 65535) i32 @vbe_find_mode(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = load ptr, ptr getelementptr inbounds (i8, ptr @g_vbe_infoblock, i64 18), align 1, !tbaa !5
  br label %5

5:                                                ; preds = %6, %3
  %.0.in = phi ptr [ %4, %3 ], [ %.08, %6 ]
  %.0 = load i16, ptr %.0.in, align 2, !tbaa !12
  %.not = icmp eq i16 %.0, -1
  br i1 %.not, label %.split.loop.exit, label %6

6:                                                ; preds = %5
  %.08 = getelementptr inbounds i8, ptr %.0.in, i64 2
  tail call void @get_vbe_mode_info(i16 noundef zeroext %.0, ptr noundef nonnull @g_vbe_modeinfoblock)
  %7 = load i16, ptr getelementptr inbounds (i8, ptr @g_vbe_modeinfoblock, i64 26), align 2, !tbaa !13
  %8 = zext i16 %7 to i32
  %9 = icmp eq i32 %8, %0
  %10 = load i16, ptr getelementptr inbounds (i8, ptr @g_vbe_modeinfoblock, i64 28), align 4
  %11 = zext i16 %10 to i32
  %12 = icmp eq i32 %11, %1
  %or.cond = select i1 %9, i1 %12, i1 false
  %13 = load i8, ptr getelementptr inbounds (i8, ptr @g_vbe_modeinfoblock, i64 33), align 1
  %14 = zext i8 %13 to i32
  %15 = icmp eq i32 %14, %2
  %or.cond15 = select i1 %or.cond, i1 %15, i1 false
  br i1 %or.cond15, label %.split.loop.exit16, label %5, !llvm.loop !15

.split.loop.exit16:                               ; preds = %6
  %16 = zext i16 %.0 to i32
  br label %.split.loop.exit

.split.loop.exit:                                 ; preds = %5, %.split.loop.exit16
  %.09 = phi i32 [ %16, %.split.loop.exit16 ], [ -1, %5 ]
  ret i32 %.09
}

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 18}
!6 = !{!"", !7, i64 0, !9, i64 4, !10, i64 6, !11, i64 14, !10, i64 18, !9, i64 26, !9, i64 28, !11, i64 30, !11, i64 34, !11, i64 38, !7, i64 42, !7, i64 264}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"short", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!9, !9, i64 0}
!13 = !{!14, !9, i64 26}
!14 = !{!"", !9, i64 0, !7, i64 2, !7, i64 3, !9, i64 4, !9, i64 6, !9, i64 8, !9, i64 10, !10, i64 16, !9, i64 24, !9, i64 26, !9, i64 28, !7, i64 30, !7, i64 31, !7, i64 32, !7, i64 33, !7, i64 34, !7, i64 35, !7, i64 36, !7, i64 37, !7, i64 38, !7, i64 39, !7, i64 40, !7, i64 41, !7, i64 42, !7, i64 43, !7, i64 44, !7, i64 45, !7, i64 46, !7, i64 47, !11, i64 48, !11, i64 52, !9, i64 56, !7, i64 58}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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