// File: /workspace/github_c_makefile_star2_O3/goghcrow[P]dubbo_ab/dubbo_client.c
// Function: dubbo_invoke_sync
bool dubbo_invoke_sync(struct dubbo_args *args)
{
    bool ret = false;

    struct dubbo_req *req = dubbo_req_create(args->service, args->method, args->args, args->attach);
    if (req == NULL)
    {
        return false;
    }

    struct buffer *buf = dubbo_encode(req);
    if (!buf)
    {
        dubbo_req_release(req);
        return false;
    }

    int sockfd = socket_clientSync(args->host, args->port);
    if (sockfd == -1)
    {
        goto release;
    }

    setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &args->timeout, sizeof(struct timeval));
    setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &args->timeout, sizeof(struct timeval));

    size_t send_n = socket_sendAllSync(sockfd, buf_peek(buf), buf_readable(buf));
    if (send_n != buf_readable(buf))
    {
        goto release;
    }

    // fucking swoole, reactor 对EPOLLRDHUP事件直接当error处理，关闭, 不能正确处理版关闭事件
    // socket_shutdownWrite(sockfd);

    // reset buffer
    buf_retrieveAll(buf);

    int errno_ = 0;
    for (;;)
    {
        ssize_t recv_n = buf_readFd(buf, sockfd, &errno_);
        if (recv_n < 0)
        {
            if (errno_ == EINTR)
            {
                continue;
            }
            else
            {
                perror("ERROR readv");
                goto release;
            }
        }
        else if (!is_dubbo_pkt(buf))
        {
            goto release;
        }
        break;
    }

    int remaining = 0;
    if (!is_completed_dubbo_pkt(buf, &remaining))
    {
        goto release;
    }

    while (remaining)
    {
        ssize_t recv_n = buf_readFd(buf, sockfd, &errno_);
        if (recv_n < 0)
        {
            if (errno_ == EINTR)
            {
                continue;
            }
            else
            {
                perror("ERROR receiving");
                goto release;
            }
        }
        remaining -= recv_n;
    }

    {
        struct dubbo_res *res = dubbo_decode(buf);
        if (res == NULL)
        {
            return false;
        }

        if (res->is_evt)
        {
            LOG_INFO("接收到 dubbo 事件包");
        }
        else if (res->data_sz)
        {
            // 返回 json, 不应该有 NULL 存在, 且非 NULL 结尾
            char *json = malloc(res->data_sz + 1);
            assert(json);
            memcpy(json, res->data, res->data_sz);
            json[res->data_sz] = '\0';

            cJSON *resp = NULL;
            if ((json[0] == '[' || json[0] == '{') && (resp = cJSON_Parse(json)))
            {
                if (res->ok)
                {
                    printf("\x1B[1;32m%s\x1B[0m\n", cJSON_Print(resp));
                }
                else
                {
                    printf("\x1B[1;31m%s\x1B[0m\n", res->desc);
                    printf("\x1B[1;31m%s\x1B[0m\n", cJSON_Print(resp));
                }
                cJSON_Delete(resp);
            }
            else
            {
                if (res->ok)
                {
                    printf("\x1B[1;32m%s\x1B[0m\n", json);
                }
                else
                {
                    printf("\x1B[1;31m%s\x1B[0m\n", json);
                }
            }
            free(json);
        }
        else if (res->data_sz == 0)
        {
            printf("\x1B[1;32m%s\x1B[0m\n", "NULL");
        }

        dubbo_res_release(res);
    }

// fimxe print attach

