// File: /workspace/github_c_makefile_star2_O2/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 0x728a
testq %rax, %rax
je 0x8377
movq %rax, %rbx
movq %rax, %rdi
callq 0x74bc
testq %rax, %rax
je 0x81cd
movq %rax, %r14
movq (%r12), %rdi
movq 0x8(%r12), %rsi
callq 0x62e8
cmpl $-0x1, %eax
je 0x81da
movl %eax, %r15d
addq $0x30, %r12
pushq $0x1
popq %r13
pushq $0x15
popq %rdx
movq %rbx, -0x48(%rbp)
pushq $0x10
popq %rbx
movl %eax, %edi
movl %r13d, %esi
movq %r12, %rcx
movl %ebx, %r8d
callq 0x2090
pushq $0x14
popq %rdx
movl %r15d, %edi
movl %r13d, %esi
movq %r12, %rcx
movl %ebx, %r8d
callq 0x2090
movq %r14, %rdi
callq 0x5283
movq %rax, %rbx
movq %r14, %rdi
callq 0x5255
movl %r15d, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x6545
movq %rax, %rbx
movq %r14, %rdi
callq 0x5255
cmpq %rax, %rbx
movq -0x48(%rbp), %rbx
jne 0x835f
movq %r14, %rdi
callq 0x549e
leaq -0x2c(%rbp), %r12
andl $0x0, (%r12)
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x5ea2
testq %rax, %rax
jns 0x81ef
cmpl $0x4, -0x2c(%rbp)
je 0x81a3
leaq 0x1f3e(%rip), %rdi      # 0xa101
callq 0x22a0
jmp 0x835f
movq %rbx, %rdi
callq 0x7471
jmp 0x8377
movq %rbx, %rdi
callq 0x7471
movq %r14, %rdi
callq 0x51e9
jmp 0x8377
movq %r14, %rdi
callq 0x7b49
testb %al, %al
je 0x835f
leaq -0x30(%rbp), %rsi
andl $0x0, (%rsi)
movq %r14, %rdi
callq 0x7b77
testb %al, %al
je 0x835f
leaq -0x2c(%rbp), %r12
cmpl $0x0, -0x30(%rbp)
je 0x824a
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x5ea2
testq %rax, %rax
js 0x8238
subl %eax, -0x30(%rbp)
jmp 0x821a
cmpl $0x4, -0x2c(%rbp)
je 0x821a
leaq 0x1ec8(%rip), %rdi      # 0xa10d
jmp 0x81c3
movq %r14, %rdi
callq 0x7728
testq %rax, %rax
je 0x8377
movq %rax, %r13
cmpb $0x1, 0x8(%rax)
je 0x8388
movq 0x20(%r13), %r12
testq %r12, %r12
je 0x82d8
movq %r13, -0x38(%rbp)
leaq 0x1(%r12), %rdi
callq 0x23a8
testq %rax, %rax
je 0x83b6
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 0x82ef
movq %r13, %rdi
callq 0x372f
testq %rax, %rax
je 0x82ef
movq %rax, %r12
movq %r13, -0x40(%rbp)
movq -0x38(%rbp), %rcx
cmpb $0x0, 0x9(%rcx)
je 0x8315
leaq 0x1ecd(%rip), %r13      # 0xa1a3
jmp 0x832a
leaq 0x1ec4(%rip), %rdi      # 0xa1a3
leaq 0xee4(%rip), %rsi       # 0x91ca
xorl %eax, %eax
callq 0x2100
jmp 0x8357
movq -0x38(%rbp), %rax
cmpb $0x1, 0x9(%rax)
jne 0x8302
leaq 0x1ea3(%rip), %rdi      # 0xa1a3
jmp 0x8309
leaq 0x1ea9(%rip), %rdi      # 0xa1b2
movq %r13, %rsi
xorl %eax, %eax
callq 0x2100
jmp 0x834b
movq 0x10(%rcx), %rsi
leaq 0x1e92(%rip), %r13      # 0xa1b2
movq %r13, %rdi
xorl %eax, %eax
callq 0x2100
movq %r12, %rdi
callq 0x373d
movq %r13, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x2100
movq %r12, %rdi
callq 0x3060
movq -0x40(%rbp), %r13
movq %r13, %rdi
callq 0x23a0
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0x7258
movq %rbx, %rdi
callq 0x7471
movq %r14, %rdi
callq 0x51e9
movl %r15d, %edi
callq 0x2160
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6c69(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x1d84(%rip), %rsi      # 0xa11d
leaq 0x1dbf(%rip), %rdx      # 0xa15f
leaq 0x1d4b(%rip), %rcx      # 0xa0f2
movl $0x284, %r8d            # imm = 0x284
xorl %eax, %eax
callq 0x21e0
jmp 0x8357
leaq 0x1db4(%rip), %rdi      # 0xa171
leaq 0x1d2e(%rip), %rsi      # 0xa0f2
leaq 0x1dab(%rip), %rcx      # 0xa176
movl $0x28a, %edx            # imm = 0x28A
callq 0x2150