; LLVM IR from ctlst-tech/catpilot :: os/freertos/services/cli (service)

; 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.cli_service = type { i32, ptr, ptr, ptr, ptr, i32, i32, i32, %struct.pthread_mutex_internal, %struct.pthread_cond_internal, i32, i32 }
%struct.pthread_mutex_internal = type { i64, %struct.xSTATIC_QUEUE, ptr, %struct.pthread_mutexattr_internal }
%struct.xSTATIC_QUEUE = type { [3 x ptr], %union.anon, [2 x %struct.xSTATIC_LIST], [3 x i64], [2 x i8], i8 }
%union.anon = type { ptr }
%struct.xSTATIC_LIST = type { i64, ptr, %struct.xSTATIC_MINI_LIST_ITEM }
%struct.xSTATIC_MINI_LIST_ITEM = type { i32, [2 x ptr] }
%struct.pthread_mutexattr_internal = type { i32 }
%struct.pthread_cond_internal = type { i64, %struct.xSTATIC_QUEUE, i32 }

@nl = external hidden global [2 x i8], align 1
@inv = external hidden global [2 x i8], align 1
@__func__.cli_invoker = external hidden unnamed_addr constant [12 x i8], align 1
@FreeRTOS_errno = external global i32, align 4
@.str.6 = external hidden unnamed_addr constant [23 x i8], align 1

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

declare i32 @pthread_setname_np(ptr noundef) #1

declare i32 @write(i32 noundef, ptr noundef, i64 noundef) #1

declare i32 @pthread_mutex_lock(ptr noundef) #1

