; 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 }
%struct.file = type { i32, ptr, ptr }

@fatfs_index = external dso_local global i32, align 4
@FreeRTOS_errno = external global i32, align 4
@fatfs_files = external dso_local global [32 x %struct.FIL], align 16

; 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

; Function Attrs: nounwind uwtable
define dso_local i32 @fatfs_open(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %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
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca %struct.FIL, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !9
  store i32 0, ptr @FreeRTOS_errno, align 4, !tbaa !9
  %13 = load i32, ptr @fatfs_index, align 4, !tbaa !9
  %14 = icmp sge i32 %13, 31
  br i1 %14, label %15, label %16

15:                                               ; preds = %2
  store i32 12, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %98

16:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.file, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !11
  store i32 %19, ptr %11, align 4, !tbaa !9
  %20 = load i32, ptr %11, align 4, !tbaa !9
  %21 = and i32 %20, 61440
  %22 = icmp eq i32 %21, 40960
  br i1 %22, label %23, label %24

23:                                               ; preds = %16
  store i32 3, ptr %6, align 4, !tbaa !9
  br label %31

24:                                               ; preds = %16
  %25 = load i32, ptr %11, align 4, !tbaa !9
  %26 = and i32 %25, 61440
  %27 = icmp eq i32 %26, 8192
  br i1 %27, label %28, label %29

28:                                               ; preds = %24
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %30

29:                                               ; preds = %24
  store i32 2, ptr %6, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %29, %28
  br label %31

31:                                               ; preds = %30, %23
  %32 = load i32, ptr %11, align 4, !tbaa !9
  %33 = and i32 %32, 2
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %46

35:                                               ; preds = %31
  %36 = load i32, ptr %11, align 4, !tbaa !9
  %37 = and i32 %36, 64
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %42

39:                                               ; preds = %35
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = or i32 %40, 8
  store i32 %41, ptr %6, align 4, !tbaa !9
  br label %45

42:                                               ; preds = %35
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = or i32 %43, 4
  store i32 %44, ptr %6, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %42, %39
  br label %46

46:                                               ; preds = %45, %31
  call void @llvm.lifetime.start.p0(i64 568, ptr %12) #5
  call void @llvm.memset.p0.i64(ptr align 8 %12, i8 0, i64 568, i1 false)
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.file, ptr %47, i32 0, i32 2
  store ptr %12, ptr %48, align 8, !tbaa !13
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = call ptr @stream_to_fatfs(ptr noundef %49)
  store ptr %50, ptr %7, align 8, !tbaa !5
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = and i32 %53, 255
  %55 = trunc i32 %54 to i8
  %56 = call i32 @f_open(ptr noundef %51, ptr noundef %52, i8 noundef zeroext %55)
  store i32 %56, ptr %8, align 4, !tbaa !9
  %57 = load i32, ptr %8, align 4, !tbaa !9
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %62

59:                                               ; preds = %46
  %60 = load i32, ptr %8, align 4, !tbaa !9
  %61 = call i32 @fatfs_to_errno(i32 noundef %60)
  store i32 %61, ptr @FreeRTOS_errno, align 4, !tbaa !9
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %97

62:                                               ; preds = %46
  %63 = load i32, ptr %11, align 4, !tbaa !9
  %64 = and i32 %63, 256
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %81

66:                                               ; preds = %62
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.FIL, ptr %68, i32 0, i32 0
  %70 = getelementptr inbounds %struct.FFOBJID, ptr %69, i32 0, i32 5
  %71 = load i32, ptr %70, align 8, !tbaa !14
  %72 = call i32 @f_lseek(ptr noundef %67, i32 noundef %71)
  store i32 %72, ptr %8, align 4, !tbaa !9
  %73 = load i32, ptr %8, align 4, !tbaa !9
  %74 = icmp ne i32 %73, 0
  br i1 %74, label %75, label %80

75:                                               ; preds = %66
  %76 = load i32, ptr %8, align 4, !tbaa !9
  %77 = call i32 @fatfs_to_errno(i32 noundef %76)
  store i32 %77, ptr @FreeRTOS_errno, align 4, !tbaa !9
  %78 = load ptr, ptr %7, align 8, !tbaa !5
  %79 = call i32 @f_close(ptr noundef %78)
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %97

80:                                               ; preds = %66
  br label %81

81:                                               ; preds = %80, %62
  %82 = load i32, ptr %9, align 4, !tbaa !9
  %83 = icmp ne i32 %82, 0
  br i1 %83, label %95, label %84

84:                                               ; preds = %81
  %85 = load i32, ptr @fatfs_index, align 4, !tbaa !9
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds [32 x %struct.FIL], ptr @fatfs_files, i64 0, i64 %86
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %87, ptr align 8 %12, i64 568, i1 false), !tbaa.struct !18
  %88 = load i32, ptr @fatfs_index, align 4, !tbaa !9
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [32 x %struct.FIL], ptr @fatfs_files, i64 0, i64 %89
  %91 = load ptr, ptr %4, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.file, ptr %91, i32 0, i32 2
  store ptr %90, ptr %92, align 8, !tbaa !13
  %93 = load i32, ptr @fatfs_index, align 4, !tbaa !9
  %94 = add nsw i32 %93, 1
  store i32 %94, ptr @fatfs_index, align 4, !tbaa !9
  br label %95

