// File: davmac314[P]dinit/src/dinitctl.cc
// Function: find_service_desc(char const*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
static std::pair<int,int> find_service_desc(const char *svc_name, const std::vector<std::string> &paths,
        std::string &service_file_path)
{
    using namespace std;

    // We use this to store a copy of the service name without argument, if needed
    std::string svc_name_store;

    // Check for service argument which must be stripped
    auto at_ptr = svc_name;
    while (*at_ptr != '\0' && *at_ptr != '@') ++at_ptr;
    if (*at_ptr == '@') {
        svc_name_store = std::string(svc_name, at_ptr - svc_name);
        svc_name = svc_name_store.c_str();
    }

    for (std::string path : paths) {
        string test_path = combine_paths(path, ::string_view(svc_name, at_ptr - svc_name));

        auto sdf_fds = open_with_dir(path.c_str(), svc_name);
        if (sdf_fds.first != -1 || sdf_fds.second != ENOENT) {
            service_file_path = test_path;
            return sdf_fds;
        }
    }

    return {-1, ENOENT};
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x55c0
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x105(%rbp)
je 0xd6df
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
setne %al
movb %al, -0x105(%rbp)
movb -0x105(%rbp), %al
testb $0x1, %al
jne 0xd6eb
jmp 0xd6f9
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xd6b8
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0xd79a
movq -0x10(%rbp), %rcx
movq %rcx, -0x120(%rbp)
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x118(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x5700
movq -0x120(%rbp), %rsi
movq -0x118(%rbp), %rdx
movq -0x110(%rbp), %rcx
leaq -0x68(%rbp), %rdi
callq 0x5480
jmp 0xd752
leaq -0x40(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x5500
leaq -0x68(%rbp), %rdi
callq 0x5280
leaq -0x69(%rbp), %rdi
callq 0x5440
leaq -0x40(%rbp), %rdi
callq 0x5160
movq %rax, -0x10(%rbp)
jmp 0xd79a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x69(%rbp), %rdi
callq 0x5440
jmp 0xd990
movq -0x18(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x175f0
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
callq 0x17620
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x17650
testb $0x1, %al
jne 0xd7e7
jmp 0xd943
leaq -0x90(%rbp), %rdi
callq 0x17690
movq %rax, %rsi
leaq -0xb8(%rbp), %rdi
callq 0x5180
jmp 0xd804
leaq -0xe8(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x14240
jmp 0xd819
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rdx
subq %rsi, %rdx
leaq -0xf8(%rbp), %rdi
callq 0x15c10
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
movq -0xf8(%rbp), %rcx
movq -0xf0(%rbp), %r8
leaq -0xd8(%rbp), %rdi
callq 0x176b0
jmp 0xd85a
leaq -0xb8(%rbp), %rdi
callq 0x5160
movq %rax, %rdi
movq -0x10(%rbp), %rsi
movl $0xffffff9c, %edx       # imm = 0xFFFFFF9C
callq 0x13870
movq %rax, -0x8(%rbp)
cmpl $-0x1, -0x8(%rbp)
jne 0xd887
cmpl $0x2, -0x4(%rbp)
je 0xd8de
movq -0x20(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x5040
jmp 0xd899
movl $0x1, -0xfc(%rbp)
jmp 0xd8e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xd990
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xd935
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x5280
jmp 0xd935
movl $0x0, -0xfc(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x5280
movl -0xfc(%rbp), %eax
testl %eax, %eax
jne 0xd90a
jmp 0xd900
movl $0x0, -0xfc(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x5280
movl -0xfc(%rbp), %eax
testl %eax, %eax
jne 0xd97a
jmp 0xd922
jmp 0xd924
leaq -0x90(%rbp), %rdi
callq 0x17870
jmp 0xd7cb
leaq -0xb8(%rbp), %rdi
callq 0x5280
jmp 0xd990
movl $0xffffffff, -0x100(%rbp) # imm = 0xFFFFFFFF
movl $0x2, -0x104(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
leaq -0x104(%rbp), %rdx
callq 0x157d0
jmp 0xd970
movl $0x1, -0xfc(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x5280
movq -0x8(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x5280
movq -0x78(%rbp), %rdi
callq 0x56f0
nopw %cs:(%rax,%rax)