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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @tf_unsafe_fseek(ptr noundef %0, i32 noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = load i64, ptr %7, align 8, !tbaa !11
  %16 = add nsw i64 %14, %15
  store i64 %16, ptr %9, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %17 = load i8, ptr @tf_info, align 4, !tbaa !13
  %18 = zext i8 %17 to i32
  %19 = icmp ne i32 %18, 0
  %20 = zext i1 %19 to i64
  %21 = select i1 %19, i32 268435448, i32 65528
  store i32 %21, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %22 = load i64, ptr %9, align 8, !tbaa !11
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.struct_TFFILE, ptr %23, i32 0, i32 10
  %25 = load i32, ptr %24, align 4, !tbaa !16
  %26 = zext i32 %25 to i64
  %27 = icmp sgt i64 %22, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %3
  store i32 1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %146

29:                                               ; preds = %3
  %30 = load i64, ptr %9, align 8, !tbaa !11
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.struct_TFFILE, ptr %31, i32 0, i32 10
  %33 = load i32, ptr %32, align 4, !tbaa !16
  %34 = zext i32 %33 to i64
  %35 = icmp eq i64 %30, %34
  br i1 %35, label %36, label %47

36:                                               ; preds = %29
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.struct_TFFILE, ptr %37, i32 0, i32 10
  %39 = load i32, ptr %38, align 4, !tbaa !16
  %40 = add i32 %39, 1
  store i32 %40, ptr %38, align 4, !tbaa !16
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.struct_TFFILE, ptr %41, i32 0, i32 7
  %43 = load i8, ptr %42, align 4, !tbaa !18
  %44 = zext i8 %43 to i32
  %45 = or i32 %44, 4
  %46 = trunc i32 %45 to i8
  store i8 %46, ptr %42, align 4, !tbaa !18
  br label %47

47:                                               ; preds = %36, %29
  %48 = load i64, ptr %9, align 8, !tbaa !11
  %49 = load i8, ptr getelementptr inbounds (%struct.struct_tfinfo, ptr @tf_info, i32 0, i32 1), align 1, !tbaa !19
  %50 = zext i8 %49 to i32
  %51 = mul nsw i32 %50, 512
  %52 = sext i32 %51 to i64
  %53 = sdiv i64 %48, %52
  %54 = trunc i64 %53 to i32
  store i32 %54, ptr %8, align 4, !tbaa !9
  %55 = load i32, ptr %8, align 4, !tbaa !9
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.struct_TFFILE, ptr %56, i32 0, i32 2
  %58 = load i32, ptr %57, align 4, !tbaa !20
  %59 = icmp ne i32 %55, %58
  br i1 %59, label %60, label %132

60:                                               ; preds = %47
  %61 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %61, ptr %11, align 4, !tbaa !9
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.struct_TFFILE, ptr %63, i32 0, i32 2
  %65 = load i32, ptr %64, align 4, !tbaa !20
  %66 = icmp ugt i32 %62, %65
  br i1 %66, label %67, label %73

67:                                               ; preds = %60
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.struct_TFFILE, ptr %68, i32 0, i32 2
  %70 = load i32, ptr %69, align 4, !tbaa !20
  %71 = load i32, ptr %8, align 4, !tbaa !9
  %72 = sub i32 %71, %70
  store i32 %72, ptr %8, align 4, !tbaa !9
  br label %79

73:                                               ; preds = %60
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.struct_TFFILE, ptr %74, i32 0, i32 1
  %76 = load i32, ptr %75, align 4, !tbaa !21
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.struct_TFFILE, ptr %77, i32 0, i32 3
  store i32 %76, ptr %78, align 4, !tbaa !22
  br label %79

79:                                               ; preds = %73, %67
  %80 = load i32, ptr %11, align 4, !tbaa !9
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.struct_TFFILE, ptr %81, i32 0, i32 2
  store i32 %80, ptr %82, align 4, !tbaa !20
  br label %83

83:                                               ; preds = %130, %79
  %84 = load i32, ptr %8, align 4, !tbaa !9
  %85 = icmp ugt i32 %84, 0
  br i1 %85, label %86, label %131

86:                                               ; preds = %83
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.struct_TFFILE, ptr %87, i32 0, i32 3
  %89 = load i32, ptr %88, align 4, !tbaa !22
  %90 = call i32 @tf_get_fat_entry(i32 noundef %89)
  store i32 %90, ptr %11, align 4, !tbaa !9
  %91 = load i32, ptr %11, align 4, !tbaa !9
  %92 = load i32, ptr %10, align 4, !tbaa !9
  %93 = icmp ne i32 268435455, %92
  %94 = zext i1 %93 to i32
  %95 = and i32 %91, %94
  %96 = icmp ne i32 %95, 0
  br i1 %96, label %97, label %101

97:                                               ; preds = %86
  %98 = load i32, ptr %11, align 4, !tbaa !9
  %99 = load ptr, ptr %5, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.struct_TFFILE, ptr %99, i32 0, i32 3
  store i32 %98, ptr %100, align 4, !tbaa !22
  br label %117

101:                                              ; preds = %86
  %102 = load ptr, ptr %5, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.struct_TFFILE, ptr %102, i32 0, i32 3
  %104 = load i32, ptr %103, align 4, !tbaa !22
  %105 = call i32 @tf_find_free_cluster_from(i32 noundef %104)
  store i32 %105, ptr %11, align 4, !tbaa !9
  %106 = load ptr, ptr %5, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.struct_TFFILE, ptr %106, i32 0, i32 3
  %108 = load i32, ptr %107, align 4, !tbaa !22
  %109 = load i32, ptr %11, align 4, !tbaa !9
  %110 = call i32 @tf_set_fat_entry(i32 noundef %108, i32 noundef %109)
  %111 = load i32, ptr %11, align 4, !tbaa !9
  %112 = load i32, ptr %10, align 4, !tbaa !9
  %113 = call i32 @tf_set_fat_entry(i32 noundef %111, i32 noundef %112)
  %114 = load i32, ptr %11, align 4, !tbaa !9
  %115 = load ptr, ptr %5, align 8, !tbaa !5
  %116 = getelementptr inbounds %struct.struct_TFFILE, ptr %115, i32 0, i32 3
  store i32 %114, ptr %116, align 4, !tbaa !22
  br label %117

117:                                              ; preds = %101, %97
  %118 = load i32, ptr %8, align 4, !tbaa !9
  %119 = add i32 %118, -1
  store i32 %119, ptr %8, align 4, !tbaa !9
  %120 = load ptr, ptr %5, align 8, !tbaa !5
  %121 = getelementptr inbounds %struct.struct_TFFILE, ptr %120, i32 0, i32 3
  %122 = load i32, ptr %121, align 4, !tbaa !22
  %123 = load i32, ptr %10, align 4, !tbaa !9
  %124 = icmp uge i32 %122, %123
  br i1 %124, label %125, label %130

125:                                              ; preds = %117
  %126 = load i32, ptr %8, align 4, !tbaa !9
  %127 = icmp ugt i32 %126, 0
  br i1 %127, label %128, label %129

128:                                              ; preds = %125
  store i32 1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %146

129:                                              ; preds = %125
  br label %130

130:                                              ; preds = %129, %117
  br label %83, !llvm.loop !23

131:                                              ; preds = %83
  br label %132

132:                                              ; preds = %131, %47
  %133 = load i64, ptr %9, align 8, !tbaa !11
  %134 = load i8, ptr getelementptr inbounds (%struct.struct_tfinfo, ptr @tf_info, i32 0, i32 1), align 1, !tbaa !19
  %135 = zext i8 %134 to i32
  %136 = mul nsw i32 %135, 512
  %137 = sext i32 %136 to i64
  %138 = srem i64 %133, %137
  %139 = trunc i64 %138 to i16
  %140 = load ptr, ptr %5, align 8, !tbaa !5
  %141 = getelementptr inbounds %struct.struct_TFFILE, ptr %140, i32 0, i32 5
  store i16 %139, ptr %141, align 2, !tbaa !25
  %142 = load i64, ptr %9, align 8, !tbaa !11
  %143 = trunc i64 %142 to i32
  %144 = load ptr, ptr %5, align 8, !tbaa !5
  %145 = getelementptr inbounds %struct.struct_TFFILE, ptr %144, i32 0, i32 6
  store i32 %143, ptr %145, align 4, !tbaa !26
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %146

146:                                              ; preds = %132, %128, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %147 = load i32, ptr %4, align 4
  ret i32 %147
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @tf_find_free_cluster_from(i32 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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !7, i64 0}
!14 = !{!"struct_tfinfo", !7, i64 0, !7, i64 1, !10, i64 4, !10, i64 8, !15, i64 12, !10, i64 16, !7, i64 20, !10, i64 24, !7, i64 28}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !10, i64 28}
!17 = !{!"struct_TFFILE", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !15, i64 16, !15, i64 18, !10, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !10, i64 28, !7, i64 32}
!18 = !{!17, !7, i64 24}
!19 = !{!14, !7, i64 1}
!20 = !{!17, !10, i64 8}
!21 = !{!17, !10, i64 4}
!22 = !{!17, !10, i64 12}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!17, !15, i64 18}
!26 = !{!17, !10, i64 20}