95:                                               ; preds = %84, %81
  %96 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %96, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %97

97:                                               ; preds = %95, %75, %59
  call void @llvm.lifetime.end.p0(i64 568, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  br label %98

98:                                               ; preds = %97, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  %99 = load i32, ptr %3, align 4
  ret i32 %99
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

declare i32 @f_open(ptr noundef, ptr noundef, i8 noundef zeroext) #3

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

declare i32 @f_close(ptr noundef) #3

; 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) #4

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { "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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 0}
!12 = !{!"file", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 16}
!14 = !{!15, !10, i64 16}
!15 = !{!"", !16, i64 0, !7, i64 24, !7, i64 25, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !6, i64 48, !7, i64 56}
!16 = !{!"", !6, i64 0, !17, i64 8, !7, i64 10, !7, i64 11, !10, i64 12, !10, i64 16, !10, i64 20}
!17 = !{!"short", !7, i64 0}
!18 = !{i64 0, i64 8, !5, i64 8, i64 2, !19, i64 10, i64 1, !20, i64 11, i64 1, !20, i64 12, i64 4, !9, i64 16, i64 4, !9, i64 20, i64 4, !9, i64 24, i64 1, !20, i64 25, i64 1, !20, i64 28, i64 4, !9, i64 32, i64 4, !9, i64 36, i64 4, !9, i64 40, i64 4, !9, i64 48, i64 8, !5, i64 56, i64 512, !20}
!19 = !{!17, !17, i64 0}
!20 = !{!7, !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"

%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 }

@fatfs_index = external dso_local local_unnamed_addr global i32, align 4
@FreeRTOS_errno = external local_unnamed_addr global i32, align 4
@fatfs_files = external dso_local global [32 x %struct.FIL], align 16

; 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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @fatfs_open(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca %struct.FIL, align 8
  store i32 0, ptr @FreeRTOS_errno, align 4, !tbaa !5
  %4 = load i32, ptr @fatfs_index, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 30
  br i1 %5, label %6, label %7

6:                                                ; preds = %2
  store i32 12, ptr @FreeRTOS_errno, align 4, !tbaa !5
  br label %38

7:                                                ; preds = %2
  %8 = load i32, ptr %0, align 8, !tbaa !9
  %9 = and i32 %8, 61440
  %switch.selectcmp = icmp eq i32 %9, 8192
  %switch.select = select i1 %switch.selectcmp, i8 1, i8 2
  %switch.selectcmp32 = icmp eq i32 %9, 40960
  %switch.select33 = select i1 %switch.selectcmp32, i8 3, i8 %switch.select
  %10 = and i32 %8, 2
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %17, label %11

11:                                               ; preds = %7
  %12 = and i32 %8, 64
  %.not28 = icmp eq i32 %12, 0
  br i1 %.not28, label %15, label %13

13:                                               ; preds = %11
  %14 = or disjoint i8 %switch.select33, 8
  br label %17

15:                                               ; preds = %11
  %16 = or disjoint i8 %switch.select33, 4
  br label %17

17:                                               ; preds = %13, %15, %7
  %.124 = phi i8 [ %14, %13 ], [ %16, %15 ], [ %switch.select33, %7 ]
  call void @llvm.lifetime.start.p0(i64 568, ptr nonnull %3) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(568) %3, i8 0, i64 568, i1 false)
  %18 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr %3, ptr %18, align 8, !tbaa !12
  %19 = call ptr @stream_to_fatfs(ptr noundef nonnull %0)
  %20 = call i32 @f_open(ptr noundef %19, ptr noundef %1, i8 noundef zeroext %.124) #5
  %.not29 = icmp eq i32 %20, 0
  br i1 %.not29, label %23, label %21

21:                                               ; preds = %17
  %22 = call i32 @fatfs_to_errno(i32 noundef %20)
  store i32 %22, ptr @FreeRTOS_errno, align 4, !tbaa !5
  br label %37

23:                                               ; preds = %17
  %24 = and i32 %8, 256
  %.not30 = icmp eq i32 %24, 0
  br i1 %.not30, label %32, label %25

25:                                               ; preds = %23
  %26 = getelementptr inbounds i8, ptr %19, i64 16
  %27 = load i32, ptr %26, align 8, !tbaa !13
  %28 = call i32 @f_lseek(ptr noundef %19, i32 noundef %27) #5
  %.not31 = icmp eq i32 %28, 0
  br i1 %.not31, label %32, label %29

29:                                               ; preds = %25
  %30 = call i32 @fatfs_to_errno(i32 noundef %28)
  store i32 %30, ptr @FreeRTOS_errno, align 4, !tbaa !5
  %31 = call i32 @f_close(ptr noundef nonnull %19) #5
  br label %37

32:                                               ; preds = %23, %25
  %33 = load i32, ptr @fatfs_index, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [32 x %struct.FIL], ptr @fatfs_files, i64 0, i64 %34
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(568) %35, ptr noundef nonnull align 8 dereferenceable(568) %3, i64 568, i1 false), !tbaa.struct !17
  store ptr %35, ptr %18, align 8, !tbaa !12
  %36 = add nsw i32 %33, 1
  store i32 %36, ptr @fatfs_index, align 4, !tbaa !5
  br label %37

