; LLVM IR from narke/Aragveli :: src/extra/fs (vfs)

; 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.anon = type { ptr }
%struct.file_system = type { [20 x i8], ptr, ptr, i64, %struct.anon.0 }
%struct.anon.0 = type { ptr, ptr }

@file_systems = external dso_local global %struct.anon, align 8

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

; Function Attrs: nounwind
declare i32 @strncmp(ptr noundef, ptr noundef, i64 noundef) #1

declare i64 @strnlen(ptr noundef, i64 noundef) #2

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

7:                                                ; preds = %26, %1
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %31

10:                                               ; preds = %7
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.file_system, ptr %11, i32 0, i32 0
  %13 = getelementptr inbounds [20 x i8], ptr %12, i64 0, i64 0
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.file_system, ptr %14, i32 0, i32 0
  %16 = getelementptr inbounds [20 x i8], ptr %15, i64 0, i64 0
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.file_system, ptr %17, i32 0, i32 0
  %19 = getelementptr inbounds [20 x i8], ptr %18, i64 0, i64 0
  %20 = call i64 @strnlen(ptr noundef %19, i64 noundef 20)
  %21 = add i64 %20, 1
  %22 = call i32 @strncmp(ptr noundef %13, ptr noundef %16, i64 noundef %21) #4
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %25, label %24

24:                                               ; preds = %10
  store i32 -10, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %52

25:                                               ; preds = %10
  br label %26

26:                                               ; preds = %25
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.file_system, ptr %27, i32 0, i32 4
  %29 = getelementptr inbounds %struct.anon.0, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  store ptr %30, ptr %4, align 8, !tbaa !5
  br label %7, !llvm.loop !15

31:                                               ; preds = %7
  br label %32

32:                                               ; preds = %31
  %33 = load ptr, ptr @file_systems, align 8, !tbaa !9
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.file_system, ptr %34, i32 0, i32 4
  %36 = getelementptr inbounds %struct.anon.0, ptr %35, i32 0, i32 0
  store ptr %33, ptr %36, align 8, !tbaa !11
  %37 = icmp ne ptr %33, null
  br i1 %37, label %38, label %45

38:                                               ; preds = %32
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.file_system, ptr %39, i32 0, i32 4
  %41 = getelementptr inbounds %struct.anon.0, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr @file_systems, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.file_system, ptr %42, i32 0, i32 4
  %44 = getelementptr inbounds %struct.anon.0, ptr %43, i32 0, i32 1
  store ptr %41, ptr %44, align 8, !tbaa !17
  br label %45

45:                                               ; preds = %38, %32
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %46, ptr @file_systems, align 8, !tbaa !9
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.file_system, ptr %47, i32 0, i32 4
  %49 = getelementptr inbounds %struct.anon.0, ptr %48, i32 0, i32 1
  store ptr @file_systems, ptr %49, align 8, !tbaa !17
  br label %50

50:                                               ; preds = %45
  br label %51

51:                                               ; preds = %50
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %52

52:                                               ; preds = %51, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %53 = load i32, ptr %2, align 4
  ret i32 %53
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 = { "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 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 #4 = { 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, !6, i64 0}
!10 = !{!"", !6, i64 0}
!11 = !{!12, !6, i64 48}
!12 = !{!"file_system", !7, i64 0, !6, i64 24, !6, i64 32, !13, i64 40, !14, i64 48}
!13 = !{!"long", !7, i64 0}
!14 = !{!"", !6, i64 0, !6, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!12, !6, i64 56}


; 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.anon = type { ptr }

@file_systems = external dso_local global %struct.anon, align 8

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @strncmp(ptr nocapture noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strnlen(ptr nocapture noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 -10, 1) i32 @fs_register(ptr noundef %0) local_unnamed_addr #1 {
  %.012 = load ptr, ptr @file_systems, align 8, !tbaa !5
  %.not13 = icmp eq ptr %.012, null
  br i1 %.not13, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %1
  %2 = getelementptr inbounds i8, ptr %0, i64 48
  store ptr %.012, ptr %2, align 8, !tbaa !9
  br label %11

3:                                                ; preds = %.lr.ph
  %4 = getelementptr inbounds i8, ptr %.014, i64 48
  %.0 = load ptr, ptr %4, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

.lr.ph:                                           ; preds = %1, %3
  %.014 = phi ptr [ %.0, %3 ], [ %.012, %1 ]
  %5 = tail call i64 @strnlen(ptr noundef nonnull dereferenceable(1) %.014, i64 noundef 20)
  %6 = add i64 %5, 1
  %7 = tail call i32 @strncmp(ptr noundef %0, ptr noundef nonnull %.014, i64 noundef %6) #2
  %.not11 = icmp eq i32 %7, 0
  br i1 %.not11, label %.loopexit, label %3

._crit_edge:                                      ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 48
  store ptr %.012, ptr %8, align 8, !tbaa !9
  br i1 %.not13, label %11, label %9

9:                                                ; preds = %._crit_edge
  %10 = getelementptr inbounds i8, ptr %.012, i64 56
  store ptr %8, ptr %10, align 8, !tbaa !15
  br label %11

11:                                               ; preds = %._crit_edge.thread, %9, %._crit_edge
  store ptr %0, ptr @file_systems, align 8, !tbaa !16
  %12 = getelementptr inbounds i8, ptr %0, i64 56
  store ptr @file_systems, ptr %12, align 8, !tbaa !15
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %11
  %.09 = phi i32 [ 0, %11 ], [ -10, %.lr.ph ]
  ret i32 %.09
}

attributes #0 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { nofree nounwind memory(readwrite, 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" }
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, !6, i64 48}
!10 = !{!"file_system", !7, i64 0, !6, i64 24, !6, i64 32, !11, i64 40, !12, i64 48}
!11 = !{!"long", !7, i64 0}
!12 = !{!"", !6, i64 0, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!10, !6, i64 56}
!16 = !{!17, !6, i64 0}
!17 = !{!"", !6, i64 0}


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