// File: /workspace/github_c_makefile_star2_O1/liuhaotian[P]xmppsh/libstrophe/src/event.c
// Function: xmpp_run_once
void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
{
    xmpp_connlist_t *connitem;
    xmpp_conn_t *conn;
    fd_set rfds, wfds;
    sock_t max = 0;
    int ret;
    struct timeval tv;
    xmpp_send_queue_t *sq, *tsq;
    int towrite;
    char buf[4096];
    uint64_t next;
    long usec;
    int tls_read_bytes = 0;

    if (ctx->loop_status == XMPP_LOOP_QUIT) return;
    ctx->loop_status = XMPP_LOOP_RUNNING;

    /* send queued data */
    connitem = ctx->connlist;
    while (connitem) {
	conn = connitem->conn;
	if (conn->state != XMPP_STATE_CONNECTED) {
	    connitem = connitem->next;
	    continue;
	}

	/* if we're running tls, there may be some remaining data waiting to
	 * be sent, so push that out */
	if (conn->tls) {
	    ret = tls_clear_pending_write(conn->tls);

	    if (ret < 0 && !tls_is_recoverable(tls_error(conn->tls))) {
		/* an error occured */
		xmpp_debug(ctx, "xmpp", "Send error occured, disconnecting.");
		conn->error = ECONNABORTED;
		conn_disconnect(conn);
	    }
	}

	/* write all data from the send queue to the socket */
	sq = conn->send_queue_head;
	while (sq) {
	    towrite = sq->len - sq->written;

	    if (conn->tls) {
		ret = tls_write(conn->tls, &sq->data[sq->written], towrite);

		if (ret < 0 && !tls_is_recoverable(tls_error(conn->tls))) {
		    /* an error occured */
		    conn->error = tls_error(conn->tls);
		    break;
		} else if (ret < towrite) {
		    /* not all data could be sent now */
		    if (ret >= 0) sq->written += ret;
		    break;
		}

	    } else {
		ret = sock_write(conn->sock, &sq->data[sq->written], towrite);

		if (ret < 0 && !sock_is_recoverable(sock_error())) {
		    /* an error occured */
		    conn->error = sock_error();
		    break;
		} else if (ret < towrite) {
		    /* not all data could be sent now */
		    if (ret >= 0) sq->written += ret;
		    break;
		}
	    }

	    /* all data for this queue item written, delete and move on */
	    xmpp_free(ctx, sq->data);
	    tsq = sq;
	    sq = sq->next;
	    xmpp_free(ctx, tsq);

	    /* pop the top item */
	    conn->send_queue_head = sq;
	    /* if we've sent everything update the tail */
	    if (!sq) conn->send_queue_tail = NULL;
	}

	/* tear down connection on error */
	if (conn->error) {
	    /* FIXME: need to tear down send queues and random other things
	     * maybe this should be abstracted */
	    xmpp_debug(ctx, "xmpp", "Send error occured, disconnecting.");
	    conn->error = ECONNABORTED;
	    conn_disconnect(conn);
	}
	
	connitem = connitem->next;
    }

    /* reset parsers if needed */
    for (connitem = ctx->connlist; connitem; connitem = connitem->next) {
	if (connitem->conn->reset_parser)
	    conn_parser_reset(connitem->conn);
    }


    /* fire any ready timed handlers, then
       make sure we don't wait past the time when timed handlers need 
       to be called */
    next = handler_fire_timed(ctx);

    usec = ((next < timeout) ? next : timeout) * 1000;
    tv.tv_sec = usec / 1000000;
    tv.tv_usec = usec % 1000000;

    FD_ZERO(&rfds); 
    FD_ZERO(&wfds);

    /* find events to watch */
    connitem = ctx->connlist;
    while (connitem) {
	conn = connitem->conn;
	
	switch (conn->state) {
	case XMPP_STATE_CONNECTING:
	    /* connect has been called and we're waiting for it to complete */
	    /* connection will give us write or error events */
	    
	    /* make sure the timeout hasn't expired */
	    if (time_elapsed(conn->timeout_stamp, time_stamp()) <= 
		conn->connect_timeout)
		FD_SET(conn->sock, &wfds);
	    else {
		conn->error = ETIMEDOUT;
		xmpp_info(ctx, "xmpp", "Connection attempt timed out.");
		conn_disconnect(conn);
	    }
	    break;
	case XMPP_STATE_CONNECTED:
	    FD_SET(conn->sock, &rfds);
	    break;
	case XMPP_STATE_DISCONNECTED:
	    /* do nothing */
	default:
	    break;
	}
	
	/* Check if there is something in the SSL buffer. */
	if (conn->tls) {
	    tls_read_bytes += tls_pending(conn->tls);
	}
	
	if (conn->sock > max) max = conn->sock;

	connitem = connitem->next;
    }

    /* check for events */
    ret = select(max + 1, &rfds,  &wfds, NULL, &tv);

    /* select errored */
    if (ret < 0) {
	if (!sock_is_recoverable(sock_error()))
	    xmpp_error(ctx, "xmpp", "event watcher internal error %d", 
		       sock_error());
	return;
    }
    
    /* no events happened */
    if (ret == 0 && tls_read_bytes == 0) return;

    /* process events */
    connitem = ctx->connlist;
    while (connitem) {
	conn = connitem->conn;

	switch (conn->state) {
	case XMPP_STATE_CONNECTING:
	    if (FD_ISSET(conn->sock, &wfds)) {
		/* connection complete */

		/* check for error */
		if (sock_connect_error(conn->sock) != 0) {
		    /* connection failed */
		    xmpp_debug(ctx, "xmpp", "connection failed");
		    conn_disconnect(conn);
		    break;
		}

		conn->state = XMPP_STATE_CONNECTED;
		xmpp_debug(ctx, "xmpp", "connection successful");

		
		/* send stream init */
		conn_open_stream(conn);
	    }

	    break;
	case XMPP_STATE_CONNECTED:
	    if (FD_ISSET(conn->sock, &rfds) || (conn->tls && tls_pending(conn->tls))) {
		if (conn->tls) {
		    ret = tls_read(conn->tls, buf, 4096);
		} else {
		    ret = sock_read(conn->sock, buf, 4096);
		}

		if (ret > 0) {
		    ret = parser_feed(conn->parser, buf, ret);
		    if (!ret) {
			/* parse error, we need to shut down */
			/* FIXME */
			xmpp_debug(ctx, "xmpp", "parse error, disconnecting");
			conn_disconnect(conn);
		    }
		} else {
		    if (conn->tls) {
			if (!tls_is_recoverable(tls_error(conn->tls)))
			{
			    xmpp_debug(ctx, "xmpp", "Unrecoverable TLS error, %d.", tls_error(conn->tls));
			    conn->error = tls_error(conn->tls);
			    conn_disconnect(conn);
			}
		    } else {
			/* return of 0 means socket closed by server */
			xmpp_debug(ctx, "xmpp", "Socket closed by remote host.");
			conn->error = ECONNRESET;
			conn_disconnect(conn);
		    }
		}
	    }

	    break;
	case XMPP_STATE_DISCONNECTED:
	    /* do nothing */
	default:
	    break;
	}

	connitem = connitem->next;
    }

    /* fire any ready handlers */
    handler_fire_timed(ctx);
}