; 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_get_fat_entry(i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @tf_unsafe_fseek(ptr nocapture noundef %0, i32 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %1 to i64
  %5 = add nsw i64 %4, %2
  %6 = load i8, ptr @tf_info, align 4, !tbaa !5
  %.not = icmp eq i8 %6, 0
  %7 = select i1 %.not, i32 65528, i32 268435448
  %8 = getelementptr inbounds i8, ptr %0, i64 28
  %9 = load i32, ptr %8, align 4, !tbaa !11
  %10 = zext i32 %9 to i64
  %11 = icmp sgt i64 %5, %10
  br i1 %11, label %.loopexit50, label %12

12:                                               ; preds = %3
  %13 = icmp eq i64 %5, %10
  br i1 %13, label %14, label %19

14:                                               ; preds = %12
  %15 = add i32 %9, 1
  store i32 %15, ptr %8, align 4, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %0, i64 24
  %17 = load i8, ptr %16, align 4, !tbaa !13
  %18 = or i8 %17, 4
  store i8 %18, ptr %16, align 4, !tbaa !13
  br label %19

19:                                               ; preds = %14, %12
  %20 = load i8, ptr getelementptr inbounds (i8, ptr @tf_info, i64 1), align 1, !tbaa !14
  %21 = zext i8 %20 to i64
  %22 = shl nuw nsw i64 %21, 9
  %23 = sdiv i64 %5, %22
  %24 = trunc i64 %23 to i32
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  %26 = load i32, ptr %25, align 4, !tbaa !15
  %.not47 = icmp eq i32 %26, %24
  br i1 %.not47, label %.loopexit, label %27

27:                                               ; preds = %19
  %28 = icmp ult i32 %26, %24
  br i1 %28, label %29, label %31

29:                                               ; preds = %27
  %30 = sub i32 %24, %26
  br label %35

31:                                               ; preds = %27
  %32 = getelementptr inbounds i8, ptr %0, i64 4
  %33 = load i32, ptr %32, align 4, !tbaa !16
  %34 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %33, ptr %34, align 4, !tbaa !17
  br label %35

35:                                               ; preds = %31, %29
  %.042 = phi i32 [ %30, %29 ], [ %24, %31 ]
  store i32 %24, ptr %25, align 4, !tbaa !15
  %36 = getelementptr inbounds i8, ptr %0, i64 12
  br label %37

37:                                               ; preds = %48, %35
  %.1 = phi i32 [ %.042, %35 ], [ %49, %48 ]
  %.not48 = icmp eq i32 %.1, 0
  br i1 %.not48, label %.loopexit.loopexit, label %38

38:                                               ; preds = %37
  %39 = load i32, ptr %36, align 4, !tbaa !17
  %40 = tail call i32 @tf_get_fat_entry(i32 noundef %39)
  %41 = and i32 %40, 1
  %.not49 = icmp eq i32 %41, 0
  br i1 %.not49, label %42, label %48

42:                                               ; preds = %38
  %43 = load i32, ptr %36, align 4, !tbaa !17
  %44 = tail call i32 @tf_find_free_cluster_from(i32 noundef %43)
  %45 = load i32, ptr %36, align 4, !tbaa !17
  %46 = tail call i32 @tf_set_fat_entry(i32 noundef %45, i32 noundef %44)
  %47 = tail call i32 @tf_set_fat_entry(i32 noundef %44, i32 noundef %7)
  br label %48

48:                                               ; preds = %38, %42
  %storemerge = phi i32 [ %44, %42 ], [ %40, %38 ]
  store i32 %storemerge, ptr %36, align 4, !tbaa !17
  %49 = add i32 %.1, -1
  %50 = icmp uge i32 %storemerge, %7
  %51 = icmp ne i32 %49, 0
  %or.cond = and i1 %51, %50
  br i1 %or.cond, label %.loopexit50, label %37, !llvm.loop !18

.loopexit.loopexit:                               ; preds = %37
  %.pre = load i8, ptr getelementptr inbounds (i8, ptr @tf_info, i64 1), align 1, !tbaa !14
  %.pre51 = zext i8 %.pre to i64
  %.pre52 = shl nuw nsw i64 %.pre51, 9
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %19
  %.pre-phi53 = phi i64 [ %.pre52, %.loopexit.loopexit ], [ %22, %19 ]
  %52 = srem i64 %5, %.pre-phi53
  %53 = trunc i64 %52 to i16
  %54 = getelementptr inbounds i8, ptr %0, i64 18
  store i16 %53, ptr %54, align 2, !tbaa !20
  %55 = trunc i64 %5 to i32
  %56 = getelementptr inbounds i8, ptr %0, i64 20
  store i32 %55, ptr %56, align 4, !tbaa !21
  br label %.loopexit50

.loopexit50:                                      ; preds = %48, %3, %.loopexit
  %.0 = phi i32 [ 0, %.loopexit ], [ 1, %3 ], [ 1, %48 ]
  ret i32 %.0
}

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


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