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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @tf_fwrite(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %14 = load ptr, ptr %9, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.struct_TFFILE, ptr %14, i32 0, i32 7
  %16 = load i8, ptr %15, align 4, !tbaa !11
  %17 = zext i8 %16 to i32
  %18 = or i32 %17, 1
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %15, align 4, !tbaa !11
  br label %20

20:                                               ; preds = %107, %4
  %21 = load i32, ptr %8, align 4, !tbaa !9
  %22 = icmp sgt i32 %21, 0
  br i1 %22, label %23, label %110

23:                                               ; preds = %20
  %24 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %24, ptr %10, align 4, !tbaa !9
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.struct_TFFILE, ptr %25, i32 0, i32 7
  %27 = load i8, ptr %26, align 4, !tbaa !11
  %28 = zext i8 %27 to i32
  %29 = or i32 %28, 4
  %30 = trunc i32 %29 to i8
  store i8 %30, ptr %26, align 4, !tbaa !11
  br label %31

31:                                               ; preds = %99, %23
  %32 = load i32, ptr %10, align 4, !tbaa !9
  %33 = icmp sgt i32 %32, 0
  br i1 %33, label %34, label %107

34:                                               ; preds = %31
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.struct_TFFILE, ptr %35, i32 0, i32 3
  %37 = load i32, ptr %36, align 4, !tbaa !14
  %38 = call i32 @tf_first_sector(i32 noundef %37)
  %39 = load ptr, ptr %9, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.struct_TFFILE, ptr %39, i32 0, i32 5
  %41 = load i16, ptr %40, align 2, !tbaa !15
  %42 = sext i16 %41 to i32
  %43 = sdiv i32 %42, 512
  %44 = add i32 %38, %43
  %45 = call i32 @tf_fetch(i32 noundef %44)
  %46 = load ptr, ptr %9, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.struct_TFFILE, ptr %46, i32 0, i32 5
  %48 = load i16, ptr %47, align 2, !tbaa !15
  %49 = sext i16 %48 to i32
  %50 = srem i32 %49, 512
  store i32 %50, ptr %11, align 4, !tbaa !9
  %51 = load i32, ptr %10, align 4, !tbaa !9
  %52 = icmp slt i32 %51, 512
  br i1 %52, label %53, label %55

53:                                               ; preds = %34
  %54 = load i32, ptr %10, align 4, !tbaa !9
  br label %56

55:                                               ; preds = %34
  br label %56

56:                                               ; preds = %55, %53
  %57 = phi i32 [ %54, %53 ], [ 512, %55 ]
  store i32 %57, ptr %12, align 4, !tbaa !9
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [512 x i8], ptr getelementptr inbounds (%struct.struct_tfinfo, ptr @tf_info, i32 0, i32 8), i64 0, i64 %59
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = load i32, ptr %12, align 4, !tbaa !9
  %63 = sext i32 %62 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %60, ptr align 1 %61, i64 %63, i1 false)
  %64 = load i8, ptr getelementptr inbounds (%struct.struct_tfinfo, ptr @tf_info, i32 0, i32 6), align 4, !tbaa !16
  %65 = zext i8 %64 to i32
  %66 = or i32 %65, 1
  %67 = trunc i32 %66 to i8
  store i8 %67, ptr getelementptr inbounds (%struct.struct_tfinfo, ptr @tf_info, i32 0, i32 6), align 4, !tbaa !16
  %68 = load ptr, ptr %9, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.struct_TFFILE, ptr %68, i32 0, i32 6
  %70 = load i32, ptr %69, align 4, !tbaa !18
  %71 = load i32, ptr %12, align 4, !tbaa !9
  %72 = add i32 %70, %71
  %73 = load ptr, ptr %9, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.struct_TFFILE, ptr %73, i32 0, i32 10
  %75 = load i32, ptr %74, align 4, !tbaa !19
  %76 = icmp ugt i32 %72, %75
  br i1 %76, label %77, label %88

