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

; 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.IDE_DEVICE = type { i8, i8, i8, i16, i16, i16, i32, i32, [41 x i8] }

@g_ide_devices = external dso_local global [5 x %struct.IDE_DEVICE], align 16
@.str.24 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [80 x i8], align 1

; 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 zeroext i8 @ide_print_error(i32 noundef, i8 noundef zeroext) #1

declare void @printf(ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @ide_ata_access(i8 noundef zeroext, i8 noundef zeroext, i32 noundef, i8 noundef zeroext, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @ide_read_sectors(i8 noundef zeroext %0, i8 noundef zeroext %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  store i8 %0, ptr %6, align 1, !tbaa !5
  store i8 %1, ptr %7, align 1, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !8
  store i32 %3, ptr %9, align 4, !tbaa !8
  %11 = load i8, ptr %6, align 1, !tbaa !5
  %12 = zext i8 %11 to i32
  %13 = icmp sgt i32 %12, 5
  br i1 %13, label %22, label %14

14:                                               ; preds = %4
  %15 = load i8, ptr %6, align 1, !tbaa !5
  %16 = zext i8 %15 to i64
  %17 = getelementptr inbounds [5 x %struct.IDE_DEVICE], ptr @g_ide_devices, i64 0, i64 %16
  %18 = getelementptr inbounds %struct.IDE_DEVICE, ptr %17, i32 0, i32 0
  %19 = load i8, ptr %18, align 16, !tbaa !10
  %20 = zext i8 %19 to i32
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %23

22:                                               ; preds = %14, %4
  call void (ptr, ...) @printf(ptr noundef @.str.24)
  store i32 -1, ptr %5, align 4
  br label %69

23:                                               ; preds = %14
  %24 = load i32, ptr %8, align 4, !tbaa !8
  %25 = load i8, ptr %7, align 1, !tbaa !5
  %26 = zext i8 %25 to i32
  %27 = add i32 %24, %26
  %28 = load i8, ptr %6, align 1, !tbaa !5
  %29 = zext i8 %28 to i64
  %30 = getelementptr inbounds [5 x %struct.IDE_DEVICE], ptr @g_ide_devices, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.IDE_DEVICE, ptr %30, i32 0, i32 7
  %32 = load i32, ptr %31, align 16, !tbaa !13
  %33 = icmp ugt i32 %27, %32
  br i1 %33, label %34, label %49

34:                                               ; preds = %23
  %35 = load i8, ptr %6, align 1, !tbaa !5
  %36 = zext i8 %35 to i64
  %37 = getelementptr inbounds [5 x %struct.IDE_DEVICE], ptr @g_ide_devices, i64 0, i64 %36
  %38 = getelementptr inbounds %struct.IDE_DEVICE, ptr %37, i32 0, i32 3
  %39 = load i16, ptr %38, align 4, !tbaa !14
  %40 = zext i16 %39 to i32
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %49

42:                                               ; preds = %34
  %43 = load i32, ptr %8, align 4, !tbaa !8
  %44 = load i8, ptr %6, align 1, !tbaa !5
  %45 = zext i8 %44 to i64
  %46 = getelementptr inbounds [5 x %struct.IDE_DEVICE], ptr @g_ide_devices, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.IDE_DEVICE, ptr %46, i32 0, i32 7
  %48 = load i32, ptr %47, align 16, !tbaa !13
  call void (ptr, ...) @printf(ptr noundef @.str.25, i32 noundef %43, i32 noundef %48)
  store i32 -2, ptr %5, align 4
  br label %69

49:                                               ; preds = %34, %23
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #3
  %50 = load i8, ptr %6, align 1, !tbaa !5
  %51 = zext i8 %50 to i64
  %52 = getelementptr inbounds [5 x %struct.IDE_DEVICE], ptr @g_ide_devices, i64 0, i64 %51
  %53 = getelementptr inbounds %struct.IDE_DEVICE, ptr %52, i32 0, i32 3
  %54 = load i16, ptr %53, align 4, !tbaa !14
  %55 = zext i16 %54 to i32
  %56 = icmp eq i32 %55, 0
  br i1 %56, label %57, label %63

57:                                               ; preds = %49
  %58 = load i8, ptr %6, align 1, !tbaa !5
  %59 = load i32, ptr %8, align 4, !tbaa !8
  %60 = load i8, ptr %7, align 1, !tbaa !5
  %61 = load i32, ptr %9, align 4, !tbaa !8
  %62 = call zeroext i8 @ide_ata_access(i8 noundef zeroext 0, i8 noundef zeroext %58, i32 noundef %59, i8 noundef zeroext %60, i32 noundef %61)
  store i8 %62, ptr %10, align 1, !tbaa !5
  br label %63

63:                                               ; preds = %57, %49
  %64 = load i8, ptr %6, align 1, !tbaa !5
  %65 = zext i8 %64 to i32
  %66 = load i8, ptr %10, align 1, !tbaa !5
  %67 = call zeroext i8 @ide_print_error(i32 noundef %65, i8 noundef zeroext %66)
  %68 = zext i8 %67 to i32
  store i32 %68, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #3
  br label %69

69:                                               ; preds = %63, %42, %22
  %70 = load i32, ptr %5, align 4
  ret i32 %70
}

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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = !{!11, !6, i64 0}
!11 = !{!"", !6, i64 0, !6, i64 1, !6, i64 2, !12, i64 4, !12, i64 6, !12, i64 8, !9, i64 12, !9, i64 16, !6, i64 20}
!12 = !{!"short", !6, i64 0}
!13 = !{!11, !9, i64 16}
!14 = !{!11, !12, 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.IDE_DEVICE = type { i8, i8, i8, i16, i16, i16, i32, i32, [41 x i8] }

@g_ide_devices = external dso_local local_unnamed_addr global [5 x %struct.IDE_DEVICE], align 16
@.str.24 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [80 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @ide_print_error(i32 noundef, i8 noundef zeroext) local_unnamed_addr #0

declare void @printf(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @ide_ata_access(i8 noundef zeroext, i8 noundef zeroext, i32 noundef, i8 noundef zeroext, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -2, 256) i32 @ide_read_sectors(i8 noundef zeroext %0, i8 noundef zeroext %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = zext i8 %0 to i32
  %6 = icmp ugt i8 %0, 5
  br i1 %6, label %12, label %7

7:                                                ; preds = %4
  %8 = zext nneg i8 %0 to i64
  %9 = getelementptr inbounds [5 x %struct.IDE_DEVICE], ptr @g_ide_devices, i64 0, i64 %8
  %10 = load i8, ptr %9, align 16, !tbaa !5
  %11 = icmp eq i8 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %7, %4
  tail call void (ptr, ...) @printf(ptr noundef nonnull @.str.24) #3
  br label %26

13:                                               ; preds = %7
  %14 = zext i8 %1 to i32
  %15 = add i32 %14, %2
  %16 = getelementptr inbounds i8, ptr %9, i64 16
  %17 = load i32, ptr %16, align 16, !tbaa !11
  %18 = icmp ule i32 %15, %17
  %19 = getelementptr inbounds i8, ptr %9, i64 4
  %20 = load i16, ptr %19, align 4, !tbaa !12
  %21 = icmp eq i16 %20, 0
  %.not19 = xor i1 %21, true
  %brmerge = select i1 %18, i1 true, i1 %.not19
  br i1 %brmerge, label %._crit_edge, label %22

22:                                               ; preds = %13
  tail call void (ptr, ...) @printf(ptr noundef nonnull @.str.25, i32 noundef %2, i32 noundef %17) #3
  br label %26

._crit_edge:                                      ; preds = %13
  tail call void @llvm.assume(i1 %18)
  tail call void @llvm.assume(i1 %21)
  %23 = tail call zeroext i8 @ide_ata_access(i8 noundef zeroext 0, i8 noundef zeroext %0, i32 noundef %2, i8 noundef zeroext %1, i32 noundef %3)
  %24 = tail call zeroext i8 @ide_print_error(i32 noundef %5, i8 noundef zeroext %23)
  %25 = zext i8 %24 to i32
  br label %26

26:                                               ; preds = %._crit_edge, %22, %12
  %.016 = phi i32 [ -1, %12 ], [ -2, %22 ], [ %25, %._crit_edge ]
  ret i32 %.016
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #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 willreturn memory(inaccessiblemem: write) }
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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !9, i64 4, !9, i64 6, !9, i64 8, !10, i64 12, !10, i64 16, !7, i64 20}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"short", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!6, !9, i64 4}


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