release:
    dubbo_req_release(req);
    buf_release(buf);
    if (sockfd != -1)
    {
        close(sockfd);
    }
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r12
movq 0x10(%rdi), %rdi
movq 0x18(%r12), %rsi
movq 0x20(%r12), %rdx
movq 0x28(%r12), %rcx
callq 0x84a5
testq %rax, %rax
je 0x92df
movq %rax, %rbx
movq %rax, %rdi
callq 0x86d5
testq %rax, %rax
je 0x912e
movq %rax, %r14
movq (%r12), %rdi
movq 0x8(%r12), %rsi
callq 0x74d9
cmpl $-0x1, %eax
je 0x913b
movl %eax, %r15d
addq $0x30, %r12
movl %eax, %edi
movl $0x1, %esi
movl $0x15, %edx
movq %r12, %rcx
movl $0x10, %r8d
callq 0x2090
movl %r15d, %edi
movl $0x1, %esi
movl $0x14, %edx
movq %r12, %rcx
movl $0x10, %r8d
callq 0x2090
movq %r14, %rdi
callq 0x631e
movq %rax, %r12
movq %r14, %rdi
callq 0x62f0
movl %r15d, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x7754
movq %rax, %r12
movq %r14, %rdi
callq 0x62f0
cmpq %rax, %r12
jne 0x92c7
movq %r14, %rdi
callq 0x653e
leaq -0x30(%rbp), %r12
movl $0x0, (%r12)
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x7091
testq %rax, %rax
jns 0x9150
cmpl $0x4, -0x30(%rbp)
je 0x9104
leaq 0x1e8d(%rip), %rdi      # 0xafb1
callq 0x22a0
jmp 0x92c7
movq %rbx, %rdi
callq 0x8685
jmp 0x92df
movq %rbx, %rdi
callq 0x8685
movq %r14, %rdi
callq 0x627d
jmp 0x92df
movq %r14, %rdi
callq 0x8c4d
testb %al, %al
je 0x92c7
leaq -0x2c(%rbp), %rsi
movl $0x0, (%rsi)
movq %r14, %rdi
callq 0x8c7f
testb %al, %al
je 0x92c7
cmpl $0x0, -0x2c(%rbp)
je 0x91b2
leaq -0x30(%rbp), %r12
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x7091
testq %rax, %rax
js 0x91a1
movl -0x2c(%rbp), %ecx
subl %eax, %ecx
movl %ecx, -0x2c(%rbp)
jmp 0x91ae
cmpl $0x4, -0x30(%rbp)
jne 0x92f7
movl -0x2c(%rbp), %ecx
testl %ecx, %ecx
jne 0x9184
movq %r14, %rdi
callq 0x88fc
testq %rax, %rax
je 0x92df
movq %rax, %r13
cmpb $0x1, 0x8(%rax)
je 0x92f0
movq 0x20(%r13), %r12
testq %r12, %r12
je 0x9240
movq %r13, -0x38(%rbp)
leaq 0x1(%r12), %rdi
callq 0x23a8
testq %rax, %rax
je 0x9303
movq %rax, %r13
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x2200
movb $0x0, (%r13,%r12)
movzbl (%r13), %eax
orl $0x20, %eax
cmpl $0x7b, %eax
jne 0x9257
movq %r13, %rdi
callq 0x40c3
testq %rax, %rax
je 0x9257
movq %rax, %r12
movq %r13, -0x40(%rbp)
movq -0x38(%rbp), %rcx
cmpb $0x0, 0x9(%rcx)
je 0x927d
leaq 0x1e15(%rip), %r13      # 0xb053
jmp 0x9292
leaq 0x1e0c(%rip), %rdi      # 0xb053
leaq 0xf80(%rip), %rsi       # 0xa1ce
xorl %eax, %eax
callq 0x2100
jmp 0x92bf
movq -0x38(%rbp), %rax
cmpb $0x1, 0x9(%rax)
jne 0x926a
leaq 0x1deb(%rip), %rdi      # 0xb053
jmp 0x9271
leaq 0x1df1(%rip), %rdi      # 0xb062
movq %r13, %rsi
xorl %eax, %eax
callq 0x2100
jmp 0x92b3
movq 0x10(%rcx), %rsi
leaq 0x1dda(%rip), %r13      # 0xb062
movq %r13, %rdi
xorl %eax, %eax
callq 0x2100
movq %r12, %rdi
callq 0x40d1
movq %r13, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x2100
movq %r12, %rdi
callq 0x3a9a
movq -0x40(%rbp), %r13
movq %r13, %rdi
callq 0x23a0
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0x8464
movq %rbx, %rdi
callq 0x8685
movq %r14, %rdi
callq 0x627d
movl %r15d, %edi
callq 0x2160
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x28aa
jmp 0x92bf
leaq 0x1cbf(%rip), %rdi      # 0xafbd
jmp 0x9124
leaq 0x1d17(%rip), %rdi      # 0xb021
leaq 0x1c91(%rip), %rsi      # 0xafa2
leaq 0x1d0e(%rip), %rcx      # 0xb026
movl $0x28a, %edx            # imm = 0x28A
callq 0x2150