; LLVM IR from ctlst-tech/catpilot :: os/fatfs/fsa (fatfs)

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

@FreeRTOS_errno = external global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local ptr @stream_to_fatfs(ptr noundef) #0

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare i32 @f_lseek(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @fatfs_lseek(ptr noundef %0, i64 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr @FreeRTOS_errno, align 4, !tbaa !11
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %40

14:                                               ; preds = %3
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = call ptr @stream_to_fatfs(ptr noundef %15)
  store ptr %16, ptr %8, align 8, !tbaa !5
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %20

19:                                               ; preds = %14
  store i32 9, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %40

20:                                               ; preds = %14
  %21 = load i32, ptr %7, align 4, !tbaa !11
  switch i32 %21, label %28 [
    i32 0, label %29
    i32 1, label %29
    i32 2, label %22
  ]

22:                                               ; preds = %20
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.FIL, ptr %23, i32 0, i32 0
  %25 = getelementptr inbounds %struct.FFOBJID, ptr %24, i32 0, i32 5
  %26 = load i32, ptr %25, align 8, !tbaa !13
  %27 = zext i32 %26 to i64
  store i64 %27, ptr %6, align 8, !tbaa !9
  br label %29

28:                                               ; preds = %20
  br label %29

29:                                               ; preds = %28, %22, %20, %20
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = load i64, ptr %6, align 8, !tbaa !9
  %32 = trunc i64 %31 to i32
  %33 = call i32 @f_lseek(ptr noundef %30, i32 noundef %32)
  store i32 %33, ptr %9, align 4, !tbaa !11
  %34 = load i32, ptr %9, align 4, !tbaa !11
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %39

36:                                               ; preds = %29
  %37 = load i32, ptr %9, align 4, !tbaa !11
  %38 = call i32 @fatfs_to_errno(i32 noundef %37)
  store i32 %38, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %40

39:                                               ; preds = %29
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %40

40:                                               ; preds = %39, %36, %19, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %41 = load i32, ptr %4, align 4
  ret i32 %41
}

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 16}
!14 = !{!"", !15, i64 0, !7, i64 24, !7, i64 25, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !6, i64 48, !7, i64 56}
!15 = !{!"", !6, i64 0, !16, i64 8, !7, i64 10, !7, i64 11, !12, i64 12, !12, i64 16, !12, i64 20}
!16 = !{!"short", !7, i64 0}


; 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"

@FreeRTOS_errno = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local ptr @stream_to_fatfs(ptr noundef) local_unnamed_addr #0

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

declare i32 @f_lseek(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @fatfs_lseek(ptr noundef %0, i64 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  store i32 0, ptr @FreeRTOS_errno, align 4, !tbaa !5
  %4 = icmp eq ptr %0, null
  br i1 %4, label %18, label %5

5:                                                ; preds = %3
  %6 = tail call ptr @stream_to_fatfs(ptr noundef nonnull %0)
  %7 = icmp eq ptr %6, null
  br i1 %7, label %.sink.split, label %8

8:                                                ; preds = %5
  %cond = icmp eq i32 %2, 2
  br i1 %cond, label %9, label %13

9:                                                ; preds = %8
  %10 = getelementptr inbounds i8, ptr %6, i64 16
  %11 = load i32, ptr %10, align 8, !tbaa !9
  %12 = zext i32 %11 to i64
  br label %13

13:                                               ; preds = %8, %9
  %.09 = phi i64 [ %12, %9 ], [ %1, %8 ]
  %14 = trunc i64 %.09 to i32
  %15 = tail call i32 @f_lseek(ptr noundef nonnull %6, i32 noundef %14) #2
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %18, label %16

16:                                               ; preds = %13
  %17 = tail call i32 @fatfs_to_errno(i32 noundef %15)
  br label %.sink.split

.sink.split:                                      ; preds = %5, %16
  %.sink = phi i32 [ %17, %16 ], [ 9, %5 ]
  store i32 %.sink, ptr @FreeRTOS_errno, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %.sink.split, %13, %3
  %.0 = phi i32 [ -1, %3 ], [ 0, %13 ], [ -1, %.sink.split ]
  ret i32 %.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" }
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 = { 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, !6, i64 16}
!10 = !{!"", !11, i64 0, !7, i64 24, !7, i64 25, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !12, i64 48, !7, i64 56}
!11 = !{!"", !12, i64 0, !13, i64 8, !7, i64 10, !7, i64 11, !6, i64 12, !6, i64 16, !6, i64 20}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"short", !7, i64 0}


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