77:                                               ; preds = %56
  %78 = load i32, ptr %12, align 4, !tbaa !9
  %79 = load ptr, ptr %9, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.struct_TFFILE, ptr %79, i32 0, i32 6
  %81 = load i32, ptr %80, align 4, !tbaa !18
  %82 = urem i32 %81, 512
  %83 = sub i32 %78, %82
  %84 = load ptr, ptr %9, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.struct_TFFILE, ptr %84, i32 0, i32 10
  %86 = load i32, ptr %85, align 4, !tbaa !19
  %87 = add i32 %86, %83
  store i32 %87, ptr %85, align 4, !tbaa !19
  br label %88

88:                                               ; preds = %77, %56
  %89 = load ptr, ptr %9, align 8, !tbaa !5
  %90 = load ptr, ptr %9, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.struct_TFFILE, ptr %90, i32 0, i32 6
  %92 = load i32, ptr %91, align 4, !tbaa !18
  %93 = load i32, ptr %12, align 4, !tbaa !9
  %94 = add i32 %92, %93
  %95 = zext i32 %94 to i64
  %96 = call i32 @tf_unsafe_fseek(ptr noundef %89, i32 noundef 0, i64 noundef %95)
  %97 = icmp ne i32 %96, 0
  br i1 %97, label %98, label %99

98:                                               ; preds = %88
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %114

99:                                               ; preds = %88
  %100 = load i32, ptr %12, align 4, !tbaa !9
  %101 = load i32, ptr %10, align 4, !tbaa !9
  %102 = sub nsw i32 %101, %100
  store i32 %102, ptr %10, align 4, !tbaa !9
  %103 = load i32, ptr %12, align 4, !tbaa !9
  %104 = load ptr, ptr %6, align 8, !tbaa !5
  %105 = sext i32 %103 to i64
  %106 = getelementptr inbounds i8, ptr %104, i64 %105
  store ptr %106, ptr %6, align 8, !tbaa !5
  br label %31, !llvm.loop !20

107:                                              ; preds = %31
  %108 = load i32, ptr %8, align 4, !tbaa !9
  %109 = add nsw i32 %108, -1
  store i32 %109, ptr %8, align 4, !tbaa !9
  br label %20, !llvm.loop !22

110:                                              ; preds = %20
  %111 = load i32, ptr %7, align 4, !tbaa !9
  %112 = load i32, ptr %10, align 4, !tbaa !9
  %113 = sub nsw i32 %111, %112
  store i32 %113, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %114

114:                                              ; preds = %110, %98
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %115 = load i32, ptr %5, align 4
  ret i32 %115
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @tf_unsafe_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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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, !7, i64 24}
!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, !10, i64 12}
!15 = !{!12, !13, i64 18}
!16 = !{!17, !7, i64 20}
!17 = !{!"struct_tfinfo", !7, i64 0, !7, i64 1, !10, i64 4, !10, i64 8, !13, i64 12, !10, i64 16, !7, i64 20, !10, i64 24, !7, i64 28}
!18 = !{!12, !10, i64 20}
!19 = !{!12, !10, i64 28}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}


