// File: /workspace/github_c_makefile_star2_O1/back1127[P]linkedge-thing-access-websocket_client_sdk/sdk/ali_ws/wsc_buffer_mgmt.c
// Function: client_buf_mgmt_init
int client_buf_mgmt_init(int single_buf_size, int max_buf_cnt)
{
	int i = 0;
    int ret = 0;

    memset(&g_ws_buf_state, 0, sizeof(msg_buf_status));
    ret = pthread_mutex_init(&g_ws_buf_state.locker, NULL);
    if(ret != 0){
        printf("failed to init buffer locker.\n");
        return LE_ERROR_CREATING_MUTEX; 
    }
    g_ws_buf_state.rd_index = 0;
	g_ws_buf_state.wr_index = 0;
    g_ws_buf_state.single_buf_size = single_buf_size;
    g_ws_buf_state.max_buf_cnt = max_buf_cnt;

    g_ws_buf = malloc(max_buf_cnt * sizeof(msg_buf_item));
    if(!g_ws_buf){
        printf("failed to malloc memory for client buffer mgmt\n");
        pthread_mutex_destroy(&g_ws_buf_state.locker);
        return LE_ERROR_ALLOCATING_MEM;
    }
    memset(g_ws_buf, 0, sizeof(msg_buf_item) * max_buf_cnt);
	for (i = 0; i < max_buf_cnt; i++) {
        g_ws_buf[i].buf = malloc(single_buf_size);
        if(!g_ws_buf[i].buf)
            goto _failed;
		memset(g_ws_buf[i].buf, 0, single_buf_size);
	}

    return 0;
_failed:
    for(i = 0; i < max_buf_cnt; i++){
        if(g_ws_buf[i].buf){
            free(g_ws_buf[i].buf);
            g_ws_buf[i].buf = NULL;
        }else
            break;
    }
    
    free(g_ws_buf);
    g_ws_buf = NULL;
    pthread_mutex_destroy(&g_ws_buf_state.locker);

	return LE_ERROR_ALLOCATING_MEM;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %r14d
movl %edi, %r15d
xorps %xmm0, %xmm0
movups %xmm0, 0x8c0e(%rip)     # 0xf548
movups %xmm0, 0x8bf7(%rip)     # 0xf538
movups %xmm0, 0x8be0(%rip)     # 0xf528
movq $0x0, 0x8c05(%rip) # 0xf558
leaq 0x8bce(%rip), %rdi      # 0xf528
xorl %esi, %esi
callq 0x4660
testl %eax, %eax
je 0x697c
leaq 0x4d06(%rip), %rdi      # 0xb672
callq 0x40d0
movl $0x186a3, %r12d         # imm = 0x186A3
jmp 0x6a76
movq $0x0, 0x8bc9(%rip) # 0xf550
movl %r15d, 0x8bca(%rip)     # 0xf558
movl %r14d, 0x8bc7(%rip)     # 0xf55c
movslq %r14d, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r13
movq %r13, %rdi
callq 0x46b8
movq %rax, 0x8bb1(%rip)      # 0xf560
testq %rax, %rax
je 0x6a15
movq %rax, %rbx
xorl %r12d, %r12d
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x42b0
testl %r14d, %r14d
jle 0x6a76
movslq %r15d, %r15
movl %r14d, %eax
movq %rax, -0x30(%rbp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %r13
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x46b8
movq %rax, (%rbx,%r14)
testq %rax, %rax
je 0x6a23
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x42b0
addq $0x18, %r14
cmpq %r14, %r13
jne 0x69ec
jmp 0x6a76
leaq 0x4c27(%rip), %rdi      # 0xb643
callq 0x40d0
jmp 0x6a64
movq -0x30(%rbp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r14
xorl %r15d, %r15d
movq (%rbx,%r15), %rdi
testq %rdi, %rdi
je 0x6a51
callq 0x46b0
movq $0x0, (%rbx,%r15)
addq $0x18, %r15
cmpq %r15, %r14
jne 0x6a32
movq %rbx, %rdi
callq 0x46b0
movq $0x0, 0x8afc(%rip) # 0xf560
leaq 0x8abd(%rip), %rdi      # 0xf528
callq 0x4270
movl $0x186a2, %r12d         # imm = 0x186A2
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq