// File: davmac314[P]dinit/src/includes/dinit-util.h
// Function: readlink_at_str(int, char const*)
inline std::pair<bool, std::vector<char>> readlink_at_str(int dirfd, const char *path) noexcept
{
    struct stat sb;
    if (bp_sys::fstatat(dirfd, path, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
        return {false, {}};
    }

    size_t vec_size = sb.st_size + 1; // add one for nul terminator

    try {
        std::vector<char> vec_str(vec_size);

        try_read_link:

        ssize_t r = bp_sys::readlinkat(dirfd, path, vec_str.data(), vec_size);

        if (r < 0) {
            return {false, {}};
        }

        if ((size_t)r > vec_size) {
            // Our size estimate was off (link changed?), try again
            vec_size = r;
            vec_str.resize(vec_size);
            goto try_read_link;
        }

        // shrink, or even expand (append nul) as necessary
        vec_str.resize(r + 1);

        return {true, std::move(vec_str)};
    }
    catch (std::bad_alloc &) {
        errno = ENOMEM;
        return {false, {}};
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x170(%rbp)
movq %rdi, %rax
movq %rax, -0x168(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl -0xc(%rbp), %edi
movq -0x18(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
movl $0x100, %ecx            # imm = 0x100
callq 0x50e0
cmpl $-0x1, %eax
jne 0x158a0
movb $0x0, -0xa9(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
movq $0x0, -0xc0(%rbp)
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x15780
movq -0x170(%rbp), %rdi
movq -0x178(%rbp), %rdx
leaq -0xa9(%rbp), %rsi
callq 0x15d80
jmp 0x1588f
leaq -0xd0(%rbp), %rdi
callq 0x15cd0
jmp 0x15b3d
movq -0x78(%rbp), %rax
incq %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0xf1(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x5700
movq -0x188(%rbp), %rsi
movq -0x180(%rbp), %rdx
leaq -0xf0(%rbp), %rdi
callq 0x15dc0
jmp 0x158eb
leaq -0xf1(%rbp), %rdi
callq 0x5440
movl -0xc(%rbp), %eax
movl %eax, -0x194(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x190(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x15ba0
movl -0x194(%rbp), %edi
movq -0x190(%rbp), %rsi
movq %rax, %rdx
movq -0xd8(%rbp), %rcx
callq 0x50a0
movq %rax, -0x110(%rbp)
cmpq $0x0, -0x110(%rbp)
jge 0x159f4
movb $0x0, -0x111(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x130(%rbp)
movq $0x0, -0x120(%rbp)
leaq -0x130(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x15780
movq -0x170(%rbp), %rdi
movq -0x1a0(%rbp), %rdx
leaq -0x111(%rbp), %rsi
callq 0x15d80
jmp 0x15993
leaq -0x130(%rbp), %rdi
callq 0x15cd0
movl $0x1, -0x134(%rbp)
jmp 0x15a85
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0xf1(%rbp), %rdi
callq 0x5440
jmp 0x15aa2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x15cd0
jmp 0x15a96
movq -0x110(%rbp), %rax
cmpq -0xd8(%rbp), %rax
jbe 0x15a40
movq -0x110(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
callq 0x15e30
jmp 0x15a27
jmp 0x158f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x15a96
movq -0x110(%rbp), %rsi
incq %rsi
leaq -0xf0(%rbp), %rdi
callq 0x15e30
jmp 0x15a58
movq -0x170(%rbp), %rdi
movb $0x1, -0x135(%rbp)
leaq -0x135(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
callq 0x15ec0
jmp 0x15a7b
movl $0x1, -0x134(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x15cd0
jmp 0x15b3d
leaq -0xf0(%rbp), %rdi
callq 0x15cd0
movl -0x104(%rbp), %eax
movl $0x2, %ecx
cmpl %ecx, %eax
jne 0x15b55
movq -0x100(%rbp), %rdi
callq 0x5150
movq %rax, -0x140(%rbp)
callq 0x5050
movl $0xc, (%rax)
movb $0x0, -0x141(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x160(%rbp)
movq $0x0, -0x150(%rbp)
leaq -0x160(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x15780
movq -0x170(%rbp), %rdi
movq -0x1a8(%rbp), %rdx
leaq -0x141(%rbp), %rsi
callq 0x15d80
jmp 0x15b1e
leaq -0x160(%rbp), %rdi
callq 0x15cd0
movl $0x1, -0x134(%rbp)
callq 0x5680
jmp 0x15b3b
jmp 0x15b3d
movq -0x168(%rbp), %rax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq %rax, %rdi
callq 0x103e0
movq -0x100(%rbp), %rdi
callq 0x103e0
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)