// File: /workspace/github_c_makefile_star2_O1/back1127[P]linkedge-thing-access-websocket_client_sdk/sdk/leda.c
// Function: leda_send_method_sync
int leda_send_method_sync(const char *pk, const char *dn, const char *method, const char *event_name,
                     const leda_device_data_t *data, int data_cnt)
{
    cJSON *root, *payload, *params, *item, *sub_item;
    unsigned int msg_id;
    char *msg;
    size_t len;
    int ret, code = 0, i;

    if (!pk || !dn || !method) {
        return LE_ERROR_INVAILD_PARAM;
    }

    msg_id = get_msg_id();

    root = cJSON_CreateObject();
    if (!root) {
        return LE_ERROR_ALLOCATING_MEM;
    }
    cJSON_AddStringToObject(root, "version", PROTOCOL_VERSION);
    cJSON_AddNumberToObject(root, "messageId", msg_id);
    cJSON_AddStringToObject(root, "method", method);
    payload = cJSON_CreateObject();
    if (!payload) {
        return LE_ERROR_ALLOCATING_MEM;
    }
    cJSON_AddItemToObject(root, "payload", payload);
    cJSON_AddStringToObject(payload, "productKey", pk);
    cJSON_AddStringToObject(payload, "deviceName", dn);

    if (0 == strcmp(method, METHOD_ONLINE) || 0 == strcmp(method, METHOD_OFFLINE)) {
        goto next_step;
    }
    params = struct_data_to_json_data(data, data_cnt);
    if (!params) {
        cJSON_Delete(root);
        goto next_step;
    }
    if (event_name) {
        cJSON_AddStringToObject(payload, "identifier", event_name);
        cJSON_AddItemToObject(payload, "outputData", params);
    } else {
        cJSON_AddItemToObject(payload, "properties", params);
    }

next_step:
    msg = cJSON_Print(root);
    cJSON_Delete(root);
    if (!msg) {
        return LE_ERROR_ALLOCATING_MEM;
    }
    len = strlen(msg);

    ret = wsa_insert_reply(msg_id);
    if (ret != LE_SUCCESS) {
        free(msg);
        return ret;
    }
    log_i(LOG_TAG, "send messege to server: %s", msg);
    ret = wsc_add_msg(msg, len, 0);
    free(msg);
    if (ret != LE_SUCCESS) {
        wsa_remove_reply(msg_id);
        return ret;
    }
    
    ret = wsa_get_reply_result(msg_id, 3000, &code, NULL);
    if (ret != LE_SUCCESS) {
        log_e(LOG_TAG, "reply code:%d\n", code);
        return ret;
    }

    return code;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r13
movl $0x0, -0x34(%rbp)
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %dl
orb %al, %dl
testq %r13, %r13
sete %al
movl $0x186a1, %r11d         # imm = 0x186A1
orb %dl, %al
jne 0x54bd
movq %rsi, %r15
movq %rdi, %rbx
movq %r8, -0x48(%rbp)
movl %r9d, -0x30(%rbp)
movq %rcx, -0x40(%rbp)
leaq 0xa1cc(%rip), %r14      # 0xf430
movq %r14, %rdi
callq 0x4680
movl 0xa1e5(%rip), %r12d     # 0xf458
incl %r12d
movl %r12d, 0xa1db(%rip)     # 0xf458
movq %r14, %rdi
callq 0x43e0
callq 0x45b0
movl $0x186a2, %r11d         # imm = 0x186A2
testq %rax, %rax
je 0x54bd
movq %rax, %r14
leaq 0x5f56(%rip), %rsi      # 0xb1f9
leaq 0x5f57(%rip), %rdx      # 0xb201
movq %rax, %rdi
callq 0x44f0
movl %r12d, -0x2c(%rbp)
movl %r12d, %eax
cvtsi2sd %rax, %xmm0
leaq 0x5ecf(%rip), %rsi      # 0xb194
movq %r14, %rdi
callq 0x4210
leaq 0x5fbe(%rip), %rsi      # 0xb292
movq %r14, %rdi
movq %r13, %rdx
callq 0x44f0
callq 0x45b0
testq %rax, %rax
je 0x5381
movq %rax, %r12
leaq 0x5edb(%rip), %rsi      # 0xb1d2
movq %r14, %rdi
movq %rax, %rdx
callq 0x45f0
leaq 0x5efd(%rip), %rsi      # 0xb206
movq %r12, %rdi
movq %rbx, %rdx
callq 0x44f0
leaq 0x5ef6(%rip), %rsi      # 0xb211
movq %r12, %rdi
movq %r15, %rdx
callq 0x44f0
leaq 0x5eef(%rip), %rsi      # 0xb21c
movq %r13, %rdi
callq 0x4360
testl %eax, %eax
je 0x53a8
leaq 0x5ee9(%rip), %rsi      # 0xb229
movq %r13, %rdi
callq 0x4360
testl %eax, %eax
je 0x53a8
movq -0x48(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x4570
testq %rax, %rax
je 0x538c
movq %rax, %rbx
movq -0x40(%rbp), %rdx
testq %rdx, %rdx
je 0x5396
leaq 0x5d0a(%rip), %rsi      # 0xb07a
movq %r12, %rdi
callq 0x44f0
leaq 0x5eb8(%rip), %rsi      # 0xb237
jmp 0x539d
movl $0x186a2, %r11d         # imm = 0x186A2
jmp 0x54bd
movq %r14, %rdi
callq 0x41a0
jmp 0x53a8
leaq 0x5f26(%rip), %rsi      # 0xb2c3
movq %r12, %rdi
movq %rbx, %rdx
callq 0x45f0
movq %r14, %rdi
callq 0x40b0
movq %rax, %rbx
movq %r14, %rdi
callq 0x41a0
testq %rbx, %rbx
movl $0x186a2, %r11d         # imm = 0x186A2
je 0x54bd
movq %rbx, %rdi
callq 0x4200
movq %rax, %r14
movl -0x2c(%rbp), %r15d
movl %r15d, %edi
callq 0x4390
testl %eax, %eax
je 0x53f8
movq %rbx, %rdi
movq %rax, %rbx
callq 0x46b0
movq %rbx, %r11
jmp 0x54bd
leaq 0x5e5f(%rip), %r10      # 0xb25e
leaq 0x5e3c(%rip), %rsi      # 0xb242
leaq 0x5c89(%rip), %rdx      # 0xb096
leaq 0x5c87(%rip), %rcx      # 0xb09b
leaq 0x5e2d(%rip), %r8       # 0xb248
movl $0x1, %edi
movl $0x1d0, %r9d            # imm = 0x1D0
xorl %eax, %eax
pushq %rbx
pushq %r10
callq 0x4260
addq $0x10, %rsp
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x4470
movl %eax, %r14d
movq %rbx, %rdi
callq 0x46b0
testl %r14d, %r14d
je 0x545e
movl %r15d, %edi
callq 0x4380
movq %r14, %r11
jmp 0x54bd
leaq -0x34(%rbp), %r14
movl %r15d, %edi
movl $0xbb8, %esi            # imm = 0xBB8
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x4030
movl (%r14), %r11d
testl %eax, %eax
je 0x54bd
movl %eax, %ebx
leaq 0x5df5(%rip), %r10      # 0xb279
leaq 0x5c05(%rip), %rsi      # 0xb090
leaq 0x5c04(%rip), %rdx      # 0xb096
leaq 0x5c02(%rip), %rcx      # 0xb09b
leaq 0x5da8(%rip), %r8       # 0xb248
movl $0x3, %edi
movl $0x1da, %r9d            # imm = 0x1DA
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x4260
addq $0x10, %rsp
movl %ebx, %r11d
movl %r11d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq