// File: /workspace/github_c_makefile_star2_O0/whtc123[P]libuvc/uvc.c
// Function: channel_read
int channel_read(channel_t c,void *buf){
	channel *chan = NULL;
	uvc_ctx *ctx=uvc_self();
	void *p=NULL;
	queue_t *node = NULL;
	channel_pool *pool = get_chan_pool();
	chan = channel_pool_get(pool,c);
	if(chan == NULL ||chan->id !=c)return -1;
	//当管道关闭的时候，buf中数据任然可读
	if (!chanbuf_empty(chan) ){
		chanbuf_pop(chan,buf);
		if(chan->closeing==1 &&chanbuf_empty(chan)){
			//管道关闭状态，且buf中数据已经读完，失败chan
			free(chan); 
			channel_pool_remove(pool,c);
			return -1;
		}
		return 0;
	}
	else if (chan->closeing == 1){
		return -1;
	}
	
	// 没有协程等待写，入队，调度
	if(queue_empty(&chan->writq) ){
		channel_queue_put(&chan->readq, ctx);
		//queue_add(&chan->readq, &ctx->i_node);
		uvc_yield();
		if(chan == NULL ||chan->id !=c || chan->closeing==1){
			return -1;
		}
		ctx = channel_queue_get(&chan->writq);
        
	}else {
		ctx = channel_queue_get(&chan->writq);
		uvc_ready(ctx);
		//uvc_ready(uvc_self());
		//uvc_yield();
		//uvc_io_ready(ctx);
		//uvc_resume(ctx);
	}
	memcpy(buf, ctx->cbuf, chan->size);
	return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
callq 0x3890
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
callq 0x38c0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl -0x8(%rbp), %esi
callq 0x2660
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x2cfc
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
cmpl -0x8(%rbp), %eax
je 0x2d08
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2e38
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
je 0x2d83
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2e50
movq -0x18(%rbp), %rax
cmpl $0x1, 0x50(%rax)
jne 0x2d77
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x2d77
movq -0x18(%rbp), %rdi
callq 0x1030
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x8(%rbp), %eax
movl $0x2800, %ecx           # imm = 0x2800
cltd
idivl %ecx
movq -0x40(%rbp), %rax
movslq %edx, %rcx
movq $0x0, 0x8(%rax,%rcx,8)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2e38
movl $0x0, -0x4(%rbp)
jmp 0x2e38
movq -0x18(%rbp), %rax
cmpl $0x1, 0x50(%rax)
jne 0x2d99
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2e38
jmp 0x2d9b
movq -0x18(%rbp), %rax
addq $0x38, %rax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jne 0x2dfc
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
movq -0x20(%rbp), %rsi
callq 0x2c40
callq 0x1880
cmpq $0x0, -0x18(%rbp)
je 0x2de0
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
cmpl -0x8(%rbp), %eax
jne 0x2de0
movq -0x18(%rbp), %rax
cmpl $0x1, 0x50(%rax)
jne 0x2de9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2e38
movq -0x18(%rbp), %rdi
addq $0x38, %rdi
callq 0x29c0
movq %rax, -0x20(%rbp)
jmp 0x2e16
movq -0x18(%rbp), %rdi
addq $0x38, %rdi
callq 0x29c0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x18c0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x138(%rax), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
callq 0x1190
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)