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

@tf_file_handles = external dso_local global [5 x %struct.struct_TFFILE], align 16

; 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
define dso_local ptr @tf_get_free_handle() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %23, %0
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 5
  br i1 %7, label %8, label %26

8:                                                ; preds = %5
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [5 x %struct.struct_TFFILE], ptr @tf_file_handles, i64 0, i64 %10
  store ptr %11, ptr %3, align 8, !tbaa !9
  %12 = load ptr, ptr %3, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct.struct_TFFILE, ptr %12, i32 0, i32 7
  %14 = load i8, ptr %13, align 4, !tbaa !11
  %15 = zext i8 %14 to i32
  %16 = and i32 %15, 2
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %8
  br label %23

19:                                               ; preds = %8
  %20 = load ptr, ptr %3, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.struct_TFFILE, ptr %20, i32 0, i32 7
  store i8 2, ptr %21, align 4, !tbaa !11
  %22 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %22, ptr %1, align 8
  store i32 1, ptr %4, align 4
  br label %27

23:                                               ; preds = %18
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %2, align 4, !tbaa !5
  br label %5, !llvm.loop !14

26:                                               ; preds = %5
  store ptr null, ptr %1, align 8
  store i32 1, ptr %4, align 4
  br label %27

27:                                               ; preds = %26, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  %28 = load ptr, ptr %1, align 8
  ret ptr %28
}

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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 24}
!12 = !{!"struct_TFFILE", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !13, i64 16, !13, i64 18, !6, i64 20, !7, i64 24, !7, i64 25, !7, i64 26, !6, i64 28, !7, i64 32}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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_TFFILE = type { i32, i32, i32, i32, i16, i16, i32, i8, i8, i8, i32, [256 x i8] }

@tf_file_handles = external dso_local global [5 x %struct.struct_TFFILE], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: write, inaccessiblemem: none) uwtable
define dso_local ptr @tf_get_free_handle() local_unnamed_addr #0 {
  %1 = load i8, ptr getelementptr inbounds (i8, ptr @tf_file_handles, i64 24), align 8, !tbaa !5
  %2 = and i8 %1, 2
  %.not = icmp eq i8 %2, 0
  br i1 %.not, label %15, label %3

3:                                                ; preds = %0
  %4 = load i8, ptr getelementptr inbounds (i8, ptr @tf_file_handles, i64 312), align 8, !tbaa !5
  %5 = and i8 %4, 2
  %.not.1 = icmp eq i8 %5, 0
  br i1 %.not.1, label %15, label %6

6:                                                ; preds = %3
  %7 = load i8, ptr getelementptr inbounds (i8, ptr @tf_file_handles, i64 600), align 8, !tbaa !5
  %8 = and i8 %7, 2
  %.not.2 = icmp eq i8 %8, 0
  br i1 %.not.2, label %15, label %9

9:                                                ; preds = %6
  %10 = load i8, ptr getelementptr inbounds (i8, ptr @tf_file_handles, i64 888), align 8, !tbaa !5
  %11 = and i8 %10, 2
  %.not.3 = icmp eq i8 %11, 0
  br i1 %.not.3, label %15, label %12

12:                                               ; preds = %9
  %13 = load i8, ptr getelementptr inbounds (i8, ptr @tf_file_handles, i64 1176), align 8, !tbaa !5
  %14 = and i8 %13, 2
  %.not.4 = icmp eq i8 %14, 0
  br i1 %.not.4, label %15, label %.loopexit

15:                                               ; preds = %12, %9, %6, %3, %0
  %.lcssa = phi ptr [ @tf_file_handles, %0 ], [ getelementptr inbounds (i8, ptr @tf_file_handles, i64 288), %3 ], [ getelementptr inbounds (i8, ptr @tf_file_handles, i64 576), %6 ], [ getelementptr inbounds (i8, ptr @tf_file_handles, i64 864), %9 ], [ getelementptr inbounds (i8, ptr @tf_file_handles, i64 1152), %12 ]
  %16 = getelementptr inbounds i8, ptr %.lcssa, i64 24
  store i8 2, ptr %16, align 4, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %12, %15
  %.0 = phi ptr [ %.lcssa, %15 ], [ null, %12 ]
  ret ptr %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, argmem: write, inaccessiblemem: none) 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, !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}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass