// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, %r12
movq 0x10(%rdi), %rdi
movq 0x18(%r12), %rsi
movq 0x20(%r12), %rdx
movq 0x28(%r12), %rcx
callq 0x85dd
testq %rax, %rax
je 0x9485
movq %rax, %rbx
movq %rax, %rdi
callq 0x8814
testq %rax, %rax
je 0x9293
movq %rax, %r14
movq (%r12), %rdi
movq 0x8(%r12), %rsi
callq 0x761b
cmpl $-0x1, %eax
je 0x92a0
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 0x645d
movq %rax, %r12
movq %r14, %rdi
callq 0x642f
movl %r15d, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x7896
movq %rax, %r12
movq %r14, %rdi
callq 0x642f
cmpq %rax, %r12
jne 0x9467
movq %r14, %rdi
callq 0x667f
leaq -0x30(%rbp), %r12
movl $0x0, (%r12)
leaq 0x1d62(%rip), %r13      # 0xafb1
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x71cd
testq %rax, %rax
js 0x9272
movq %r14, %rdi
callq 0x8d84
movzbl %al, %eax
orl $0x2, %eax
jmp 0x927d
movl $0x4, %eax
cmpl $0x4, -0x30(%rbp)
jne 0x9284
cmpl $0x4, %eax
je 0x924f
jmp 0x92b5
movq %r13, %rdi
callq 0x22a0
movl $0x2, %eax
jmp 0x927d
movq %rbx, %rdi
callq 0x87c4
jmp 0x9485
movq %rbx, %rdi
callq 0x87c4
movq %r14, %rdi
callq 0x63bc
jmp 0x9485
cmpl $0x2, %eax
je 0x9467
leaq -0x2c(%rbp), %rsi
movl $0x0, (%rsi)
movq %r14, %rdi
callq 0x8db6
testb %al, %al
je 0x9467
cmpl $0x0, -0x2c(%rbp)
je 0x9332
leaq -0x30(%rbp), %r12
leaq 0x1cd4(%rip), %r13      # 0xafbd
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x71cd
testq %rax, %rax
js 0x9303
subl %eax, -0x2c(%rbp)
xorl %eax, %eax
jmp 0x930e
movl $0x5, %eax
cmpl $0x4, -0x30(%rbp)
jne 0x9323
testl %eax, %eax
je 0x931b
cmpl $0x5, %eax
jne 0x93c8
cmpl $0x0, -0x2c(%rbp)
jne 0x92e9
jmp 0x9332
movq %r13, %rdi
callq 0x22a0
movl $0x2, %eax
jmp 0x930e
movq %r14, %rdi
callq 0x8a46
testq %rax, %rax
je 0x9485
movq %rax, %r12
cmpb $0x1, 0x8(%rax)
je 0x9496
movq %r12, -0x40(%rbp)
movq 0x20(%r12), %r12
testq %r12, %r12
je 0x93d6
leaq 0x1(%r12), %rdi
callq 0x23a8
testq %rax, %rax
je 0x949d
movq %rax, %r13
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rsi
movq %r13, %rdi
movq %r12, %rdx
movq %rax, %r12
callq 0x2200
movq 0x20(%r12), %rax
movb $0x0, (%r13,%rax)
movzbl (%r13), %eax
orl $0x20, %eax
cmpl $0x7b, %eax
jne 0x93f1
movq %r13, %rdi
callq 0x4160
testq %rax, %rax
je 0x93f1
movq %r13, -0x48(%rbp)
cmpb $0x0, 0x9(%r12)
je 0x9415
leaq 0x1c91(%rip), %rcx      # 0xb053
movq %rcx, -0x38(%rbp)
jmp 0x9432
cmpl $0x2, %eax
je 0x9467
jmp 0x9485
leaq 0x1c76(%rip), %rdi      # 0xb053
leaq 0xdea(%rip), %rsi       # 0xa1ce
xorl %eax, %eax
callq 0x2100
movq -0x40(%rbp), %r12
jmp 0x945f
cmpb $0x1, 0x9(%r12)
jne 0x9402
leaq 0x1c53(%rip), %rdi      # 0xb053
jmp 0x9409
leaq 0x1c59(%rip), %rdi      # 0xb062
movq %r13, %rsi
xorl %eax, %eax
callq 0x2100
jmp 0x9457
movq 0x10(%r12), %rsi
leaq 0x1c41(%rip), %rdi      # 0xb062
movq %rax, %r13
movq %rdi, -0x38(%rbp)
xorl %eax, %eax
callq 0x2100
movq %r13, %rax
movq %rax, %r13
movq %rax, %rdi
callq 0x416e
movq -0x38(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x2100
movq %r13, %rdi
callq 0x3af0
movq -0x48(%rbp), %r13
movq %r13, %rdi
callq 0x23a0
movq %r12, %rdi
callq 0x859c
movq %rbx, %rdi
callq 0x87c4
movq %r14, %rdi
callq 0x63bc
cmpl $-0x1, %r15d
je 0x9485
movl %r15d, %edi
callq 0x2160
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x28aa
jmp 0x945f
leaq 0x1b7d(%rip), %rdi      # 0xb021
leaq 0x1af7(%rip), %rsi      # 0xafa2
leaq 0x1b74(%rip), %rcx      # 0xb026
movl $0x28a, %edx            # imm = 0x28A
callq 0x2150