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

; Function Attrs: nounwind uwtable
define dso_local void @cli_add_to_history(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.cli_service, ptr %4, i32 0, i32 4
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = getelementptr inbounds ptr, ptr %6, i64 1
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.cli_service, ptr %9, i32 0, i32 3
  %11 = load ptr, ptr %10, align 8, !tbaa !17
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.cli_service, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !18
  %15 = sub nsw i32 %14, 1
  %16 = sext i32 %15 to i64
  %17 = call i32 @strncmp(ptr noundef %8, ptr noundef %11, i64 noundef %16) #4
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %20, label %19

19:                                               ; preds = %1
  br label %68

20:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.cli_service, ptr %21, i32 0, i32 10
  %23 = load i32, ptr %22, align 8, !tbaa !19
  store i32 %23, ptr %3, align 4, !tbaa !20
  br label %24

24:                                               ; preds = %50, %20
  %25 = load i32, ptr %3, align 4, !tbaa !20
  %26 = icmp sge i32 %25, 2
  br i1 %26, label %28, label %27

27:                                               ; preds = %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  br label %53

28:                                               ; preds = %24
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.cli_service, ptr %29, i32 0, i32 4
  %31 = load ptr, ptr %30, align 8, !tbaa !9
  %32 = load i32, ptr %3, align 4, !tbaa !20
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds ptr, ptr %31, i64 %33
  %35 = load ptr, ptr %34, align 8, !tbaa !5
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.cli_service, ptr %36, i32 0, i32 4
  %38 = load ptr, ptr %37, align 8, !tbaa !9
  %39 = load i32, ptr %3, align 4, !tbaa !20
  %40 = sub nsw i32 %39, 1
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds ptr, ptr %38, i64 %41
  %43 = load ptr, ptr %42, align 8, !tbaa !5
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.cli_service, ptr %44, i32 0, i32 0
  %46 = load i32, ptr %45, align 8, !tbaa !18
  %47 = sub nsw i32 %46, 1
  %48 = sext i32 %47 to i64
  %49 = call ptr @strncpy(ptr noundef %35, ptr noundef %43, i64 noundef %48) #5
  br label %50

50:                                               ; preds = %28
  %51 = load i32, ptr %3, align 4, !tbaa !20
  %52 = add nsw i32 %51, -1
  store i32 %52, ptr %3, align 4, !tbaa !20
  br label %24, !llvm.loop !21

53:                                               ; preds = %27
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.cli_service, ptr %54, i32 0, i32 4
  %56 = load ptr, ptr %55, align 8, !tbaa !9
  %57 = getelementptr inbounds ptr, ptr %56, i64 1
  %58 = load ptr, ptr %57, align 8, !tbaa !5
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.cli_service, ptr %59, i32 0, i32 3
  %61 = load ptr, ptr %60, align 8, !tbaa !17
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.cli_service, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %63, align 8, !tbaa !18
  %65 = sub nsw i32 %64, 1
  %66 = sext i32 %65 to i64
  %67 = call ptr @strncpy(ptr noundef %58, ptr noundef %61, i64 noundef %66) #5
  br label %68

68:                                               ; preds = %53, %19
  ret void
}

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

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

; Function Attrs: nounwind
declare ptr @strncpy(ptr noundef, ptr noundef, i64 noundef) #3

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

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 = { nounwind willreturn memory(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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { nounwind willreturn memory(read) }
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, !6, i64 32}
!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 = !{!10, !6, i64 24}
!18 = !{!10, !11, i64 0}
!19 = !{!10, !11, i64 384}
!20 = !{!11, !11, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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

; Function Attrs: nofree nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @cli_add_to_history(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !16
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !17
  %8 = load i32, ptr %0, align 8, !tbaa !18
  %9 = add nsw i32 %8, -1
  %10 = sext i32 %9 to i64
  %11 = tail call i32 @strncmp(ptr noundef %5, ptr noundef %7, i64 noundef %10) #3
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %30, label %12

12:                                               ; preds = %1
  %13 = getelementptr inbounds i8, ptr %0, i64 384
  %14 = load i32, ptr %13, align 8, !tbaa !19
  %15 = icmp sgt i32 %14, 1
  br i1 %15, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %12
  %16 = zext nneg i32 %14 to i64
  br label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load ptr, ptr %2, align 8, !tbaa !5
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.pre, i64 8
  %.pre15 = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !16
  %.pre16 = load ptr, ptr %6, align 8, !tbaa !17
  %.pre17 = load i32, ptr %0, align 8, !tbaa !18
  %.pre18 = add nsw i32 %.pre17, -1
  %.pre19 = sext i32 %.pre18 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %12
  %.pre-phi20 = phi i64 [ %.pre19, %._crit_edge.loopexit ], [ %10, %12 ]
  %17 = phi ptr [ %.pre16, %._crit_edge.loopexit ], [ %7, %12 ]
  %18 = phi ptr [ %.pre15, %._crit_edge.loopexit ], [ %5, %12 ]
  %19 = tail call ptr @strncpy(ptr noundef %18, ptr noundef %17, i64 noundef %.pre-phi20) #4
  br label %30

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %16, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = getelementptr inbounds ptr, ptr %20, i64 %indvars.iv
  %22 = load ptr, ptr %21, align 8, !tbaa !16
  %23 = getelementptr i8, ptr %21, i64 -8
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = load i32, ptr %0, align 8, !tbaa !18
  %26 = add nsw i32 %25, -1
  %27 = sext i32 %26 to i64
  %28 = tail call ptr @strncpy(ptr noundef %22, ptr noundef %24, i64 noundef %27) #4
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %29 = icmp ugt i64 %indvars.iv, 2
  br i1 %29, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !20

30:                                               ; preds = %1, %._crit_edge
  ret void
}

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strncpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly, i64 noundef) local_unnamed_addr #2

attributes #0 = { 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 #1 = { 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 #2 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 willreturn memory(read) }
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, !10, i64 32}
!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}
!16 = !{!10, !10, i64 0}
!17 = !{!6, !10, i64 24}
!18 = !{!6, !7, i64 0}
!19 = !{!6, !7, i64 384}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass