// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, (%rsp)
movl %edi, %r12d
callq 0x2fcb
movq 0x140c8(%rax), %r14
callq 0x2fcb
movq %rax, %r15
movslq %r12d, %rax
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x2c, %rcx
addl %edx, %ecx
shll $0xb, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
movslq %eax, %r13
movq 0x18(%r15,%r13,8), %rbx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rbx, %rbx
je 0x2ac6
cmpl %r12d, 0x10(%rbx)
jne 0x2ac6
movl 0x14(%rbx), %ecx
movslq 0x18(%rbx), %rax
cmpl %eax, %ecx
jne 0x2a09
cmpl $0x1, 0x50(%rbx)
je 0x2ac6
leaq 0x38(%rbx), %r13
movq 0x38(%rbx), %r15
cmpq %r15, %r13
je 0x2a71
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rax, (%rcx)
movq %rcx, 0x8(%rax)
movq 0x10(%r15), %rax
addq $-0x100, %r15
testq %rax, %rax
cmovneq %rax, %r15
callq 0x2fcb
leaq 0x140d0(%rax), %rcx
movq 0x140d8(%rax), %rdx
leaq 0x118(%r15), %rsi
movq %rdx, 0x120(%r15)
movq %rsi, (%rdx)
movq %rcx, 0x118(%r15)
movq %rsi, 0x140d8(%rax)
movq 0x138(%r15), %rsi
movq (%rbx), %rdx
movq (%rsp), %rdi
callq 0x11b0
xorl %ebp, %ebp
jmp 0x2ac6
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x2a4c
movslq %ecx, %rsi
incq %rax
xorl %edx, %edx
divq %rdi
cmpq %rsi, %rdx
je 0x2a4c
movq (%rbx), %rdx
imulq %rdx, %rsi
addq %rbx, %rsi
addq $0x54, %rsi
movq (%rsp), %rdi
callq 0x11b0
movslq 0x14(%rbx), %rax
incq %rax
xorl %edx, %edx
divq 0x8(%rbx)
movq %rdx, %rcx
movl %ecx, 0x14(%rbx)
xorl %ebp, %ebp
cmpl $0x1, 0x50(%rbx)
jne 0x2ac6
cmpl 0x18(%rbx), %ecx
jne 0x2ac6
movq %rbx, %rdi
callq 0x1030
movq $0x0, 0x18(%r15,%r13,8)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2ac6
leaq 0x20(%rbx), %rax
movq 0x28(%rbx), %rcx
leaq 0x100(%r14), %rdx
movq %rcx, 0x108(%r14)
movq %rdx, (%rcx)
movq %rax, 0x100(%r14)
movq %rdx, 0x28(%rbx)
callq 0x2fcb
movq 0x140c0(%rax), %rsi
movq 0x140c8(%rax), %rdi
addq $0x48, %rdi
addq $0x48, %rsi
callq 0x30b0
cmpl %r12d, 0x10(%rbx)
jne 0x2ac6
cmpl $0x1, 0x50(%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jne 0x2ad7
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %r15
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rax, (%rcx)
movq %rcx, 0x8(%rax)
movq 0x10(%r15), %rax
addq $-0x100, %r15
testq %rax, %rax
cmovneq %rax, %r15
jmp 0x29ef