; LLVM IR from ryansturmer/thinfat32 :: src (thinfat32)

; 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.struct_tfinfo = type { i8, i8, i32, i32, i16, i32, i8, i32, [512 x i8] }
%struct.struct_TFFILE = type { i32, i32, i32, i32, i16, i16, i32, i8, i8, i8, i32, [256 x i8] }

@tf_info = external dso_local global %struct.struct_tfinfo, align 4

; 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 i32 @tf_fetch(i32 noundef) #1

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

10:                                               ; preds = %66, %3
  %11 = load i32, ptr %6, align 4, !tbaa !9
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %13, label %67

13:                                               ; preds = %10
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.struct_TFFILE, ptr %14, i32 0, i32 3
  %16 = load i32, ptr %15, align 4, !tbaa !11
  %17 = call i32 @tf_first_sector(i32 noundef %16)
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.struct_TFFILE, ptr %18, i32 0, i32 5
  %20 = load i16, ptr %19, align 2, !tbaa !14
  %21 = sext i16 %20 to i32
  %22 = sdiv i32 %21, 512
  %23 = add i32 %17, %22
  store i32 %23, ptr %8, align 4, !tbaa !9
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = call i32 @tf_fetch(i32 noundef %24)
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.struct_TFFILE, ptr %26, i32 0, i32 5
  %28 = load i16, ptr %27, align 2, !tbaa !14
  %29 = sext i16 %28 to i32
  %30 = srem i32 %29, 512
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [512 x i8], ptr getelementptr inbounds (%struct.struct_tfinfo, ptr @tf_info, i32 0, i32 8), i64 0, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !15
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %34, i32 1
  store ptr %35, ptr %5, align 8, !tbaa !5
  store i8 %33, ptr %34, align 1, !tbaa !15
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = add nsw i32 %36, -1
  store i32 %37, ptr %6, align 4, !tbaa !9
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.struct_TFFILE, ptr %38, i32 0, i32 8
  %40 = load i8, ptr %39, align 1, !tbaa !16
  %41 = zext i8 %40 to i32
  %42 = and i32 %41, 16
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %55

44:                                               ; preds = %13
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.struct_TFFILE, ptr %46, i32 0, i32 6
  %48 = load i32, ptr %47, align 4, !tbaa !17
  %49 = add i32 %48, 1
  %50 = zext i32 %49 to i64
  %51 = call i32 @tf_fseek(ptr noundef %45, i32 noundef 0, i64 noundef %50)
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %53, label %54

53:                                               ; preds = %44
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %68

54:                                               ; preds = %44
  br label %66

55:                                               ; preds = %13
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.struct_TFFILE, ptr %57, i32 0, i32 6
  %59 = load i32, ptr %58, align 4, !tbaa !17
  %60 = add i32 %59, 1
  %61 = zext i32 %60 to i64
  %62 = call i32 @tf_fseek(ptr noundef %56, i32 noundef 0, i64 noundef %61)
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %65

64:                                               ; preds = %55
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %68

65:                                               ; preds = %55
  br label %66

66:                                               ; preds = %65, %54
  br label %10, !llvm.loop !18

67:                                               ; preds = %10
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %68

68:                                               ; preds = %67, %64, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %69 = load i32, ptr %4, align 4
  ret i32 %69
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @tf_first_sector(i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @tf_fseek(ptr noundef, i32 noundef, i64 noundef) #1

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 12}
!12 = !{!"struct_TFFILE", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !13, i64 16, !13, i64 18, !10, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !10, i64 28, !7, i64 32}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 18}
!15 = !{!7, !7, i64 0}
!16 = !{!12, !7, i64 25}
!17 = !{!12, !10, i64 20}
!18 = distinct !{!18, !19}
!19 = !{!"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.struct_tfinfo = type { i8, i8, i32, i32, i16, i32, i8, i32, [512 x i8] }

@tf_info = external dso_local local_unnamed_addr global %struct.struct_tfinfo, align 4

; Function Attrs: nounwind uwtable
declare dso_local i32 @tf_fetch(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @tf_fread(ptr nocapture noundef writeonly %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %2, i64 20
  %5 = icmp sgt i32 %1, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %6 = getelementptr inbounds i8, ptr %2, i64 12
  %7 = getelementptr inbounds i8, ptr %2, i64 18
  br label %8

8:                                                ; preds = %.lr.ph, %25
  %.01217 = phi ptr [ %0, %.lr.ph ], [ %27, %25 ]
  %.01316 = phi i32 [ %1, %.lr.ph ], [ %26, %25 ]
  %9 = load i32, ptr %6, align 4, !tbaa !5
  %10 = tail call i32 @tf_first_sector(i32 noundef %9)
  %11 = load i16, ptr %7, align 2, !tbaa !11
  %12 = sdiv i16 %11, 512
  %13 = sext i16 %12 to i32
  %14 = add i32 %10, %13
  %15 = tail call i32 @tf_fetch(i32 noundef %14)
  %16 = load i16, ptr %7, align 2, !tbaa !11
  %17 = srem i16 %16, 512
  %18 = sext i16 %17 to i64
  %19 = getelementptr inbounds [512 x i8], ptr getelementptr inbounds (i8, ptr @tf_info, i64 28), i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !12
  store i8 %20, ptr %.01217, align 1, !tbaa !12
  %21 = load i32, ptr %4, align 4, !tbaa !13
  %22 = add i32 %21, 1
  %23 = zext i32 %22 to i64
  %24 = tail call i32 @tf_fseek(ptr noundef nonnull %2, i32 noundef 0, i64 noundef %23)
  %.not14 = icmp eq i32 %24, 0
  br i1 %.not14, label %25, label %._crit_edge

25:                                               ; preds = %8
  %26 = add nsw i32 %.01316, -1
  %27 = getelementptr inbounds i8, ptr %.01217, i64 1
  %28 = icmp ugt i32 %.01316, 1
  br i1 %28, label %8, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %25, %8, %3
  %.0 = phi i32 [ 0, %3 ], [ 0, %25 ], [ -1, %8 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @tf_first_sector(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @tf_fseek(ptr noundef, i32 noundef, i64 noundef) local_unnamed_addr #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, !7, i64 12}
!6 = !{!"struct_TFFILE", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !10, i64 16, !10, i64 18, !7, i64 20, !8, i64 24, !8, i64 25, !8, i64 26, !7, i64 28, !8, i64 32}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!6, !10, i64 18}
!12 = !{!8, !8, i64 0}
!13 = !{!6, !7, i64 20}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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