// File: /workspace/github_c_makefile_star2_O1/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, %ebp
callq 0x3031
movq 0x140c8(%rax), %rbx
callq 0x3031
movq %rax, %r15
movslq %ebp, %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), %r14
testq %r14, %r14
je 0x29a1
cmpl %ebp, 0x10(%r14)
je 0x29a4
xorl %r14d, %r14d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testq %r14, %r14
je 0x2b1c
cmpl %ebp, 0x10(%r14)
jne 0x2b1c
movslq 0x14(%r14), %rcx
movslq 0x18(%r14), %rax
cmpl %eax, %ecx
jne 0x2a59
cmpl $0x1, 0x50(%r14)
je 0x2b1c
leaq 0x38(%r14), %r13
movq 0x38(%r14), %r15
cmpq %r15, %r13
je 0x2ac5
movq (%r15), %rax
movq 0x8(%r15), %rcx
movq %rax, (%rcx)
movq (%r15), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%r15), %rax
addq $-0x100, %r15
testq %rax, %rax
cmovneq %rax, %r15
callq 0x3031
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 (%r14), %rdx
movq (%rsp), %rdi
callq 0x11b0
xorl %r12d, %r12d
jmp 0x2b1c
movq 0x8(%r14), %rsi
testq %rsi, %rsi
je 0x2a98
incq %rax
xorl %edx, %edx
divq %rsi
cmpq %rcx, %rdx
je 0x2a98
movq (%r14), %rdx
imulq %rdx, %rcx
leaq (%r14,%rcx), %rsi
addq $0x54, %rsi
movq (%rsp), %rdi
callq 0x11b0
movslq 0x14(%r14), %rax
incq %rax
xorl %edx, %edx
divq 0x8(%r14)
movl %edx, 0x14(%r14)
xorl %r12d, %r12d
cmpl $0x1, 0x50(%r14)
jne 0x2b1c
movl 0x14(%r14), %eax
cmpl 0x18(%r14), %eax
jne 0x2b1c
movq %r14, %rdi
callq 0x1030
movq $0x0, 0x18(%r15,%r13,8)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x2b1c
leaq 0x20(%r14), %rax
movq 0x28(%r14), %rcx
leaq 0x100(%rbx), %rdx
movq %rcx, 0x108(%rbx)
movq %rdx, (%rcx)
movq %rax, 0x100(%rbx)
movq %rdx, 0x28(%r14)
callq 0x3031
movq 0x140c0(%rax), %rsi
movq 0x140c8(%rax), %rdi
addq $0x48, %rdi
addq $0x48, %rsi
callq 0x312c
cmpl %ebp, 0x10(%r14)
jne 0x2b1c
cmpl $0x1, 0x50(%r14)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jne 0x2b2e
movl %r12d, %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 (%r15), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%r15), %rax
addq $-0x100, %r15
testq %rax, %rax
cmovneq %rax, %r15
jmp 0x2a3e