// File: /workspace/github_c_makefile_star2_O0/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
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
callq 0xbb30
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xd20c
movb $0x0, -0x1(%rbp)
jmp 0xd5a1
movq -0x20(%rbp), %rdi
callq 0xbe70
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xd232
movq -0x20(%rbp), %rdi
callq 0xbde0
movb $0x0, -0x1(%rbp)
jmp 0xd5a1
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0xa070
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0xd254
jmp 0xd579
movl -0x2c(%rbp), %edi
movq -0x10(%rbp), %rcx
addq $0x30, %rcx
movl $0x1, %esi
movl $0x15, %edx
movl $0x10, %r8d
callq 0x20a0
movl -0x2c(%rbp), %edi
movq -0x10(%rbp), %rcx
addq $0x30, %rcx
movl $0x1, %esi
movl $0x14, %edx
movl $0x10, %r8d
callq 0x20a0
movl -0x2c(%rbp), %eax
movl %eax, -0x84(%rbp)
movq -0x28(%rbp), %rdi
callq 0x83b0
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rdi
callq 0x8330
movl -0x84(%rbp), %edi
movq -0x80(%rbp), %rsi
movq %rax, %rdx
callq 0xa500
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
callq 0x8330
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
je 0xd2eb
jmp 0xd579
movq -0x28(%rbp), %rdi
callq 0x8780
movl $0x0, -0x3c(%rbp)
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
leaq -0x3c(%rbp), %rdx
callq 0x9970
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jge 0xd32f
cmpl $0x4, -0x3c(%rbp)
jne 0xd31e
jmp 0xd2fb
leaq 0x31c4(%rip), %rdi      # 0x104e9
callq 0x2300
jmp 0xd579
movq -0x28(%rbp), %rdi
callq 0xc3d0
testb $0x1, %al
jne 0xd341
jmp 0xd579
jmp 0xd343
jmp 0xd345
movl $0x0, -0x4c(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0xc420
testb $0x1, %al
jne 0xd362
jmp 0xd579
jmp 0xd364
cmpl $0x0, -0x4c(%rbp)
je 0xd3ae
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
leaq -0x3c(%rbp), %rdx
callq 0x9970
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jge 0xd39e
cmpl $0x4, -0x3c(%rbp)
jne 0xd38d
jmp 0xd364
leaq 0x3161(%rip), %rdi      # 0x104f5
callq 0x2300
jmp 0xd579
movq -0x58(%rbp), %rcx
movslq -0x4c(%rbp), %rax
subq %rcx, %rax
movl %eax, -0x4c(%rbp)
jmp 0xd364
movq -0x28(%rbp), %rdi
callq 0xc000
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0xd3cb
movb $0x0, -0x1(%rbp)
jmp 0xd5a1
movq -0x60(%rbp), %rax
testb $0x1, 0x8(%rax)
je 0xd406
movq 0x7c1c(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
leaq 0x311f(%rip), %rsi      # 0x10505
leaq 0x315a(%rip), %rdx      # 0x10547
leaq 0x30e6(%rip), %rcx      # 0x104da
movl $0x284, %r8d            # imm = 0x284
movb $0x0, %al
callq 0x2220
jmp 0xd570
movq -0x60(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0xd54c
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x1, %rdi
callq 0x23f8
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0xd433
jmp 0xd452
leaq 0x311f(%rip), %rdi      # 0x10559
leaq 0x3099(%rip), %rsi      # 0x104da
movl $0x28a, %edx            # imm = 0x28A
leaq 0x3111(%rip), %rcx      # 0x1055e
callq 0x2170
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x2240
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
movq 0x20(%rcx), %rcx
movb $0x0, (%rax,%rcx)
movq $0x0, -0x70(%rbp)
movq -0x68(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5b, %eax
je 0xd49b
movq -0x68(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x7b, %eax
jne 0xd50f
movq -0x68(%rbp), %rdi
callq 0x4210
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0xd50f
movq -0x60(%rbp), %rax
testb $0x1, 0x9(%rax)
je 0xd4d4
movq -0x70(%rbp), %rdi
callq 0x4240
movq %rax, %rsi
leaq 0x30c0(%rip), %rdi      # 0x1058b
movb $0x0, %al
callq 0x2120
jmp 0xd504
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0x30b7(%rip), %rdi      # 0x1059a
movb $0x0, %al
callq 0x2120
movq -0x70(%rbp), %rdi
callq 0x4240
movq %rax, %rsi
leaq 0x309d(%rip), %rdi      # 0x1059a
movb $0x0, %al
callq 0x2120
movq -0x70(%rbp), %rdi
callq 0x3b30
jmp 0xd541
movq -0x60(%rbp), %rax
testb $0x1, 0x9(%rax)
je 0xd52d
movq -0x68(%rbp), %rsi
leaq 0x3067(%rip), %rdi      # 0x1058b
movb $0x0, %al
callq 0x2120
jmp 0xd53f
movq -0x68(%rbp), %rsi
leaq 0x3062(%rip), %rdi      # 0x1059a
movb $0x0, %al
callq 0x2120
jmp 0xd541
movq -0x68(%rbp), %rdi
callq 0x23f0
jmp 0xd56e
movq -0x60(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0xd56c
leaq 0x302d(%rip), %rdi      # 0x1058b
leaq 0x1d6f(%rip), %rsi      # 0xf2d4
movb $0x0, %al
callq 0x2120
jmp 0xd56e
jmp 0xd570
movq -0x60(%rbp), %rdi
callq 0xbac0
movq -0x20(%rbp), %rdi
callq 0xbde0
movq -0x28(%rbp), %rdi
callq 0x8270
cmpl $-0x1, -0x2c(%rbp)
je 0xd599
movl -0x2c(%rbp), %edi
callq 0x21a0
movb -0x11(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
nop