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

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

@FatFs = external hidden global [1 x ptr], align 8
@SysLock = external hidden global i8, align 1

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

17:                                               ; preds = %3
  store i32 11, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %76

18:                                               ; preds = %3
  %19 = load i32, ptr %9, align 4, !tbaa !10
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1 x ptr], ptr @FatFs, i64 0, i64 %20
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  store ptr %22, ptr %8, align 8, !tbaa !5
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %33

25:                                               ; preds = %18
  %26 = load i32, ptr %9, align 4, !tbaa !10
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1 x ptr], ptr @FatFs, i64 0, i64 %27
  store ptr null, ptr %28, align 8, !tbaa !5
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  call void @clear_share(ptr noundef %29)
  %30 = load i32, ptr %9, align 4, !tbaa !10
  call void @ff_mutex_delete(i32 noundef %30)
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.FATFS, ptr %31, i32 0, i32 0
  store i8 0, ptr %32, align 8, !tbaa !12
  br label %33

33:                                               ; preds = %25, %18
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = icmp ne ptr %34, null
  br i1 %35, label %36, label %66

36:                                               ; preds = %33
  %37 = load i32, ptr %9, align 4, !tbaa !10
  %38 = trunc i32 %37 to i8
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.FATFS, ptr %39, i32 0, i32 1
  store i8 %38, ptr %40, align 1, !tbaa !15
  %41 = load i32, ptr %9, align 4, !tbaa !10
  %42 = trunc i32 %41 to i8
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.FATFS, ptr %43, i32 0, i32 2
  store i8 %42, ptr %44, align 2, !tbaa !16
  %45 = load i32, ptr %9, align 4, !tbaa !10
  %46 = call i32 @ff_mutex_create(i32 noundef %45)
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %49, label %48

48:                                               ; preds = %36
  store i32 2, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %76

49:                                               ; preds = %36
  %50 = load i8, ptr @SysLock, align 1, !tbaa !9
  %51 = zext i8 %50 to i32
  %52 = icmp eq i32 %51, 0
  br i1 %52, label %53, label %59

53:                                               ; preds = %49
  %54 = call i32 @ff_mutex_create(i32 noundef 1)
  %55 = icmp ne i32 %54, 0
  br i1 %55, label %58, label %56

56:                                               ; preds = %53
  %57 = load i32, ptr %9, align 4, !tbaa !10
  call void @ff_mutex_delete(i32 noundef %57)
  store i32 2, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %76

58:                                               ; preds = %53
  store i8 1, ptr @SysLock, align 1, !tbaa !9
  br label %59

59:                                               ; preds = %58, %49
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.FATFS, ptr %60, i32 0, i32 0
  store i8 0, ptr %61, align 8, !tbaa !12
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = load i32, ptr %9, align 4, !tbaa !10
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [1 x ptr], ptr @FatFs, i64 0, i64 %64
  store ptr %62, ptr %65, align 8, !tbaa !5
  br label %66

66:                                               ; preds = %59, %33
  %67 = load i8, ptr %7, align 1, !tbaa !9
  %68 = zext i8 %67 to i32
  %69 = icmp eq i32 %68, 0
  br i1 %69, label %70, label %71

70:                                               ; preds = %66
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %76

71:                                               ; preds = %66
  %72 = call i32 @mount_volume(ptr noundef %6, ptr noundef %5, i8 noundef zeroext 0)
  store i32 %72, ptr %10, align 4, !tbaa !10
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = load i32, ptr %10, align 4, !tbaa !10
  call void @unlock_volume(ptr noundef %73, i32 noundef %74)
  %75 = load i32, ptr %10, align 4, !tbaa !10
  store i32 %75, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %76

76:                                               ; preds = %71, %70, %56, %48, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %77 = load i32, ptr %4, align 4
  ret i32 %77
}

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