37:                                               ; preds = %32, %29, %21
  %.0 = phi i32 [ -1, %21 ], [ -1, %29 ], [ 0, %32 ]
  call void @llvm.lifetime.end.p0(i64 568, ptr nonnull %3) #5
  br label %38

38:                                               ; preds = %37, %6
  %.1 = phi i32 [ -1, %6 ], [ %.0, %37 ]
  ret i32 %.1
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

declare i32 @f_open(ptr noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #3

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

declare i32 @f_close(ptr noundef) local_unnamed_addr #3

; 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) #4

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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { "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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 0}
!10 = !{!"file", !6, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 16}
!13 = !{!14, !6, i64 16}
!14 = !{!"", !15, i64 0, !7, i64 24, !7, i64 25, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !11, i64 48, !7, i64 56}
!15 = !{!"", !11, i64 0, !16, i64 8, !7, i64 10, !7, i64 11, !6, i64 12, !6, i64 16, !6, i64 20}
!16 = !{!"short", !7, i64 0}
!17 = !{i64 0, i64 8, !18, i64 8, i64 2, !19, i64 10, i64 1, !20, i64 11, i64 1, !20, i64 12, i64 4, !5, i64 16, i64 4, !5, i64 20, i64 4, !5, i64 24, i64 1, !20, i64 25, i64 1, !20, i64 28, i64 4, !5, i64 32, i64 4, !5, i64 36, i64 4, !5, i64 40, i64 4, !5, i64 48, i64 8, !18, i64 56, i64 512, !20}
!18 = !{!11, !11, i64 0}
!19 = !{!16, !16, i64 0}
!20 = !{!7, !7, i64 0}


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