// File: /workspace/github_c_makefile_star2_O2/xiaowenhuyuehu[P]remote_access/src/core/remote_terminal.c
// Function: sda_run_loop
int sda_run_loop (void)
{
    char *buf = NULL, *buf_hdr = NULL, *session_id = NULL, *tmp_buf = NULL;
    int ret = 0, tmp = 0, tmp_fd = 0, payload = 0, tmp_index = 0, timeout = 0;
    struct timeval tv;
    ConnectInfo_t connInfo;
    fd_set rfds;
    int socketfd_local[DEFAULT_SESSION_COUNT] = {0};
    RemoteTerminalMsgHeader hdr;
    
    buf = malloc(DEFAULT_MSG_BUFFER_LEN);
    if(!buf){
        log_error("failed to alloc memory  "); 
        goto _exit;
    }

    ret = sda_connect_to_cloud();
    if (0 != ret) {
        goto _exit;
    }

    memset(&connInfo, 0x0, sizeof(ConnectInfo_t));
    ret = rd_net_get_conn_info(&g_network, NULL, &connInfo);
    if (0 != ret) {
        log_error("get cloud connect info failed ");
        goto _exit;
    }
    _init_local_fd_array(socketfd_local, DEFAULT_SESSION_COUNT);
    log_info("connect to cloud success, socketfd: %d ", connInfo.sockfd);
    while (g_is_running) {
        tv.tv_sec = 0;
        tv.tv_usec = 500*1000L;
        _add_select_array(connInfo.sockfd, socketfd_local, DEFAULT_SESSION_COUNT, &rfds);

        ret = select(_get_max_socketfd(connInfo.sockfd, socketfd_local, DEFAULT_SESSION_COUNT), &rfds, NULL, NULL, &tv);

        memset(buf, 0, DEFAULT_MSG_BUFFER_LEN);
        if (ret == 0) {
            nopoll_conn_send_ping (g_network.handle);
            if(++timeout >= 30*60){
                log_info("there is no package received in 30 min, close current connection...");
                break;
            } 
        }else if(ret < 0){
            log_error("failed to select: %s , close current connection", strerror(errno));
            _send_error_resp(ERR_CONNECTION_CLOSE, "device network error.", NULL, NULL, buf);
            break;
        }

		//local service
		if(ret > 0)
		{
			int index = _get_active_socketfd_index(socketfd_local, DEFAULT_SESSION_COUNT, &rfds);
			if(index != -1){
				char *token = get_session_id_by_socketfd(socketfd_local + index);
				if(token == NULL){
					log_error("can not find session id for socketfd: %d, close it.", socketfd_local[index]);
					_del_socketfd_by_index(index, socketfd_local, DEFAULT_SESSION_COUNT);
					close(socketfd_local[index]);
					continue;
				}

				ret = recv(socketfd_local[index], buf, DEFAULT_MSG_BUFFER_LEN - DEFAULT_MSG_HDR_LEN, 0);
				timeout = 0; 
				log_debug("recv msg comes from sshd, socketfd: %d, index: %d, len:%d ", socketfd_local[index], index, ret);
				//dump_hex(buf, ret);
				if (ret > 0) {
					buf_hdr = sda_gen_msg_header(MSG_SERVICE_PROVIDER_RAW_PROTOCOL, ret, NULL, token);
					memmove(buf + strlen(buf_hdr), buf, ret);
					memmove(buf, buf_hdr, strlen(buf_hdr));
					tmp = rd_net_write(&g_network, NULL, buf, ret + strlen(buf_hdr), 0);
					log_debug("resend to cloud, len:%d", tmp);
				} else {
					log_error("local service exit:  %s ", strerror(errno));
					free_session(get_session_id_by_socketfd(socketfd_local + index));
					_del_socketfd_by_index(index, socketfd_local, DEFAULT_SESSION_COUNT);
					close(socketfd_local[index]);
					_send_error_resp(ERR_SERVICE_EXIT, "local service exit", NULL, token, buf);
				}
			}
		}
		
		//cloud service
        {
            ret = _read_msg_hdr(buf, DEFAULT_MSG_BUFFER_LEN);
			if(ret < 0)
				continue;
            
            timeout = 0;
            if(0 == get_msg_header(buf, &hdr)){
                log_error("read wrong format data, pending data len: %d", rd_net_read(&g_network, NULL, buf, DEFAULT_MSG_BUFFER_LEN, 0));
                _send_error_resp(ERR_PARAM_INVALID, "cloud msg format is invalid", NULL, NULL, buf);
                continue;
            }
            if(hdr.msg_type == MSG_SERVICE_CONSUMER_NEW_SESSION){
                rd_net_read(&g_network, NULL, buf + strlen(buf), hdr.payload_len + 4, 0);//FIXME, deal with service port
                if(0 == is_session_avalibe()){
                    log_error("session is too much...  ");
                    _send_error_resp(ERR_SESSION_LIMIT, "no available session to be allocated", hdr.msg_id, NULL, buf);
                    continue;
                }

                tmp_fd = sda_connect_to_local_service();
                if(tmp_fd == -1){
                    log_error("failed to connect to local service\n");
                    _send_error_resp(ERR_SERVICE_UNAVALIBE, "local service is not available", hdr.msg_id, NULL, buf);
                    continue;
                }
                tmp_index = _add_localfd(tmp_fd, socketfd_local, DEFAULT_SESSION_COUNT);
                if(tmp_index == DEFAULT_SESSION_COUNT){
                    _send_error_resp(ERR_SESSION_CREATE_FAILED, "socketfd insert error", hdr.msg_id, NULL, buf);
                    close(tmp_fd);
                    log_error("failed to add socketfd");
                    continue;
                }

                session_id = alloc_new_session(socketfd_local + tmp_index);
                if(session_id == NULL){
                    close(tmp_fd);
                    log_error("failed to alloc new session");
                    _send_error_resp(ERR_SESSION_CREATE_FAILED, "memory error", hdr.msg_id, NULL, buf);
                    continue;
                }
                char data[128] = {0};
                snprintf(data, sizeof(data) - 1, "{\"session_id\": \"%s\"}", session_id);
                tmp_buf  = sda_gen_payload_msg(0, data, "new session response");

                buf_hdr = sda_gen_msg_header(MSG_RESP_OK , strlen(tmp_buf), hdr.msg_id, session_id);
                memset(buf, 0, DEFAULT_MSG_BUFFER_LEN);
                snprintf(buf, DEFAULT_MSG_BUFFER_LEN, "%s%s", buf_hdr, tmp_buf); 
                rd_net_write(&g_network, NULL, buf, strlen(buf), 0);
                log_debug("send new session respose to cloud, socketfd: %d, index: %d, %s ", socketfd_local[tmp_index], tmp_index, buf);
                free(session_id);
            } else if(hdr.msg_type == MSG_SERVICE_CONSUMER_RELEASE_SESSION){//TODO
                rd_net_read(&g_network, NULL, buf + strlen(buf), hdr.payload_len, 0);//FIXME, deal with service port
                int _socketfd = get_socketfd_by_session_id(hdr.token);
                if(_socketfd < 0){
                    _send_error_resp(ERR_SESSION_NONEXISTENT, "cannot find local connection by session id", hdr.msg_id, hdr.token, buf);
                    log_error("session is not exist");
                    continue; 
                }
                _del_socketfd_by_id(_socketfd, socketfd_local, DEFAULT_SESSION_COUNT);
                close(_socketfd);

                free_session(hdr.token);

                char data[128] = {0};
                snprintf(data, sizeof(data) - 1, "{\"session_id\": \"%s\"}", hdr.token);
                tmp_buf  = sda_gen_payload_msg(0, data, "release session response");

                buf_hdr = sda_gen_msg_header(MSG_RESP_OK , strlen(tmp_buf), hdr.msg_id, hdr.token);
                memset(buf, 0, DEFAULT_MSG_BUFFER_LEN);
                snprintf(buf, DEFAULT_MSG_BUFFER_LEN, "%s%s", buf_hdr, tmp_buf); 
                rd_net_write(&g_network, NULL, buf, strlen(buf), 0);
                log_debug("send release session respose to cloud, socketfd: %d,  %s ", _socketfd, buf); 
            } else if(hdr.msg_type == MSG_SERVICE_CONSUMER_RAW_PROTOCOL){
                int _socketfd = get_socketfd_by_session_id(hdr.token);
                if(_socketfd < 0){
                    _send_error_resp(ERR_SESSION_NONEXISTENT, "cannot find local connection by session id", hdr.msg_id, hdr.token, buf);
                    log_error("session is not exist"); 
                    continue; 
                }

                int n = 0, nwritten = 0, nread = 0, m = 0;
                log_debug("send payload %d start, session id: %s\n", hdr.payload_len, hdr.token);
                memset(buf, 0, DEFAULT_MSG_BUFFER_LEN);
                while(nread < hdr.payload_len){
                    memset(buf, 0, DEFAULT_MSG_BUFFER_LEN);
                    n = nopoll_conn_read(g_network.handle, buf, hdr.payload_len - nread > DEFAULT_MSG_BUFFER_LEN ? DEFAULT_MSG_BUFFER_LEN : hdr.payload_len - nread, nopoll_true, 1000);
                    if(n == -1)
                        continue;
                    nread += n;
                    //dump_hex(buf, n);
                    nwritten = 0;
                    while(nwritten < n){
                        m = send(_socketfd, buf + nwritten, n - nwritten, 0);
                        nwritten += m;
                        if(m == -1){
                            log_info("resend to local service FAILED, len: %d, payload: %d ", n, payload);
                            free_session(hdr.token);
                            _del_socketfd_by_id(_socketfd, socketfd_local, DEFAULT_SESSION_COUNT);
                            close(_socketfd);
                            _send_error_resp(ERR_SERVICE_EXIT, "local service exit", hdr.msg_id, hdr.token, buf);
                            break;
                        }
                    }
                }
                log_debug("send payload %d done\n", hdr.payload_len);
            } else if(hdr.msg_type == MSG_RESP_OK){
                ret = rd_net_read(&g_network, NULL, buf + strlen(buf), hdr.payload_len, 0);//FIXME, deal with service port
                log_debug("login response: %d %s\n", ret, buf);
                if((0 == strcmp(hdr.msg_id, DEFAULT_MSG_ID_HDSK)) && 
                        (0 == is_login_ok(buf + hdr.hdr_len, hdr.payload_len))){
                    log_error("login to cloud failed ");
                    goto _exit;
                }
            }
        } 
    }

_exit:
    deinit_session_system();
    rd_net_destroy(&g_network);
    _close_all_conn(socketfd_local, DEFAULT_SESSION_COUNT);

    if(buf != NULL)
        free(buf);

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa28, %rsp            # imm = 0xA28
xorps %xmm0, %xmm0
movaps %xmm0, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movl $0x2000, %edi           # imm = 0x2000
callq 0x2150
movq %rax, %rbx
testq %rax, %rax
je 0x5560
leaq 0x220(%rsp), %rdi
movl $0x800, %edx            # imm = 0x800
xorl %esi, %esi
callq 0x2090
movq 0x6eaa(%rip), %rax      # 0xc3a0
xorl %edx, %edx
cmpb $0x1, 0x3d(%rax)
sete %dl
movq (%rax), %rcx
movq 0x8(%rax), %r8
movzbl 0x3c(%rax), %esi
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
movl %esi, (%rsp)
leaq 0x6e35(%rip), %r14      # 0xc358
movq %r14, %rdi
xorl %esi, %esi
xorl %r9d, %r9d
callq 0x29b9
movq %r14, %rdi
callq 0x27d8
testl %eax, %eax
je 0x55e2
movl %eax, %r14d
leaq 0x30b6(%rip), %rsi      # 0x8600
leaq 0x30b5(%rip), %rdx      # 0x8606
leaq 0x35de(%rip), %r8       # 0x8b36
pushq $0x3
popq %rdi
pushq $0x33
popq %rcx
jmp 0x5580
leaq 0x3099(%rip), %rsi      # 0x8600
leaq 0x3098(%rip), %rdx      # 0x8606
leaq 0x30ac(%rip), %r8       # 0x8621
xorl %r14d, %r14d
pushq $0x3
popq %rdi
movl $0x17e, %ecx            # imm = 0x17E
xorl %eax, %eax
callq 0x403f
movq %r14, %r15
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x7806
leaq 0x6dbd(%rip), %rdi      # 0xc358
callq 0x27a0
cmpq $0x20, %r14
je 0x55c5
movl 0xf0(%rsp,%r14,4), %edi
testl %edi, %edi
jle 0x55c0
callq 0x20c0
orl $-0x1, 0xf0(%rsp,%r14,4)
incq %r14
jmp 0x55a0
movq %rbx, %rdi
callq 0x2250
movl %r15d, %eax
addq $0xa28, %rsp            # imm = 0xA28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6db7(%rip), %rax      # 0xc3a0
movq 0x18(%rax), %rdi
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
callq 0x6bf2
movq %rax, %r14
movq %rax, %rdi
callq 0x2270
leaq 0x3464(%rip), %rdx      # 0x8a70
pushq $0x1
popq %rbp
movl %ebp, %edi
movl %eax, %esi
xorl %ecx, %ecx
callq 0x6ae4
movq %rax, %r15
leaq 0x3035(%rip), %r12      # 0x8659
leaq 0x2fdb(%rip), %r13      # 0x8606
leaq 0x3513(%rip), %r8       # 0x8b45
pushq $0x3a
popq %rcx
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
movq %rax, %r9
xorl %eax, %eax
callq 0x403f
leaq 0x350b(%rip), %r8       # 0x8b59
pushq $0x3b
popq %rcx
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %r9
xorl %eax, %eax
callq 0x403f
leaq 0x32a4(%rip), %rdx      # 0x890e
leaq 0x220(%rsp), %r12
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdi
movq %r15, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x20b0
movq %r12, %rdi
callq 0x2270
leaq 0x6cc2(%rip), %rdi      # 0xc358
xorl %esi, %esi
movq %r12, %rdx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x2756
testl %eax, %eax
jle 0x571c
leaq 0x2fa9(%rip), %rsi      # 0x8659
leaq 0x2f4f(%rip), %rdx      # 0x8606
leaq 0x34cb(%rip), %r8       # 0x8b89
pushq $0x1
popq %rdi
pushq $0x44
popq %rcx
movl %eax, %r9d
xorl %eax, %eax
callq 0x403f
xorps %xmm0, %xmm0
leaq 0x170(%rsp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x10(%rdx)
andq $0x0, 0x20(%rdx)
leaq 0x6c6c(%rip), %rdi      # 0xc358
xorl %esi, %esi
callq 0x293c
testl %eax, %eax
je 0x5747
movl %eax, %r14d
leaq 0x2eff(%rip), %rsi      # 0x8600
leaq 0x2efe(%rip), %rdx      # 0x8606
leaq 0x2f2b(%rip), %r8       # 0x863a
pushq $0x3
popq %rdi
movl $0x18a, %ecx            # imm = 0x18A
jmp 0x5580
leaq 0x2edd(%rip), %rsi      # 0x8600
leaq 0x2edc(%rip), %rdx      # 0x8606
leaq 0x343c(%rip), %r8       # 0x8b6d
pushq $0x3
popq %rdi
pushq $0x40
popq %rcx
xorl %eax, %eax
callq 0x403f
pushq $-0x1
popq %r14
jmp 0x5587
xorl %eax, %eax
cmpq $0x20, %rax
je 0x575c
orl $-0x1, 0xf0(%rsp,%rax,4)
incq %rax
jmp 0x5749
movl 0x170(%rsp), %r9d
leaq 0x2eee(%rip), %rsi      # 0x8659
leaq 0x2e94(%rip), %rbp      # 0x8606
leaq 0x2ee6(%rip), %r8       # 0x865f
pushq $0x1
popq %rdi
movq %rbp, %rdx
movl $0x18e, %ecx            # imm = 0x18E
xorl %eax, %eax
callq 0x403f
pushq $0x1
popq %r12
leaq 0xc0(%rsp), %r13
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
cmpb $0x0, 0x6bee(%rip)      # 0xc398
jne 0x5587
andq $0x0, 0xd0(%rsp)
movq $0x7a120, 0xd8(%rsp) # imm = 0x7A120
movl 0x170(%rsp), %edi
xorl %eax, %eax
cmpq $0x10, %rax
je 0x57e2
andq $0x0, 0x1a0(%rsp,%rax,8)
incq %rax
jmp 0x57ce
testl %edi, %edi
js 0x57fb
movq %r12, %rax
movl %edi, %ecx
shlq %cl, %rax
movl %edi, %ecx
shrl $0x6, %ecx
orq %rax, 0x1a0(%rsp,%rcx,8)
xorl %eax, %eax
cmpq $0x20, %rax
je 0x5824
movl 0xf0(%rsp,%rax,4), %ecx
testl %ecx, %ecx
js 0x581f
movq %r12, %rdx
shlq %cl, %rdx
shrl $0x6, %ecx
orq %rdx, 0x1a0(%rsp,%rcx,8)
incq %rax
jmp 0x57fd
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0x20, %rcx
je 0x583f
movl 0xf0(%rsp,%rcx,4), %edx
cmpl %eax, %edx
cmovgl %edx, %eax
incq %rcx
jmp 0x5828
cmpl %edi, %eax
cmovgl %eax, %edi
incl %edi
leaq 0x1a0(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0xd0(%rsp), %r8
callq 0x20f0
movl %eax, %r14d
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2090
testl %r14d, %r14d
je 0x58c3
js 0x668a
movq %r14, %r15
leaq 0xf0(%rsp), %r14
xorl %ebp, %ebp
pushq $0x40
popq %rcx
cmpq $0x20, %rbp
je 0x58ba
movl (%r14), %eax
cmpl $-0x1, %eax
je 0x58b1
cltd
idivl %ecx
cltq
movq 0x1a0(%rsp,%rax,8), %rax
btq %rdx, %rax
jb 0x5d68
incq %rbp
addq $0x4, %r14
jmp 0x588c
leaq 0x2d45(%rip), %rbp      # 0x8606
jmp 0x58e6
movq 0x6ac6(%rip), %rdi      # 0xc390
callq 0x21b0
movq 0x18(%rsp), %rax
cmpl $0x707, %eax            # imm = 0x707
jge 0x66da
incl %eax
movq %rax, 0x18(%rsp)
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2090
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x2110
xorl %eax, %eax
movq %rax, %r15
leaq (%rbx,%rax), %r14
movq 0x6a7f(%rip), %rdi      # 0xc390
movl $0x1f4, %r8d            # imm = 0x1F4
movq %r14, %rsi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
callq 0x2230
cmpb $0x7b, (%rbx)
jne 0x598d
cmpl $-0x1, %eax
jne 0x5978
movq %r13, %rdi
xorl %esi, %esi
callq 0x2110
movq %r13, %rdi
leaq 0x30(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
callq 0x53d8
imulq $0x3e8, 0xe0(%rsp), %rcx # imm = 0x3E8
movq 0xe8(%rsp), %rax
cqto
movl $0x3e8, %esi            # imm = 0x3E8
idivq %rsi
addq %rcx, %rax
cmpq $0x3e9, %rax            # imm = 0x3E9
jl 0x590a
jmp 0x5998
cmpb $0x7d, (%r14)
je 0x5f19
movq %r15, %rax
incq %rax
jmp 0x5903
pushq $-0x1
popq %rax
movl %eax, %r14d
jmp 0x57a3
movq %r15, %r14
movl %r14d, (%rsp)
pushq $0x3
popq %rdi
leaq 0x2c57(%rip), %rsi      # 0x8600
movq %rbp, %rdx
movl $0x152, %ecx            # imm = 0x152
leaq 0x31ed(%rip), %r8       # 0x8ba5
movq %rax, %r9
xorl %eax, %eax
callq 0x403f
testl %r14d, %r14d
js 0x57a3
movq %r14, 0x20(%rsp)
movl $0xa0, %edx
leaq 0x220(%rsp), %rdi
xorl %esi, %esi
callq 0x2090
movq %rbx, %rdi
pushq $0x7b
popq %rsi
callq 0x23c0
testq %rax, %rax
je 0x5cc6
movq %rax, %rbp
movq %rbx, %rdi
pushq $0x7d
popq %rsi
callq 0x23c0
testq %rax, %rax
je 0x5cc6
movq %rax, %r14
subq %rbx, %rbp
subl %ebx, %r14d
movslq %ebp, %rax
leaq (%rbx,%rax), %rbp
subl %eax, %r14d
leal 0x1(%r14), %r15d
movq %rbp, %rdi
movl %r15d, %esi
leaq 0x3191(%rip), %rdx      # 0x8bc4
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x371a
testq %rax, %rax
je 0x5cc6
movq %rax, %rdi
callq 0x2380
movl %eax, 0x220(%rsp)
movq %rbp, %rdi
movl %r15d, %esi
leaq 0x316b(%rip), %rdx      # 0x8bcd
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x371a
testq %rax, %rax
je 0x5a80
movq %rax, %rdi
callq 0x2380
movl %eax, 0x224(%rsp)
movq %rbp, %rdi
movl %r15d, %esi
leaq 0x314d(%rip), %rdx      # 0x8bda
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x371a
testq %rax, %rax
je 0x5cc6
movq %rax, %rdi
callq 0x2380
movl %eax, 0x228(%rsp)
andl $0x0, 0x30(%rsp)
movq %rbp, %rdi
movl %r15d, %esi
leaq 0x3125(%rip), %rdx      # 0x8be6
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
callq 0x371a
testq %rax, %rax
je 0x5af3
movl 0x30(%rsp), %ecx
cmpl $0x3f, %ecx
pushq $0x3f
popq %rdx
cmovgel %edx, %ecx
movslq %ecx, %rdx
leaq 0x22c(%rsp), %rdi
movq %rax, %rsi
callq 0x24c0
movq %rbp, %rdi
movl %r15d, 0x18(%rsp)
movl %r15d, %esi
leaq 0x30e8(%rip), %rdx      # 0x8bed
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x371a
testq %rax, %rax
leaq 0x683f(%rip), %r15      # 0xc358
je 0x5b2b
movq %rax, %rdi
callq 0x2300
movq %rax, 0x270(%rsp)
andl $0x0, 0x30(%rsp)
movq %rbp, %rdi
movl 0x18(%rsp), %esi
leaq 0x30b9(%rip), %rdx      # 0x8bf7
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
callq 0x371a
testq %rax, %rax
leaq 0x2ab1(%rip), %rbp      # 0x8606
je 0x5b77
movl 0x30(%rsp), %ecx
cmpl $0x3f, %ecx
pushq $0x3f
popq %rdx
cmovgel %edx, %ecx
movslq %ecx, %rdx
leaq 0x278(%rsp), %rdi
movq %rax, %rsi
callq 0x24c0
addl $0x5, %r14d
movl %r14d, 0x2b8(%rsp)
pushq $0x1
popq %rdi
leaq 0x2acc(%rip), %rsi      # 0x8659
movq %rbp, %rdx
movl $0x9f, %ecx
leaq 0x3061(%rip), %r8       # 0x8bfd
movq %rbx, %r9
xorl %eax, %eax
callq 0x403f
movl 0x220(%rsp), %eax
testl %eax, %eax
je 0x61e6
movq $0x0, 0x18(%rsp)
cmpl $0x16, %eax
leaq 0x278(%rsp), %r14
je 0x5f3f
cmpl $0x5, %eax
je 0x6088
cmpl $0x4, %eax
movq 0x20(%rsp), %r14
jne 0x57a3
movq %rbx, %rdi
callq 0x2270
leaq (%rax,%rbx), %rdx
movl 0x228(%rsp), %ecx
addl $0x4, %ecx
leaq 0x6755(%rip), %rdi      # 0xc358
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x270c
xorl %eax, %eax
callq 0x782c
testl %eax, %eax
je 0x633a
andq $0x0, 0x38(%rsp)
movw $0x2, 0x30(%rsp)
movq 0x6770(%rip), %rax      # 0xc3a0
movzwl 0x38(%rax), %ecx
rolw $0x8, %cx
movw %cx, 0x32(%rsp)
movq 0x30(%rax), %rdi
callq 0x25f0
movl %eax, 0x34(%rsp)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x23d0
cmpl $-0x1, %eax
je 0x63d8
movl %eax, %r14d
movl %eax, %edi
leaq 0x30(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x23a0
cmpl $-0x1, %eax
je 0x640e
pushq $0x1
popq %rdi
leaq 0x29d4(%rip), %rsi      # 0x8659
movq %rbp, %rdx
pushq $0x61
popq %rcx
leaq 0x2fcc(%rip), %r8       # 0x8c5e
movl %r14d, %r15d
movl %r14d, %r9d
xorl %eax, %eax
callq 0x403f
leaq 0xf0(%rsp), %r14
xorl %eax, %eax
cmpq $0x20, %rax
je 0x648e
cmpl $-0x1, (%r14)
je 0x64db
incq %rax
addq $0x4, %r14
jmp 0x5ca9
movq $0x0, 0x18(%rsp)
movq %r12, %r15
pushq $0x3
popq %r12
movl %r12d, %edi
leaq 0x2920(%rip), %r14      # 0x8600
movq %r14, %rsi
leaq 0x291c(%rip), %rbp      # 0x8606
movq %rbp, %rdx
movl $0xa2, %ecx
leaq 0x2f14(%rip), %r8       # 0x8c0d
movq %rbx, %r9
xorl %eax, %eax
callq 0x403f
leaq 0x664e(%rip), %rdi      # 0xc358
xorl %esi, %esi
movq %rbx, %rdx
movl $0x2000, %ecx           # imm = 0x2000
xorl %r8d, %r8d
callq 0x270c
movl %r12d, %edi
movq %r15, %r12
leaq 0xc0(%rsp), %r13
movq %r14, %rsi
movq %rbp, %rdx
movl $0x1cc, %ecx            # imm = 0x1CC
leaq 0x2a88(%rip), %r8       # 0x87c4
movl %eax, %r9d
xorl %eax, %eax
callq 0x403f
movl $0x18ce1, %edi          # imm = 0x18CE1
leaq 0x2a9f(%rip), %rsi      # 0x87f1
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x6751
movq 0x20(%rsp), %r14
jmp 0x57a3
movq %r14, %rdi
callq 0x7b1e
testq %rax, %rax
je 0x5e67
movq %rax, 0x20(%rsp)
movl (%r14), %edi
movl $0x1c00, %edx           # imm = 0x1C00
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x2170
leaq 0x226d(%rip), %rsi      # 0x8004
movq %rax, %r15
movl (%r14), %r9d
movl %r15d, 0x8(%rsp)
movl %ebp, (%rsp)
xorl %edi, %edi
leaq 0x2858(%rip), %rbp      # 0x8606
movq %rbp, %rdx
movl $0x1b2, %ecx            # imm = 0x1B2
leaq 0x2988(%rip), %r8       # 0x8745
xorl %eax, %eax
callq 0x403f
testl %r15d, %r15d
jle 0x5ea5
movq $0x0, 0x18(%rsp)
pushq $0x15
popq %rdi
movl %r15d, %esi
xorl %edx, %edx
movq 0x20(%rsp), %rcx
callq 0x6ae4
movq %rax, %r14
movq %rax, %rdi
callq 0x2270
leaq (%rax,%rbx), %rdi
movl %r15d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rbx, %rsi
callq 0x23b0
movq %r14, %rdi
callq 0x2270
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x23b0
movq %r14, %rdi
callq 0x2270
addl %eax, %r15d
leaq 0x6528(%rip), %rdi      # 0xc358
xorl %esi, %esi
movq %rbx, %rdx
movl %r15d, %ecx
xorl %r8d, %r8d
callq 0x2756
xorl %edi, %edi
leaq 0x21bb(%rip), %rsi      # 0x8004
movq %rbp, %rdx
movl $0x1b9, %ecx            # imm = 0x1B9
leaq 0x2928(%rip), %r8       # 0x8780
movl %eax, %r9d
xorl %eax, %eax
callq 0x403f
jmp 0x58e6
movl (%r14), %r9d
pushq $0x3
popq %rdi
leaq 0x278c(%rip), %rsi      # 0x8600
leaq 0x278b(%rip), %rbp      # 0x8606
movq %rbp, %rdx
movl $0x1aa, %ecx            # imm = 0x1AA
leaq 0x2887(%rip), %r8       # 0x8711
xorl %eax, %eax
callq 0x403f
orl $-0x1, (%r14)
pushq $-0x1
popq %rdi
callq 0x20c0
movq %r15, %r14
jmp 0x57a3
callq 0x2400
movl (%rax), %edi
callq 0x2330
movq $0x0, 0x18(%rsp)
pushq $0x3
popq %rdi
leaq 0x273c(%rip), %rsi      # 0x8600
movq %rbp, %rdx
movl $0x1bb, %ecx            # imm = 0x1BB
leaq 0x28c5(%rip), %r8       # 0x8798
movq %rax, %r9
xorl %eax, %eax
callq 0x403f
movq %r14, %rdi
callq 0x7b1e
movq %rax, %rdi
callq 0x7a99
orl $-0x1, (%r14)
pushq $-0x1
popq %rdi
callq 0x20c0
movl $0x18ce5, %edi          # imm = 0x18CE5
leaq 0x28ac(%rip), %rsi      # 0x87b1
xorl %edx, %edx
movq 0x20(%rsp), %rcx
movq %rbx, %r8
callq 0x6751
jmp 0x58e6
movq 0x6470(%rip), %rdi      # 0xc390
incq %r14
movl $0x3e8, %r8d            # imm = 0x3E8
movq %r14, %rsi
pushq $0x4
popq %rdx
pushq $0x1
popq %rcx
callq 0x2230
movq %r15, %r14
jmp 0x59c2
movq %r14, %rdi
callq 0x7bd1
testl %eax, %eax
js 0x62fb
movl 0x228(%rsp), %r9d
movq %r14, (%rsp)
movl $0x0, 0x18(%rsp)
xorl %edi, %edi
leaq 0x2098(%rip), %rsi      # 0x8004
movl %eax, %r15d
movq %rbp, %rdx
movl $0x216, %ecx            # imm = 0x216
leaq 0x2a68(%rip), %r8       # 0x89e6
xorl %eax, %eax
callq 0x403f
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2090
movq 0x20(%rsp), %r14
movl 0x228(%rsp), %r9d
movl %r9d, %ebp
subl 0x18(%rsp), %ebp
jbe 0x62c7
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2090
movq 0x63cc(%rip), %rdi      # 0xc390
movl $0x2000, %eax           # imm = 0x2000
cmpl %eax, %ebp
cmovael %eax, %ebp
movl $0x3e8, %r8d            # imm = 0x3E8
movq %rbx, %rsi
movl %ebp, %edx
pushq $0x1
popq %rcx
callq 0x2230
cmpl $-0x1, %eax
je 0x5f99
movl %eax, %ebp
addl %eax, 0x18(%rsp)
xorl %r14d, %r14d
movl %ebp, %eax
subl %r14d, %eax
jle 0x5f94
movslq %r14d, %rsi
addq %rbx, %rsi
movslq %eax, %rdx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0x2440
addl %eax, %r14d
cmpl $-0x1, %eax
jne 0x5fef
andl $0x0, (%rsp)
pushq $0x1
popq %rdi
leaq 0x263a(%rip), %rsi      # 0x8659
leaq 0x25e0(%rip), %rdx      # 0x8606
movl $0x224, %ecx            # imm = 0x224
leaq 0x29db(%rip), %r8       # 0x8a0d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x403f
leaq 0x278(%rsp), %r14
movq %r14, %rdi
callq 0x7a99
movl %r15d, %edi
leaq 0xf0(%rsp), %rsi
callq 0x67dd
movl %r15d, %edi
callq 0x20c0
movl $0x18ce5, %edi          # imm = 0x18CE5
leaq 0x2741(%rip), %rsi      # 0x87b1
leaq 0x22c(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
callq 0x6751
jmp 0x5f94
movq %rbx, %rdi
callq 0x2270
leaq (%rax,%rbx), %rdx
movl 0x228(%rsp), %ecx
movq %r15, %rdi
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x270c
movq %r14, %rdi
callq 0x7bd1
testl %eax, %eax
js 0x6386
movl %eax, %ebp
movl %eax, 0x28(%rsp)
movl %eax, %edi
leaq 0xf0(%rsp), %rsi
callq 0x67dd
movl %ebp, %edi
callq 0x20c0
movq %r14, %rdi
callq 0x7a99
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
pushq $0x7f
popq %rsi
leaq 0x27b9(%rip), %rdx      # 0x88e4
movq %r14, %rcx
xorl %eax, %eax
callq 0x20b0
xorl %edi, %edi
movq %rbp, %rsi
leaq 0x2852(%rip), %rdx      # 0x8993
callq 0x6b8c
movq %rax, %rbp
movq %rax, %rdi
callq 0x2270
xorl %edi, %edi
movl %eax, %esi
leaq 0x22c(%rsp), %rdx
movq %r14, %rcx
callq 0x6ae4
movq %rax, %r14
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2090
movl $0x2000, %esi           # imm = 0x2000
movq %rbx, %rdi
leaq 0x2788(%rip), %rdx      # 0x890e
movq %r14, %rcx
movq %rbp, %r8
leaq 0x2473(%rip), %rbp      # 0x8606
xorl %eax, %eax
callq 0x20b0
movq %rbx, %rdi
callq 0x2270
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x2756
movq %rbx, (%rsp)
xorl %edi, %edi
leaq 0x1e43(%rip), %rsi      # 0x8004
movq %rbp, %rdx
movl $0x20c, %ecx            # imm = 0x20C
leaq 0x27dc(%rip), %r8       # 0x89ac
movl 0x28(%rsp), %r9d
xorl %eax, %eax
callq 0x403f
movq 0x20(%rsp), %r14
jmp 0x57a3
movq %rbx, %rdi
callq 0x2270
leaq (%rax,%rbx), %rdx
movl 0x228(%rsp), %ecx
movq $0x0, 0x18(%rsp)
movq %r15, %rdi
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x270c
movl %eax, %r14d
movq %rbx, (%rsp)
xorl %edi, %edi
leaq 0x1de5(%rip), %rsi      # 0x8004
movq %rbp, %rdx
movl $0x230, %ecx            # imm = 0x230
leaq 0x282b(%rip), %r8       # 0x8a59
movl %eax, %r9d
xorl %eax, %eax
callq 0x403f
leaq 0x22c(%rsp), %rdi
leaq 0x2829(%rip), %rsi      # 0x8a70
pushq $0x15
popq %rdx
callq 0x2350
testl %eax, %eax
jne 0x57a3
movq %r14, %r15
movl 0x228(%rsp), %esi
movl 0x2b8(%rsp), %r14d
andl $0x0, 0x30(%rsp)
addq %rbx, %r14
andl $0x0, 0xc0(%rsp)
movq %r14, %rdi
leaq 0x2a24(%rip), %rdx      # 0x8ca7
leaq 0x30(%rsp), %rcx
movq %r13, %r8
callq 0x371a
testq %rax, %rax
je 0x66ff
cmpl $0x0, 0x30(%rsp)
jle 0x66ff
cmpl $0x3, 0xc0(%rsp)
jne 0x66ff
movq %rax, %rdi
callq 0x2380
testl %eax, %eax
je 0x5e9d
jmp 0x66ff
movq $0x0, 0x18(%rsp)
xorl %edi, %edi
leaq 0x1d2b(%rip), %rsi      # 0x8004
leaq 0x2326(%rip), %rbp      # 0x8606
movq %rbp, %rdx
movl $0x22d, %ecx            # imm = 0x22D
leaq 0x2754(%rip), %r8       # 0x8a43
xorl %eax, %eax
callq 0x403f
jmp 0x57a3
movl $0x18ce3, %edi          # imm = 0x18CE3
leaq 0x264c(%rip), %rsi      # 0x8953
leaq 0x22c(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
callq 0x6751
movq $0x0, 0x18(%rsp)
pushq $0x3
popq %rdi
leaq 0x22d3(%rip), %rsi      # 0x8600
movq %rbp, %rdx
movl $0x211, %ecx            # imm = 0x211
jmp 0x63c0
movq $0x0, 0x18(%rsp)
pushq $0x3
popq %rdi
leaq 0x22b3(%rip), %rsi      # 0x8600
movq %rbp, %rdx
movl $0x1d3, %ecx            # imm = 0x1D3
leaq 0x24b1(%rip), %r8       # 0x880d
xorl %eax, %eax
callq 0x403f
movl $0x18ce2, %edi          # imm = 0x18CE2
leaq 0x24b7(%rip), %rsi      # 0x8826
leaq 0x22c(%rsp), %rdx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x6751
jmp 0x57a3
movl $0x18ce3, %edi          # imm = 0x18CE3
leaq 0x25c1(%rip), %rsi      # 0x8953
leaq 0x22c(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
callq 0x6751
movq $0x0, 0x18(%rsp)
pushq $0x3
popq %rdi
leaq 0x2248(%rip), %rsi      # 0x8600
movq %rbp, %rdx
movl $0x1fc, %ecx            # imm = 0x1FC
leaq 0x25b7(%rip), %r8       # 0x897e
xorl %eax, %eax
callq 0x403f
movq 0x20(%rsp), %r14
jmp 0x57a3
callq 0x2400
movl (%rax), %edi
callq 0x2330
pushq $0x3
popq %r14
movl %r14d, %edi
leaq 0x220e(%rip), %r15      # 0x8600
movq %r15, %rsi
movq %rbp, %rdx
pushq $0x57
popq %rcx
leaq 0x282b(%rip), %r8       # 0x8c2d
movq %rax, %r9
xorl %eax, %eax
callq 0x403f
jmp 0x6450
callq 0x2400
movl (%rax), %edi
callq 0x2330
movl %r14d, 0x18(%rsp)
pushq $0x3
popq %r14
movl %r14d, %edi
leaq 0x21d3(%rip), %r15      # 0x8600
movq %r15, %rsi
movq %rbp, %rdx
pushq $0x5d
popq %rcx
leaq 0x280a(%rip), %r8       # 0x8c47
movq %rax, %r9
xorl %eax, %eax
callq 0x403f
movl 0x18(%rsp), %edi
callq 0x20c0
movq $0x0, 0x18(%rsp)
movl %r14d, %edi
movq %r15, %rsi
movq %rbp, %rdx
movl $0x1da, %ecx            # imm = 0x1DA
leaq 0x23dd(%rip), %r8       # 0x884b
xorl %eax, %eax
callq 0x403f
movl $0x18ce4, %edi          # imm = 0x18CE4
leaq 0x23ee(%rip), %rsi      # 0x886f
leaq 0x22c(%rsp), %rdx
jmp 0x5d54
movq $0x0, 0x18(%rsp)
movl $0x18ce4, %edi          # imm = 0x18CE4
leaq 0x23eb(%rip), %rsi      # 0x888e
leaq 0x22c(%rsp), %rdx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x6751
movl %r15d, %edi
callq 0x20c0
pushq $0x3
popq %rdi
leaq 0x2139(%rip), %rsi      # 0x8600
movq %rbp, %rdx
movl $0x1e2, %ecx            # imm = 0x1E2
leaq 0x23ce(%rip), %r8       # 0x88a4
jmp 0x63c7
movl %r15d, (%r14)
movl %r15d, (%rsp)
pushq $0x1
popq %rdi
leaq 0x216d(%rip), %rsi      # 0x8659
movq %rbp, %rdx
movl $0xc9, %ecx
leaq 0x2794(%rip), %r8       # 0x8c8f
movq %rax, %r9
movq %rax, 0x28(%rsp)
xorl %eax, %eax
callq 0x403f
movq %r14, %rdi
callq 0x7894
testq %rax, %rax
je 0x6648
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
pushq $0x7f
popq %rsi
leaq 0x237a(%rip), %rdx      # 0x88e4
movq %rax, %r15
movq %rax, %rcx
movq %rax, 0xb8(%rsp)
xorl %eax, %eax
callq 0x20b0
xorl %edi, %edi
movq %rbp, %rsi
leaq 0x236e(%rip), %rdx      # 0x88f9
callq 0x6b8c
movq %rax, %rbp
movq %rax, %rdi
callq 0x2270
xorl %edi, %edi
movl %eax, %esi
leaq 0x22c(%rsp), %rdx
movq %r15, %rcx
callq 0x6ae4
movq %rax, %r15
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2090
movl $0x2000, %esi           # imm = 0x2000
movq %rbx, %rdi
leaq 0x233e(%rip), %rdx      # 0x890e
movq %r15, %rcx
movq %rbp, %r8
leaq 0x2029(%rip), %rbp      # 0x8606
xorl %eax, %eax
callq 0x20b0
movq %rbx, %rdi
callq 0x2270
leaq 0x5d65(%rip), %rdi      # 0xc358
xorl %esi, %esi
movq %rbx, %rdx
movl %eax, %ecx
xorl %r8d, %r8d
callq 0x2756
movl (%r14), %r9d
movq %rbx, 0x8(%rsp)
movq 0x28(%rsp), %rax
movl %eax, (%rsp)
xorl %edi, %edi
leaq 0x19e9(%rip), %rsi      # 0x8004
movq %rbp, %rdx
movl $0x1f5, %ecx            # imm = 0x1F5
leaq 0x22e9(%rip), %r8       # 0x8913
xorl %eax, %eax
callq 0x403f
movq 0xb8(%rsp), %rdi
callq 0x2250
movq 0x20(%rsp), %r14
jmp 0x57a3
movl %r15d, %edi
callq 0x20c0
movq $0x0, 0x18(%rsp)
pushq $0x3
popq %rdi
leaq 0x1f9d(%rip), %rsi      # 0x8600
movq %rbp, %rdx
movl $0x1e9, %ecx            # imm = 0x1E9
leaq 0x2249(%rip), %r8       # 0x88bb
xorl %eax, %eax
callq 0x403f
movl $0x18ce4, %edi          # imm = 0x18CE4
leaq 0x2252(%rip), %rsi      # 0x88d7
jmp 0x6481
callq 0x2400
movl (%rax), %edi
callq 0x2330
leaq 0x1f63(%rip), %rsi      # 0x8600
leaq 0x1f62(%rip), %rdx      # 0x8606
leaq 0x2020(%rip), %r8       # 0x86cb
pushq $0x3
popq %rdi
movl $0x19e, %ecx            # imm = 0x19E
movq %rax, %r9
xorl %eax, %eax
callq 0x403f
leaq 0x2037(%rip), %rsi      # 0x86fb
movl $0x18ce6, %edi          # imm = 0x18CE6
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x6751
jmp 0x5587
leaq 0x1f78(%rip), %rsi      # 0x8659
leaq 0x1f1e(%rip), %rdx      # 0x8606
leaq 0x1f98(%rip), %r8       # 0x8687
xorl %r14d, %r14d
pushq $0x1
popq %rdi
movl $0x19a, %ecx            # imm = 0x19A
jmp 0x5580
leaq 0x1efa(%rip), %r12      # 0x8600
leaq 0x1ef9(%rip), %r13      # 0x8606
leaq 0x2598(%rip), %r8       # 0x8cac
pushq $0x3
popq %rbp
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
movl $0xad, %ecx
movq %r14, %r9
xorl %eax, %eax
callq 0x403f
leaq 0x2350(%rip), %r8       # 0x8a85
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
movl $0x233, %ecx            # imm = 0x233
xorl %eax, %eax
callq 0x403f
movq %r15, %r14
jmp 0x5587