// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/lib/https.c
// Function: netConnectHttpsThread
void *netConnectHttpsThread(void *threadParam)
/* use a thread to run socket back to user */
{
/* child */

struct netConnectHttpsParams *params = threadParam;

pthread_detach(params->thread);  // this thread will never join back with it's progenitor

int fd=0;
char *proxyUrl = getenv("https_proxy");
if (params->noProxy)
    proxyUrl = NULL;
char *connectHost;
int connectPort;

BIO *fbio=NULL;  // file descriptor bio
BIO *sbio=NULL;  // ssl bio
SSL_CTX *ctx;
SSL *ssl;

openSslInit();

ctx = SSL_CTX_new(SSLv23_client_method());

fd_set readfds;
fd_set writefds;
int err;
struct timeval tv;


/* TODO checking certificates 

char *certFile = NULL;
char *certPath = NULL;
if (certFile || certPath)
    {
    SSL_CTX_load_verify_locations(ctx,certFile,certPath);
#if (OPENSSL_VERSION_NUMBER < 0x0090600fL)
    SSL_CTX_set_verify_depth(ctx,1);
#endif
    }

// verify paths and mode.

*/



// Don't want any retries since we are non-blocking bio now
// This is available on newer versions of openssl
//SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

// Support for Http Proxy
struct netParsedUrl pxy;
if (proxyUrl)
    {
    netParseUrl(proxyUrl, &pxy);
    if (!sameString(pxy.protocol, "http"))
	{
	char s[256];	
	safef(s, sizeof s, "Unknown proxy protocol %s in %s. Should be http.", pxy.protocol, proxyUrl);
	xerr(s);
	goto cleanup;
	}
    connectHost = pxy.host;
    connectPort = atoi(pxy.port);
    }
else
    {
    connectHost = params->hostName;
    connectPort = params->port;
    }
fd = netConnect(connectHost,connectPort);
if (fd == -1)
    {
    xerr("netConnect() failed");
    goto cleanup;
    }

if (proxyUrl)
    {
    char *logProxy = getenv("log_proxy");
    if (sameOk(logProxy,"on"))
	verbose(1, "CONNECT %s:%d HTTP/1.0 via %s:%d\n", params->hostName, params->port, connectHost,connectPort);
    struct dyString *dy = newDyString(512);
    dyStringPrintf(dy, "CONNECT %s:%d HTTP/1.0\r\n", params->hostName, params->port);
    setAuthorization(pxy, "Proxy-Authorization", dy);
    dyStringAppend(dy, "\r\n");
    mustWriteFd(fd, dy->string, dy->stringSize);
    dyStringFree(&dy);
    // verify response
    char *newUrl = NULL;
    boolean success = netSkipHttpHeaderLinesWithRedirect(fd, proxyUrl, &newUrl);
    if (!success) 
	{
	xerr("proxy server response failed");
	goto cleanup;
	}
    if (newUrl) /* no redirects */
	{
	xerr("proxy server response should not be a redirect");
	goto cleanup;
	}
    }


fbio=BIO_new_socket(fd,BIO_NOCLOSE);  
// BIO_NOCLOSE because we handle closing fd ourselves.
if (fbio == NULL)
    {
    xerr("BIO_new_socket() failed");
    goto cleanup;
    }
sbio = BIO_new_ssl(ctx, 1);
if (sbio == NULL) 
    {
    xerr("BIO_new_ssl() failed");
    goto cleanup;
    }
sbio = BIO_push(sbio, fbio);
BIO_get_ssl(sbio, &ssl);
if(!ssl) 
    {
    xerr("Can't locate SSL pointer");
    goto cleanup;
    }


/* 
Server Name Indication (SNI)
Required to complete tls ssl negotiation for systems which house multiple domains. (SNI)
This is common when serving HTTPS requests with a wildcard certificate (*.domain.tld).
This line will allow the ssl connection to send the hostname at tls negotiation time.
It tells the remote server which hostname the client is connecting to.
The hostname must not be an IP address.
*/ 
if (!internetIsDottedQuad(params->hostName))
    SSL_set_tlsext_host_name(ssl,params->hostName);

BIO_set_nbio(sbio, 1);     /* non-blocking mode */

while (1) 
    {
    if (BIO_do_handshake(sbio) == 1) 
	{
	break;  /* Connected */
	}
    if (! BIO_should_retry(sbio)) 
	{
	xerr("BIO_do_handshake() failed");
	char s[256];	
	safef(s, sizeof s, "SSL error: %s", ERR_reason_error_string(ERR_get_error()));
	xerr(s);
	goto cleanup;
	}

    fd = BIO_get_fd(sbio, NULL);
    if (fd == -1) 
	{
	xerr("unable to get BIO descriptor");
	goto cleanup;
	}
    FD_ZERO(&readfds);
    FD_ZERO(&writefds);
    if (BIO_should_read(sbio)) 
	{
	FD_SET(fd, &readfds);
	}
    else if (BIO_should_write(sbio)) 
	{
	FD_SET(fd, &writefds);
	}
    else 
	{  /* BIO_should_io_special() */
	FD_SET(fd, &readfds);
	FD_SET(fd, &writefds);
	}
    tv.tv_sec = (long) (DEFAULTCONNECTTIMEOUTMSEC/1000);  // timeout default 10 seconds
    tv.tv_usec = (long) (((DEFAULTCONNECTTIMEOUTMSEC/1000)-tv.tv_sec)*1000000);

    err = select(fd + 1, &readfds, &writefds, NULL, &tv);
    if (err < 0) 
	{
	xerr("select() error");
	goto cleanup;
	}

    if (err == 0) 
	{
	char s[256];	
	safef(s, sizeof s, "connection timeout to %s", params->hostName);
	xerr(s);
	goto cleanup;
	}
    }


/* TODO checking certificates 

if (certFile || certPath)
    if (!check_cert(ssl, host))
	return -1;

*/

/* we need to wait on both the user's socket and the BIO SSL socket 
 * to see if we need to ferry data from one to the other */


char sbuf[32768];  // socket buffer sv[1] to user
char bbuf[32768];  // bio buffer
int srd = 0;
int swt = 0;
int brd = 0;
int bwt = 0;
while (1) 
    {

    // Do NOT move this outside the while loop. 
    /* Get underlying file descriptor, needed for select call */
    fd = BIO_get_fd(sbio, NULL);
    if (fd == -1) 
	{
	xerr("BIO doesn't seem to be initialized in https, unable to get descriptor.");
	goto cleanup;
	}


    FD_ZERO(&readfds);
    FD_ZERO(&writefds);

    if (brd == 0)
	FD_SET(fd, &readfds);
    if (swt < srd)
	FD_SET(fd, &writefds);
    if (srd == 0)
	FD_SET(params->sv[1], &readfds);

    tv.tv_sec = (long) (DEFAULTCONNECTTIMEOUTMSEC/1000);  // timeout default 10 seconds
    tv.tv_usec = (long) (((DEFAULTCONNECTTIMEOUTMSEC/1000)-tv.tv_sec)*1000000);

    err = select(max(fd,params->sv[1]) + 1, &readfds, &writefds, NULL, &tv);

    /* Evaluate select() return code */
    if (err < 0) 
	{
	xerr("error during select()");
	goto cleanup;
	}
    else if (err == 0) 
	{
	/* Timed out - just quit */
	xerr("https timeout expired");
	goto cleanup;
	}

    else 
	{
	if (FD_ISSET(params->sv[1], &readfds))
	    {
	    swt = 0;
	    srd = read(params->sv[1], sbuf, 32768);
	    if (srd == -1)
		{
		if (errno != 104) // udcCache often closes causing "Connection reset by peer"
		    xerrno("error reading user pipe for https socket");
		goto cleanup;
		}
	    if (srd == 0) 
		break;  // user closed socket, we are done
	    }

	if (FD_ISSET(fd, &writefds))
	    {
	    int swtx = BIO_write(sbio, sbuf+swt, srd-swt);
	    if (swtx <= 0)
		{
		if (!BIO_should_write(sbio))
		    {
		    ERR_print_errors_fp(stderr);
		    xerr("Error writing SSL connection");
		    goto cleanup;
		    }
		}
	    else
		{
		swt += swtx;
		if (swt >= srd)
		    {
		    swt = 0;
		    srd = 0;
		    }
		}
	    }

	if (FD_ISSET(fd, &readfds))
	    {
	    bwt = 0;
	    brd = BIO_read(sbio, bbuf, 32768);

	    if (brd <= 0)
		{
		if (BIO_should_read(sbio))
		    {
		    brd = 0;
		    continue;
		    }
		else
		    {
		    if (brd == 0) break;
		    ERR_print_errors_fp(stderr);
		    xerr("Error reading SSL connection");
		    goto cleanup;
		    }
		}
	    // write the https data received immediately back on socket to user, and it's ok if it blocks.
	    while(bwt < brd)
		{
		// NOTE: Intended as a thread-specific library-safe way not to ignore SIG_PIPE for the entire application.
		// temporarily block sigpipe on this thread
		sigset_t sigpipe_mask;
		sigemptyset(&sigpipe_mask);
		sigaddset(&sigpipe_mask, SIGPIPE);
		sigset_t saved_mask;
		if (pthread_sigmask(SIG_BLOCK, &sigpipe_mask, &saved_mask) == -1) {
		    perror("pthread_sigmask");
		    exit(1);
		}
		int bwtx = write(params->sv[1], bbuf+bwt, brd-bwt);
		int saveErrno = errno;
		if ((bwtx == -1) && (saveErrno == EPIPE))
		    { // if there was a EPIPE, accept and consume the SIGPIPE now.
		    int sigErr, sig;
		    if ((sigErr = sigwait(&sigpipe_mask, &sig)) != 0) 
			{
			errno = sigErr;
			perror("sigwait");
			exit(1);
			}
		    }
		// restore signal mask on this thread
		if (pthread_sigmask(SIG_SETMASK, &saved_mask, 0) == -1) 
		    {
		    perror("pthread_sigmask");
		    exit(1);
		    }
		errno = saveErrno;
		if (bwtx == -1)
		    {
		    if ((errno != 104)  // udcCache often closes causing "Connection reset by peer"
		     && (errno !=  32)) // udcCache often closes causing "Broken pipe"
			xerrno("error writing https data back to user pipe");
		    goto cleanup;
		    }
		bwt += bwtx;
		}
	    brd = 0;
	    bwt = 0;
	    }
	}
    }

cleanup:

BIO_free_all(sbio);  // will free entire chain of bios
close(fd);     // Needed because we use BIO_NOCLOSE above. Someday might want to re-use a connection.
close(params->sv[1]);  /* we are done with it */

return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x15690, %rsp          # imm = 0x15690
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6490
movl $0x0, -0x14(%rbp)
leaq 0x76432(%rip), %rdi     # 0xe17b3
callq 0x68d0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x6b39c
movq $0x0, -0x20(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
callq 0x6b2e0
callq 0x6350
movq %rax, %rdi
callq 0x6a70
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x6b454
movq -0x20(%rbp), %rdi
leaq -0x2998(%rbp), %rsi
callq 0x42160
leaq -0x2998(%rbp), %rdi
leaq 0x7095f(%rip), %rsi     # 0xdbd4a
callq 0x6a40
cmpl $0x0, %eax
je 0x6b42b
leaq -0x2aa0(%rbp), %rdi
leaq -0x2998(%rbp), %rcx
movq -0x20(%rbp), %r8
movl $0x100, %esi            # imm = 0x100
leaq 0x763ac(%rip), %rdx     # 0xe17bf
movb $0x0, %al
callq 0x31090
leaq -0x2aa0(%rbp), %rdi
callq 0x6c060
jmp 0x6c037
leaq -0x2998(%rbp), %rax
addq $0x1010, %rax           # imm = 0x1010
movq %rax, -0x28(%rbp)
leaq -0x2998(%rbp), %rdi
addq $0x1810, %rdi           # imm = 0x1810
callq 0x6bd0
movl %eax, -0x2c(%rbp)
jmp 0x6b46a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x41710
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x6b490
leaq 0x7636a(%rip), %rdi     # 0xe17f0
callq 0x6c060
jmp 0x6c037
cmpq $0x0, -0x20(%rbp)
je 0x6b5f0
leaq 0x709b4(%rip), %rdi     # 0xdbe56
callq 0x68d0
movq %rax, -0x2aa8(%rbp)
movq -0x2aa8(%rbp), %rdi
leaq 0x6c2fc(%rip), %rsi     # 0xd77b8
callq 0x2d310
cmpl $0x0, %eax
jne 0x6b4f0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0x28(%rbp), %r8
movl -0x2c(%rbp), %r9d
movl $0x1, %edi
leaq 0x7631b(%rip), %rsi     # 0xe1804
movb $0x0, %al
callq 0x60040
movl $0x200, %edi            # imm = 0x200
callq 0x35020
movq %rax, -0x2ab0(%rbp)
movq -0x2ab0(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x10(%rbp), %rax
movl 0x10(%rax), %ecx
leaq 0x76308(%rip), %rsi     # 0xe1826
movb $0x0, %al
callq 0x35540
movq -0x2ab0(%rbp), %rax
movq %rax, -0x12e30(%rbp)
movq %rsp, %rdi
movl $0x506, %ecx            # imm = 0x506
leaq -0x2998(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq -0x12e30(%rbp), %rsi
leaq 0x70943(%rip), %rdi     # 0xdbe96
callq 0x43350
movq -0x2ab0(%rbp), %rdi
leaq 0x7660e(%rip), %rsi     # 0xe1b74
callq 0x353b0
movl -0x14(%rbp), %edi
movq -0x2ab0(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x2ab0(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x2fe40
leaq -0x2ab0(%rbp), %rdi
callq 0x35080
movq $0x0, -0x2ab8(%rbp)
movl -0x14(%rbp), %edi
movq -0x20(%rbp), %rsi
leaq -0x2ab8(%rbp), %rdx
callq 0x443d0
movl %eax, -0x2abc(%rbp)
cmpl $0x0, -0x2abc(%rbp)
jne 0x6b5d3
leaq 0x76276(%rip), %rdi     # 0xe183f
callq 0x6c060
jmp 0x6c037
cmpq $0x0, -0x2ab8(%rbp)
je 0x6b5ee
leaq 0x76278(%rip), %rdi     # 0xe185c
callq 0x6c060
jmp 0x6c037
jmp 0x6b5f0
movl -0x14(%rbp), %edi
xorl %esi, %esi
callq 0x6890
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x6b616
leaq 0x7627f(%rip), %rdi     # 0xe188b
callq 0x6c060
jmp 0x6c037
movq -0x48(%rbp), %rdi
movl $0x1, %esi
callq 0x60c0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x6b640
leaq 0x7626d(%rip), %rdi     # 0xe18a3
callq 0x6c060
jmp 0x6c037
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x6730
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movl $0x6e, %esi
xorl %eax, %eax
movl %eax, %edx
leaq -0x50(%rbp), %rcx
callq 0x61a0
cmpq $0x0, -0x50(%rbp)
jne 0x6b67f
leaq 0x76243(%rip), %rdi     # 0xe18b8
callq 0x6c060
jmp 0x6c037
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x6c1e0
cmpl $0x0, %eax
jne 0x6b6ab
movq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movl $0x37, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x6280
movq -0x40(%rbp), %rdi
movl $0x66, %esi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x61a0
movq -0x40(%rbp), %rdi
movl $0x65, %esi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x61a0
cmpq $0x1, %rax
jne 0x6b6e2
jmp 0x6b9ca
movq -0x40(%rbp), %rdi
movl $0x8, %esi
callq 0x6ad0
cmpl $0x0, %eax
jne 0x6b74a
leaq 0x761d5(%rip), %rdi     # 0xe18d1
callq 0x6c060
leaq -0x2bc0(%rbp), %rax
movq %rax, -0x12e38(%rbp)
callq 0x69b0
movq %rax, %rdi
callq 0x6e20
movq -0x12e38(%rbp), %rdi
movq %rax, %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x761b9(%rip), %rdx     # 0xe18eb
movb $0x0, %al
callq 0x31090
leaq -0x2bc0(%rbp), %rdi
callq 0x6c060
jmp 0x6c037
movq -0x40(%rbp), %rdi
movl $0x69, %esi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x61a0
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x6b779
leaq 0x7618a(%rip), %rdi     # 0xe18f9
callq 0x6c060
jmp 0x6c037
jmp 0x6b77b
leaq -0xd0(%rbp), %rax
movq %rax, -0x2bd0(%rbp)
movl $0x0, -0x2bc4(%rbp)
movl -0x2bc4(%rbp), %eax
cmpq $0x10, %rax
jae 0x6b7c5
movq -0x2bd0(%rbp), %rax
movl -0x2bc4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x2bc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2bc4(%rbp)
jmp 0x6b793
jmp 0x6b7c7
jmp 0x6b7c9
leaq -0x150(%rbp), %rax
movq %rax, -0x2be0(%rbp)
movl $0x0, -0x2bd4(%rbp)
movl -0x2bd4(%rbp), %eax
cmpq $0x10, %rax
jae 0x6b813
movq -0x2be0(%rbp), %rax
movl -0x2bd4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x2bd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2bd4(%rbp)
jmp 0x6b7e1
jmp 0x6b815
movq -0x40(%rbp), %rdi
movl $0x1, %esi
callq 0x6ad0
cmpl $0x0, %eax
je 0x6b864
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x14(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xd0(%rbp,%rax,8), %rcx
movq %rcx, -0xd0(%rbp,%rax,8)
jmp 0x6b920
movq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x6ad0
cmpl $0x0, %eax
je 0x6b8b0
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x14(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x150(%rbp,%rax,8), %rcx
movq %rcx, -0x150(%rbp,%rax,8)
jmp 0x6b91e
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x14(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xd0(%rbp,%rax,8), %rcx
movq %rcx, -0xd0(%rbp,%rax,8)
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x14(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x150(%rbp,%rax,8), %rcx
movq %rcx, -0x150(%rbp,%rax,8)
jmp 0x6b920
movq $0xa, -0x168(%rbp)
movl $0xa, %eax
subq -0x168(%rbp), %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
movq %rax, -0x160(%rbp)
movl -0x14(%rbp), %edi
addl $0x1, %edi
leaq -0xd0(%rbp), %rsi
leaq -0x150(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x168(%rbp), %r8
callq 0x66b0
movl %eax, -0x154(%rbp)
cmpl $0x0, -0x154(%rbp)
jge 0x6b989
leaq 0x75f97(%rip), %rdi     # 0xe1916
callq 0x6c060
jmp 0x6c037
cmpl $0x0, -0x154(%rbp)
jne 0x6b9c5
leaq -0x2ce0(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x75f78(%rip), %rdx     # 0xe1925
movb $0x0, %al
callq 0x31090
leaq -0x2ce0(%rbp), %rdi
callq 0x6c060
jmp 0x6c037
jmp 0x6b6c2
movl $0x0, -0x12ce4(%rbp)
movl $0x0, -0x12ce8(%rbp)
movl $0x0, -0x12cec(%rbp)
movl $0x0, -0x12cf0(%rbp)
movq -0x40(%rbp), %rdi
movl $0x69, %esi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x61a0
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x6ba21
leaq 0x75f27(%rip), %rdi     # 0xe193e
callq 0x6c060
jmp 0x6c037
jmp 0x6ba23
leaq -0xd0(%rbp), %rax
movq %rax, -0x12d00(%rbp)
movl $0x0, -0x12cf4(%rbp)
movl -0x12cf4(%rbp), %eax
cmpq $0x10, %rax
jae 0x6ba6d
movq -0x12d00(%rbp), %rax
movl -0x12cf4(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x12cf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12cf4(%rbp)
jmp 0x6ba3b
jmp 0x6ba6f
jmp 0x6ba71
leaq -0x150(%rbp), %rax
movq %rax, -0x12d10(%rbp)
movl $0x0, -0x12d04(%rbp)
movl -0x12d04(%rbp), %eax
cmpq $0x10, %rax
jae 0x6babb
movq -0x12d10(%rbp), %rax
movl -0x12d04(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x12d04(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12d04(%rbp)
jmp 0x6ba89
jmp 0x6babd
cmpl $0x0, -0x12cec(%rbp)
jne 0x6bafd
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x14(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xd0(%rbp,%rax,8), %rcx
movq %rcx, -0xd0(%rbp,%rax,8)
movl -0x12ce8(%rbp), %eax
cmpl -0x12ce4(%rbp), %eax
jge 0x6bb42
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x14(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x150(%rbp,%rax,8), %rcx
movq %rcx, -0x150(%rbp,%rax,8)
cmpl $0x0, -0x12ce4(%rbp)
jne 0x6bb8a
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xd0(%rbp,%rax,8), %rcx
movq %rcx, -0xd0(%rbp,%rax,8)
movq $0xa, -0x168(%rbp)
movl $0xa, %eax
subq -0x168(%rbp), %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
movq %rax, -0x160(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jle 0x6bbc6
movl -0x14(%rbp), %eax
movl %eax, -0x12e3c(%rbp)
jmp 0x6bbd3
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x12e3c(%rbp)
movl -0x12e3c(%rbp), %edi
addl $0x1, %edi
leaq -0xd0(%rbp), %rsi
leaq -0x150(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x168(%rbp), %r8
callq 0x66b0
movl %eax, -0x154(%rbp)
cmpl $0x0, -0x154(%rbp)
jge 0x6bc1a
leaq 0x75d75(%rip), %rdi     # 0xe1985
callq 0x6c060
jmp 0x6c037
cmpl $0x0, -0x154(%rbp)
jne 0x6bc34
leaq 0x75d71(%rip), %rdi     # 0xe199b
callq 0x6c060
jmp 0x6c037
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xd0(%rbp,%rax,8), %rax
movq %rax, -0x12e48(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x12e48(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x6bcdc
movl $0x0, -0x12ce8(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %edi
leaq -0xace0(%rbp), %rsi
movl $0x8000, %edx           # imm = 0x8000
callq 0x6a90
movl %eax, -0x12ce4(%rbp)
cmpl $-0x1, -0x12ce4(%rbp)
jne 0x6bccc
callq 0x6060
cmpl $0x68, (%rax)
je 0x6bcc7
leaq 0x75cef(%rip), %rdi     # 0xe19b1
callq 0x6c0a0
jmp 0x6c037
cmpl $0x0, -0x12ce4(%rbp)
jne 0x6bcda
jmp 0x6c035
jmp 0x6bcdc
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x150(%rbp,%rax,8), %rax
movq %rax, -0x12e50(%rbp)
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x12e50(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x6bdc6
movq -0x40(%rbp), %rdi
leaq -0xace0(%rbp), %rsi
movslq -0x12ce8(%rbp), %rax
addq %rax, %rsi
movl -0x12ce4(%rbp), %edx
subl -0x12ce8(%rbp), %edx
callq 0x67a0
movl %eax, -0x12d14(%rbp)
cmpl $0x0, -0x12d14(%rbp)
jg 0x6bd8e
movq -0x40(%rbp), %rdi
movl $0x2, %esi
callq 0x6ad0
cmpl $0x0, %eax
jne 0x6bd8c
movq 0x9925d(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
callq 0x6380
leaq 0x75c58(%rip), %rdi     # 0xe19da
callq 0x6c060
jmp 0x6c037
jmp 0x6bdc4
movl -0x12d14(%rbp), %eax
addl -0x12ce8(%rbp), %eax
movl %eax, -0x12ce8(%rbp)
movl -0x12ce8(%rbp), %eax
cmpl -0x12ce4(%rbp), %eax
jl 0x6bdc2
movl $0x0, -0x12ce8(%rbp)
movl $0x0, -0x12ce4(%rbp)
jmp 0x6bdc4
jmp 0x6bdc6
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xd0(%rbp,%rax,8), %rax
movq %rax, -0x12e58(%rbp)
movl -0x14(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x12e58(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x6c02c
movl $0x0, -0x12cf0(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x12ce0(%rbp), %rsi
movl $0x8000, %edx           # imm = 0x8000
callq 0x6820
movl %eax, -0x12cec(%rbp)
cmpl $0x0, -0x12cec(%rbp)
jg 0x6be8c
movq -0x40(%rbp), %rdi
movl $0x1, %esi
callq 0x6ad0
cmpl $0x0, %eax
je 0x6be5e
movl $0x0, -0x12cec(%rbp)
jmp 0x6b9f2
cmpl $0x0, -0x12cec(%rbp)
jne 0x6be6c
jmp 0x6c035
movq 0x9915d(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
callq 0x6380
leaq 0x75b75(%rip), %rdi     # 0xe19f7
callq 0x6c060
jmp 0x6c037
jmp 0x6be8e
movl -0x12cf0(%rbp), %eax
cmpl -0x12cec(%rbp), %eax
jge 0x6c018
leaq -0x12d98(%rbp), %rdi
callq 0x6920
leaq -0x12d98(%rbp), %rdi
movl $0xd, %esi
callq 0x6ba0
xorl %edi, %edi
leaq -0x12d98(%rbp), %rsi
leaq -0x12e18(%rbp), %rdx
callq 0x61b0
cmpl $-0x1, %eax
jne 0x6beed
leaq 0x75b36(%rip), %rdi     # 0xe1a14
callq 0x6590
movl $0x1, %edi
callq 0x68b0
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %edi
leaq -0x12ce0(%rbp), %rsi
movslq -0x12cf0(%rbp), %rax
addq %rax, %rsi
movl -0x12cec(%rbp), %eax
subl -0x12cf0(%rbp), %eax
movslq %eax, %rdx
callq 0x6250
movl %eax, -0x12e1c(%rbp)
callq 0x6060
movl (%rax), %eax
movl %eax, -0x12e20(%rbp)
cmpl $-0x1, -0x12e1c(%rbp)
jne 0x6bf8d
cmpl $0x20, -0x12e20(%rbp)
jne 0x6bf8d
leaq -0x12d98(%rbp), %rdi
leaq -0x12e28(%rbp), %rsi
callq 0x6200
movl %eax, -0x12e24(%rbp)
cmpl $0x0, %eax
je 0x6bf8b
movl -0x12e24(%rbp), %eax
movl %eax, -0x12e5c(%rbp)
callq 0x6060
movl -0x12e5c(%rbp), %ecx
movl %ecx, (%rax)
leaq 0x75aa8(%rip), %rdi     # 0xe1a24
callq 0x6590
movl $0x1, %edi
callq 0x68b0
jmp 0x6bf8d
movl $0x2, %edi
leaq -0x12e18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x61b0
cmpl $-0x1, %eax
jne 0x6bfbd
leaq 0x75a66(%rip), %rdi     # 0xe1a14
callq 0x6590
movl $0x1, %edi
callq 0x68b0
movl -0x12e20(%rbp), %eax
movl %eax, -0x12e60(%rbp)
callq 0x6060
movl -0x12e60(%rbp), %ecx
movl %ecx, (%rax)
cmpl $-0x1, -0x12e1c(%rbp)
jne 0x6c001
callq 0x6060
cmpl $0x68, (%rax)
je 0x6bfff
callq 0x6060
cmpl $0x20, (%rax)
je 0x6bfff
leaq 0x75a32(%rip), %rdi     # 0xe1a2c
callq 0x6c0a0
jmp 0x6c037
movl -0x12e1c(%rbp), %eax
addl -0x12cf0(%rbp), %eax
movl %eax, -0x12cf0(%rbp)
jmp 0x6be8e
movl $0x0, -0x12cec(%rbp)
movl $0x0, -0x12cf0(%rbp)
jmp 0x6c02e
jmp 0x6c030
jmp 0x6b9f2
jmp 0x6c037
movq -0x40(%rbp), %rdi
callq 0x6290
movl -0x14(%rbp), %edi
callq 0x6e90
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %edi
callq 0x6e90
xorl %eax, %eax
addq $0x15690, %rsp          # imm = 0x15690
popq %rbp
retq
nop