; 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
declare dso_local i32 @tf_first_sector(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @tf_fwrite(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %3, i64 24
  %6 = load i8, ptr %5, align 4, !tbaa !5
  %7 = or i8 %6, 1
  store i8 %7, ptr %5, align 4, !tbaa !5
  %8 = icmp sgt i32 %2, 0
  br i1 %8, label %.lr.ph43, label %._crit_edge44

.lr.ph43:                                         ; preds = %4
  %9 = icmp sgt i32 %1, 0
  %10 = getelementptr inbounds i8, ptr %3, i64 12
  %11 = getelementptr inbounds i8, ptr %3, i64 18
  %12 = getelementptr inbounds i8, ptr %3, i64 20
  %13 = getelementptr inbounds i8, ptr %3, i64 28
  br i1 %9, label %.lr.ph.us, label %.lr.ph43.split

.lr.ph.us:                                        ; preds = %.lr.ph43, %._crit_edge.us
  %.02941.us = phi ptr [ %45, %._crit_edge.us ], [ %0, %.lr.ph43 ]
  %.03240.us = phi i32 [ %47, %._crit_edge.us ], [ %2, %.lr.ph43 ]
  %14 = load i8, ptr %5, align 4, !tbaa !5
  %15 = or i8 %14, 4
  store i8 %15, ptr %5, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %.lr.ph.us, %43
  %.138.us = phi ptr [ %.02941.us, %.lr.ph.us ], [ %45, %43 ]
  %.13137.us = phi i32 [ %1, %.lr.ph.us ], [ %44, %43 ]
  %17 = load i32, ptr %10, align 4, !tbaa !11
  %18 = tail call i32 @tf_first_sector(i32 noundef %17)
  %19 = load i16, ptr %11, align 2, !tbaa !12
  %20 = sdiv i16 %19, 512
  %21 = sext i16 %20 to i32
  %22 = add i32 %18, %21
  %23 = tail call i32 @tf_fetch(i32 noundef %22)
  %24 = load i16, ptr %11, align 2, !tbaa !12
  %25 = srem i16 %24, 512
  %26 = tail call i32 @llvm.umin.i32(i32 %.13137.us, i32 512)
  %27 = sext i16 %25 to i64
  %28 = getelementptr inbounds [512 x i8], ptr getelementptr inbounds (i8, ptr @tf_info, i64 28), i64 0, i64 %27
  %29 = zext nneg i32 %26 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %28, ptr align 1 %.138.us, i64 %29, i1 false)
  %30 = load i8, ptr getelementptr inbounds (i8, ptr @tf_info, i64 20), align 4, !tbaa !13
  %31 = or i8 %30, 1
  store i8 %31, ptr getelementptr inbounds (i8, ptr @tf_info, i64 20), align 4, !tbaa !13
  %32 = load i32, ptr %12, align 4, !tbaa !15
  %33 = add i32 %32, %26
  %34 = load i32, ptr %13, align 4, !tbaa !16
  %35 = icmp ugt i32 %33, %34
  br i1 %35, label %36, label %40

36:                                               ; preds = %16
  %37 = and i32 %32, 511
  %38 = sub nsw i32 %26, %37
  %39 = add i32 %38, %34
  store i32 %39, ptr %13, align 4, !tbaa !16
  br label %40

40:                                               ; preds = %36, %16
  %41 = zext i32 %33 to i64
  %42 = tail call i32 @tf_unsafe_fseek(ptr noundef nonnull %3, i32 noundef 0, i64 noundef %41)
  %.not.us = icmp eq i32 %42, 0
  br i1 %.not.us, label %43, label %.loopexit

43:                                               ; preds = %40
  %44 = sub nsw i32 %.13137.us, %26
  %45 = getelementptr inbounds i8, ptr %.138.us, i64 %29
  %46 = icmp sgt i32 %44, 0
  br i1 %46, label %16, label %._crit_edge.us, !llvm.loop !17

._crit_edge.us:                                   ; preds = %43
  %47 = add nsw i32 %.03240.us, -1
  %48 = icmp sgt i32 %.03240.us, 1
  br i1 %48, label %.lr.ph.us, label %._crit_edge44, !llvm.loop !19

.lr.ph43.split:                                   ; preds = %.lr.ph43
  %49 = or i8 %6, 5
  store i8 %49, ptr %5, align 4, !tbaa !5
  br label %._crit_edge44

._crit_edge44:                                    ; preds = %._crit_edge.us, %.lr.ph43.split, %4
  %.030.lcssa = phi i32 [ undef, %4 ], [ %1, %.lr.ph43.split ], [ %44, %._crit_edge.us ]
  %50 = sub nsw i32 %1, %.030.lcssa
  br label %.loopexit

.loopexit:                                        ; preds = %40, %._crit_edge44
  %.0 = phi i32 [ %50, %._crit_edge44 ], [ -1, %40 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @tf_unsafe_fseek(ptr noundef, i32 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !8, i64 24}
!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, !7, i64 12}
!12 = !{!6, !10, i64 18}
!13 = !{!14, !8, i64 20}
!14 = !{!"struct_tfinfo", !8, i64 0, !8, i64 1, !7, i64 4, !7, i64 8, !10, i64 12, !7, i64 16, !8, i64 20, !7, i64 24, !8, i64 28}
!15 = !{!6, !7, i64 20}
!16 = !{!6, !7, i64 28}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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