declare i32 @pthread_mutex_unlock(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @cli_invoker(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %5, ptr %4, align 8, !tbaa !5
  %6 = call i32 @pthread_setname_np(ptr noundef @__func__.cli_invoker)
  br label %7

7:                                                ; preds = %34, %1
  br label %8

8:                                                ; preds = %7
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.cli_service, ptr %9, i32 0, i32 8
  %11 = call i32 @pthread_mutex_lock(ptr noundef %10)
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.cli_service, ptr %12, i32 0, i32 9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.cli_service, ptr %14, i32 0, i32 8
  %16 = call i32 @pthread_cond_wait(ptr noundef %13, ptr noundef %15)
  %17 = call i32 @write(i32 noundef 1, ptr noundef @nl, i64 noundef 2)
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.cli_service, ptr %18, i32 0, i32 3
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  %21 = call i32 @cli_cmd_execute(ptr noundef %20)
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %34

23:                                               ; preds = %8
  %24 = load i32, ptr @FreeRTOS_errno, align 4, !tbaa !17
  %25 = icmp ne i32 %24, 11
  br i1 %25, label %26, label %33

26:                                               ; preds = %23
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.cli_service, ptr %27, i32 0, i32 3
  %29 = load ptr, ptr %28, align 8, !tbaa !9
  %30 = call i32 (ptr, ...) @printf_(ptr noundef @.str.6, ptr noundef %29)
  %31 = call i32 @cli_cmd_print()
  %32 = call i32 @write(i32 noundef 1, ptr noundef @nl, i64 noundef 2)
  br label %33

33:                                               ; preds = %26, %23
  br label %34

34:                                               ; preds = %33, %8
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.cli_service, ptr %35, i32 0, i32 8
  %37 = call i32 @pthread_mutex_unlock(ptr noundef %36)
  %38 = call i32 @write(i32 noundef 1, ptr noundef @inv, i64 noundef 2)
  br label %7
}

declare i32 @pthread_cond_wait(ptr noundef, ptr noundef) #1

declare i32 @cli_cmd_execute(ptr noundef) #1

declare i32 @printf_(ptr noundef, ...) #1

declare i32 @cli_cmd_print() #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !6, i64 24}
!10 = !{!"cli_service", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !11, i64 44, !11, i64 48, !12, i64 56, !16, i64 224, !11, i64 384, !11, i64 388}
!11 = !{!"int", !7, i64 0}
!12 = !{!"pthread_mutex_internal", !13, i64 0, !14, i64 8, !6, i64 152, !15, i64 160}
!13 = !{!"long", !7, i64 0}
!14 = !{!"xSTATIC_QUEUE", !7, i64 0, !7, i64 24, !7, i64 32, !7, i64 112, !7, i64 136, !7, i64 138}
!15 = !{!"pthread_mutexattr_internal", !11, i64 0}
!16 = !{!"pthread_cond_internal", !13, i64 0, !14, i64 8, !11, i64 152}
!17 = !{!11, !11, 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"

@nl = external hidden global [2 x i8], align 1
@inv = external hidden global [2 x i8], align 1
@__func__.cli_invoker = external hidden unnamed_addr constant [12 x i8], align 1
@FreeRTOS_errno = external local_unnamed_addr global i32, align 4
@.str.6 = external hidden unnamed_addr constant [23 x i8], align 1

declare i32 @pthread_setname_np(ptr noundef) local_unnamed_addr #0

declare i32 @write(i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #0

declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #0

; Function Attrs: noreturn nounwind uwtable
define dso_local noalias noundef nonnull ptr @cli_invoker(ptr noundef %0) local_unnamed_addr #1 {
  %2 = tail call i32 @pthread_setname_np(ptr noundef nonnull @__func__.cli_invoker) #2
  %3 = getelementptr inbounds i8, ptr %0, i64 56
  %4 = getelementptr inbounds i8, ptr %0, i64 224
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  br label %6

6:                                                ; preds = %20, %1
  %7 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull %3) #2
  %8 = tail call i32 @pthread_cond_wait(ptr noundef nonnull %4, ptr noundef nonnull %3) #2
  %9 = tail call i32 @write(i32 noundef 1, ptr noundef nonnull @nl, i64 noundef 2) #2
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = tail call i32 @cli_cmd_execute(ptr noundef %10) #2
  %12 = icmp ne i32 %11, 0
  %13 = load i32, ptr @FreeRTOS_errno, align 4
  %14 = icmp ne i32 %13, 11
  %or.cond = select i1 %12, i1 %14, i1 false
  br i1 %or.cond, label %15, label %20

15:                                               ; preds = %6
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = tail call i32 (ptr, ...) @printf_(ptr noundef nonnull @.str.6, ptr noundef %16) #2
  %18 = tail call i32 @cli_cmd_print() #2
  %19 = tail call i32 @write(i32 noundef 1, ptr noundef nonnull @nl, i64 noundef 2) #2
  br label %20

20:                                               ; preds = %15, %6
  %21 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull %3) #2
  %22 = tail call i32 @write(i32 noundef 1, ptr noundef nonnull @inv, i64 noundef 2) #2
  br label %6
}

declare i32 @pthread_cond_wait(ptr noundef, ptr noundef) local_unnamed_addr #0

declare i32 @cli_cmd_execute(ptr noundef) local_unnamed_addr #0

declare i32 @printf_(ptr noundef, ...) local_unnamed_addr #0

declare i32 @cli_cmd_print() local_unnamed_addr #0

attributes #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 = { noreturn 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, !10, i64 24}
!6 = !{!"cli_service", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !7, i64 40, !7, i64 44, !7, i64 48, !11, i64 56, !15, i64 224, !7, i64 384, !7, i64 388}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"pthread_mutex_internal", !12, i64 0, !13, i64 8, !10, i64 152, !14, i64 160}
!12 = !{!"long", !8, i64 0}
!13 = !{!"xSTATIC_QUEUE", !8, i64 0, !8, i64 24, !8, i64 32, !8, i64 112, !8, i64 136, !8, i64 138}
!14 = !{!"pthread_mutexattr_internal", !7, i64 0}
!15 = !{!"pthread_cond_internal", !12, i64 0, !13, i64 8, !7, i64 152}


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