// Assembly:
cmpl $0x2, 0x10(%rdi)
je 0x5c0f
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1128, %rsp           # imm = 0x1128
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movl $0x1, 0x10(%rdi)
movq 0x18(%rdi), %r12
testq %r12, %r12
je 0x57fb
movq (%r12), %r13
cmpl $0x2, 0x14(%r13)
jne 0x57f1
movq 0x38(%r13), %rdi
testq %rdi, %rdi
je 0x56ec
callq 0xc25e
testl %eax, %eax
jns 0x56ec
movq 0x38(%r13), %rdi
callq 0xc000
movl %eax, %edi
callq 0xc1df
testl %eax, %eax
jne 0x56ec
movq %rbx, %rdi
leaq 0x797b(%rip), %rsi      # 0xd049
leaq 0x85ab(%rip), %rdx      # 0xdc80
xorl %eax, %eax
callq 0x5546
movl $0x67, 0x20(%r13)
movq %r13, %rdi
callq 0x4e8b
movq 0xa8(%r13), %rbp
testq %rbp, %rbp
je 0x57c2
movq 0x8(%rbp), %r14
movq 0x10(%rbp), %rsi
subq %rsi, %r14
movq 0x38(%r13), %rdi
testq %rdi, %rdi
je 0x5739
addq (%rbp), %rsi
movslq %r14d, %rdx
callq 0xc232
movl %eax, %r15d
testl %eax, %eax
jns 0x5760
movq 0x38(%r13), %rdi
callq 0xc000
movl %eax, %edi
callq 0xc1df
testl %eax, %eax
jne 0x5760
jmp 0x57ae
movl 0x30(%r13), %edi
addq (%rbp), %rsi
movslq %r14d, %rdx
callq 0xac8d
movl %eax, %r15d
testl %eax, %eax
jns 0x5760
callq 0xab82
movl %eax, %edi
callq 0xac94
testl %eax, %eax
je 0x57b9
cmpl %r14d, %r15d
jl 0x57a0
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x5293
movq 0x18(%rbp), %r14
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5293
movq %r14, 0xa8(%r13)
movq %r14, %rbp
testq %r14, %r14
jne 0x56fc
movq $0x0, 0xb0(%r13)
jmp 0x57c2
testl %r15d, %r15d
js 0x57c2
movl %r15d, %eax
addq %rax, 0x10(%rbp)
jmp 0x57c2
movq 0x38(%r13), %rdi
callq 0xc000
jmp 0x57be
callq 0xab82
movl %eax, 0x20(%r13)
cmpl $0x0, 0x20(%r13)
je 0x57f1
movq %rbx, %rdi
leaq 0x7876(%rip), %rsi      # 0xd049
leaq 0x84a6(%rip), %rdx      # 0xdc80
xorl %eax, %eax
callq 0x5546
movl $0x67, 0x20(%r13)
movq %r13, %rdi
callq 0x4e8b
movq 0x8(%r12), %r12
jmp 0x5686
movq 0x18(%rbx), %r14
testq %r14, %r14
je 0x581b
movq (%r14), %rdi
cmpl $0x0, 0xb8(%rdi)
je 0x5815
callq 0x4ef6
movq 0x8(%r14), %r14
jmp 0x57ff
movq %rbx, %rdi
callq 0x5ed0
movq 0x8(%rsp), %rcx
cmpq %rcx, %rax
cmovbq %rax, %rcx
imulq $0x3e8, %rcx, %rcx      # imm = 0x3E8
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
movq %rcx, %rax
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
movq %rdx, 0x10(%rsp)
imulq $0xf4240, %rdx, %rax    # imm = 0xF4240
subq %rax, %rcx
movq %rcx, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movq 0x18(%rbx), %r14
testq %r14, %r14
je 0x59d3
xorl %r15d, %r15d
xorl %ebp, %ebp
movq (%r14), %r12
movl 0x14(%r12), %eax
cmpl $0x2, %eax
je 0x594e
cmpl $0x1, %eax
jne 0x599f
movq 0x18(%r12), %r13
callq 0xc2c0
movq %r13, %rdi
movq %rax, %rsi
callq 0xc2fe
movl 0xc8(%r12), %ecx
cmpq %rcx, %rax
jbe 0x5979
movl $0x6e, 0x20(%r12)
movq %rbx, %rdi
leaq 0x7713(%rip), %rsi      # 0xd049
leaq 0x8366(%rip), %rdx      # 0xdca3
xorl %eax, %eax
callq 0x54b8
movq %r12, %rdi
callq 0x4e8b
jmp 0x599f
movl 0x30(%r12), %ecx
leal 0x3f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, 0xa0(%rsp,%rax,8)
jmp 0x599f
movl 0x30(%r12), %ecx
leal 0x3f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, 0x20(%rsp,%rax,8)
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x59b1
callq 0xc1fd
addl %eax, %r15d
movl 0x30(%r12), %eax
cmpl %ebp, %eax
cmovgl %eax, %ebp
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0x58eb
incl %ebp
testl %r15d, %r15d
sete %r14b
jmp 0x59db
movb $0x1, %r14b
movl $0x1, %ebp
leaq 0xa0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x10(%rsp), %r8
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x2040
testl %eax, %eax
js 0x5bc5
sete %al
testb %r14b, %al
jne 0x5bfe
movq 0x18(%rbx), %rbp
testq %rbp, %rbp
je 0x5bf6
leaq 0x120(%rsp), %r14
leaq 0x7623(%rip), %r15      # 0xd049
leaq 0x82b4(%rip), %r13      # 0xdce1
movq (%rbp), %r12
movl 0x14(%r12), %eax
cmpl $0x2, %eax
je 0x5a80
cmpl $0x1, %eax
jne 0x5b94
movl 0x30(%r12), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0x20(%rsp,%rax,8), %rax
btq %rdi, %rax
jae 0x5b94
callq 0xaca6
testl %eax, %eax
je 0x5b6f
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
jmp 0x5b13
movl 0x30(%r12), %eax
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0xa0(%rsp,%rcx,8), %rcx
btq %rax, %rcx
jae 0x5aba
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x5ad6
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rsi
callq 0xc206
jmp 0x5ae8
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x5b94
callq 0xc1fd
testl %eax, %eax
jne 0x5aa1
jmp 0x5b94
movl 0x30(%r12), %edi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rsi
callq 0xac86
testl %eax, %eax
jle 0x5b24
movq 0xc0(%r12), %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x78df
testl %eax, %eax
jne 0x5b94
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x81f6(%rip), %rdx      # 0xdd09
xorl %eax, %eax
callq 0x5546
movq %r12, %rdi
callq 0x4e8b
jmp 0x5b94
movq 0x38(%r12), %rdi
testq %rdi, %rdi
je 0x5ba3
callq 0xc000
movl %eax, %edi
callq 0xc1df
testl %eax, %eax
jne 0x5b94
movq 0x38(%r12), %rdi
callq 0xc000
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x81cf(%rip), %rdx      # 0xdd24
movl %eax, %ecx
xorl %eax, %eax
callq 0x5546
movq 0x38(%r12), %rdi
callq 0xc000
movl %eax, 0x20(%r12)
jmp 0x5b1a
movl $0x2, 0x14(%r12)
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x816e(%rip), %rdx      # 0xdcf3
xorl %eax, %eax
callq 0x5546
movq %r12, %rdi
callq 0x51da
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
jne 0x5a2d
jmp 0x5bf6
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x8191(%rip), %rdx      # 0xdd41
xorl %eax, %eax
callq 0x5546
movl $0x68, 0x20(%r12)
jmp 0x5b1a
callq 0xab82
movl %eax, %edi
callq 0xac94
testl %eax, %eax
jne 0x5bfe
callq 0xab82
leaq 0x7468(%rip), %rsi      # 0xd049
leaq 0x80d9(%rip), %rdx      # 0xdcc1
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x539c
jmp 0x5bfe
movq %rbx, %rdi
callq 0x5ed0
addq $0x1128, %rsp           # imm = 0x1128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq