// File: /workspace/github_c_makefile_star2_O0/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
subq $0x20, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
leaq 0xaec5(%rip), %rdi      # 0x12648
xorl %esi, %esi
movl $0x38, %edx
callq 0x4300
leaq 0xaeb2(%rip), %rdi      # 0x12648
xorl %eax, %eax
movl %eax, %esi
callq 0x4750
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x77c2
leaq 0x6e98(%rip), %rdi      # 0xe647
movb $0x0, %al
callq 0x4290
movl $0x186a3, -0x4(%rbp)    # imm = 0x186A3
jmp 0x7958
movl $0x0, 0xaea4(%rip)      # 0x12670
movl $0x0, 0xae9e(%rip)      # 0x12674
movl -0x8(%rbp), %eax
movl %eax, 0xae99(%rip)      # 0x12678
movl -0xc(%rbp), %eax
movl %eax, 0xae94(%rip)      # 0x1267c
movslq -0xc(%rbp), %rax
imulq $0x18, %rax, %rdi
callq 0x4798
movq %rax, 0xae84(%rip)      # 0x12680
cmpq $0x0, 0xae7c(%rip)      # 0x12680
jne 0x782c
leaq 0x6e59(%rip), %rdi      # 0xe666
movb $0x0, %al
callq 0x4290
leaq 0xae2d(%rip), %rdi      # 0x12648
callq 0x42c0
movl $0x186a2, -0x4(%rbp)    # imm = 0x186A2
jmp 0x7958
movq 0xae4d(%rip), %rdi      # 0x12680
movslq -0xc(%rbp), %rax
imulq $0x18, %rax, %rdx
xorl %esi, %esi
callq 0x4300
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x78b7
movslq -0x8(%rbp), %rdi
callq 0x4798
movq %rax, %rcx
movq 0xae1c(%rip), %rax      # 0x12680
movslq -0x10(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq 0xae07(%rip), %rax      # 0x12680
movslq -0x10(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jne 0x788c
jmp 0x78c3
movq 0xaded(%rip), %rax      # 0x12680
movslq -0x10(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movslq -0x8(%rbp), %rdx
xorl %esi, %esi
callq 0x4300
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x7849
movl $0x0, -0x4(%rbp)
jmp 0x7958
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x792e
movq 0xada7(%rip), %rax      # 0x12680
movslq -0x10(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0x791f
movq 0xad8f(%rip), %rax      # 0x12680
movslq -0x10(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x4790
movq 0xad75(%rip), %rax      # 0x12680
movslq -0x10(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq $0x0, (%rax)
jmp 0x7921
jmp 0x792e
jmp 0x7923
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x78ca
movq 0xad4b(%rip), %rdi      # 0x12680
callq 0x4790
movq $0x0, 0xad3b(%rip) # 0x12680
leaq 0xacfc(%rip), %rdi      # 0x12648
callq 0x42c0
movl $0x186a2, -0x4(%rbp)    # imm = 0x186A2
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)