; Function Attrs: nounwind uwtable
declare hidden i32 @get_ldnumber(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @clear_share(ptr noundef) #0

declare void @ff_mutex_delete(i32 noundef) #2

declare i32 @ff_mutex_create(i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @mount_volume(ptr noundef, ptr noundef, i8 noundef zeroext) #0

; Function Attrs: nounwind uwtable
declare hidden void @unlock_volume(ptr noundef, i32 noundef) #0

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

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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !7, i64 0}
!13 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4, !7, i64 5, !14, i64 6, !14, i64 8, !14, i64 10, !6, i64 16, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !7, i64 64}
!14 = !{!"short", !7, i64 0}
!15 = !{!13, !7, i64 1}
!16 = !{!13, !7, i64 2}


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

@FatFs = external hidden local_unnamed_addr global [1 x ptr], align 8
@SysLock = external hidden local_unnamed_addr global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @f_mount(ptr noundef %0, ptr noundef %1, i8 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, 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 8, ptr nonnull %6) #3
  store ptr %1, ptr %6, align 8, !tbaa !5
  %7 = call i32 @get_ldnumber(ptr noundef nonnull %6)
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %33, label %9

9:                                                ; preds = %3
  %10 = zext nneg i32 %7 to i64
  %11 = getelementptr inbounds [1 x ptr], ptr @FatFs, i64 0, i64 %10
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %14, label %13

13:                                               ; preds = %9
  store ptr null, ptr %11, align 8, !tbaa !5
  call void @clear_share(ptr noundef nonnull %12)
  call void @ff_mutex_delete(i32 noundef %7) #3
  store i8 0, ptr %12, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %13, %9
  %.not18 = icmp eq ptr %0, null
  br i1 %.not18, label %28, label %15

15:                                               ; preds = %14
  %16 = trunc i32 %7 to i8
  %17 = getelementptr inbounds i8, ptr %0, i64 1
  store i8 %16, ptr %17, align 1, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %0, i64 2
  store i8 %16, ptr %18, align 2, !tbaa !14
  %19 = call i32 @ff_mutex_create(i32 noundef %7) #3
  %.not19 = icmp eq i32 %19, 0
  br i1 %.not19, label %33, label %20

20:                                               ; preds = %15
  %21 = load i8, ptr @SysLock, align 1, !tbaa !15
  %22 = icmp eq i8 %21, 0
  br i1 %22, label %23, label %27

23:                                               ; preds = %20
  %24 = call i32 @ff_mutex_create(i32 noundef 1) #3
  %.not20 = icmp eq i32 %24, 0
  br i1 %.not20, label %25, label %26

25:                                               ; preds = %23
  call void @ff_mutex_delete(i32 noundef %7) #3
  br label %33

26:                                               ; preds = %23
  store i8 1, ptr @SysLock, align 1, !tbaa !15
  br label %27

27:                                               ; preds = %26, %20
  store i8 0, ptr %0, align 8, !tbaa !9
  store ptr %0, ptr %11, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %27, %14
  %29 = icmp eq i8 %2, 0
  br i1 %29, label %33, label %30

30:                                               ; preds = %28
  %31 = call i32 @mount_volume(ptr noundef nonnull %5, ptr noundef nonnull %4, i8 noundef zeroext 0)
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  call void @unlock_volume(ptr noundef %32, i32 noundef %31)
  br label %33

33:                                               ; preds = %28, %15, %3, %30, %25
  %.0 = phi i32 [ %31, %30 ], [ 2, %25 ], [ 11, %3 ], [ 2, %15 ], [ 0, %28 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #3
  ret i32 %.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: nounwind uwtable
declare hidden i32 @get_ldnumber(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @clear_share(ptr noundef) local_unnamed_addr #0

declare void @ff_mutex_delete(i32 noundef) local_unnamed_addr #2

declare i32 @ff_mutex_create(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @mount_volume(ptr noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @unlock_volume(ptr noundef, i32 noundef) local_unnamed_addr #0

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

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 = { "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, !7, i64 0}
!10 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4, !7, i64 5, !11, i64 6, !11, i64 8, !11, i64 10, !6, i64 16, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52, !12, i64 56, !12, i64 60, !7, i64 64}
!11 = !{!"short", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !7, i64 1}
!14 = !{!10, !7, i64 2}
!15 = !{!7, !7, i64 0}


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