// File: /workspace/github_c_makefile_star2_O0/ToniWestbrook[P]paladin/uniprot.c
// Function: retrieveUniprotOnline
void retrieveUniprotOnline(UniprotList * passList, CURLBuffer * retBuffer, const char * passProxy) {
	int entryIdx, queryIdx, parseIdx, errorIdx, queryCount;
    long unsigned int jobSize;
	CURL * curlHandle;
	CURLcode curlResult;
	CURLBuffer tempBuffer;
	char queryString[UNIPROT_MAX_SUBMIT * 50], buildString[UNIPROT_MAX_SUBMIT * 50], jobID[50];
    char url[1024];
	char * httpString;

	// Init structures
	curlHandle = curl_easy_init();
	initCURLBuffer(retBuffer, UNIPROT_BUFFER_GROW);
	initCURLBuffer(&tempBuffer, UNIPROT_BUFFER_GROW);
	queryCount = (passList->entryCount < UNIPROT_MAX_SUBMIT) ? passList->entryCount : UNIPROT_MAX_SUBMIT;

	for (entryIdx = 0 ; entryIdx < passList->entryCount ; ) {
        // Preparation - build and sanitize query string outside of error loop 
        queryString[0] = 0;
        for (queryIdx = 0 ; (queryIdx < queryCount) && (entryIdx < passList->entryCount) ; entryIdx++) {
            for (parseIdx = 0 ; parseIdx < strlen(passList->entries[entryIdx].id) ; parseIdx++) {
                sprintf(buildString, "%s%s ", queryString, passList->entries[entryIdx].id);
                sprintf(queryString, "%s", buildString);
                queryIdx++;
                break;
            }
        }

        httpString = curl_easy_escape(curlHandle, queryString, 0);
        sprintf(queryString, "ids=%s&from=UniProtKB_AC-ID&to=UniProtKB", httpString);
        curl_free(httpString);

		// Restart a limited number of times if errors encountered
		for (errorIdx = 0 ; errorIdx < UNIPROT_MAX_ERROR ; errorIdx++) {
            // Stage 1 - Submit query for processing
            logMessage(__func__, LOG_LEVEL_MESSAGE, "Submitting %d of %d entries to UniProt...\n", entryIdx, passList->entryCount);

			curl_easy_setopt(curlHandle, CURLOPT_URL, "https://rest.uniprot.org/idmapping/run");
			curl_easy_setopt(curlHandle, CURLOPT_POSTFIELDS, queryString);
			curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 0L);
			curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, receiveUniprotOutput);
			curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &tempBuffer);
            if (passProxy) curl_easy_setopt(curlHandle, CURLOPT_PROXY, passProxy);

			resetCURLBuffer(&tempBuffer);
			curlResult = curl_easy_perform(curlHandle);

			if (curlResult != CURLE_OK) {
				logMessage(__func__, LOG_LEVEL_ERROR, "CURL: %s (retrying)\n", curl_easy_strerror(curlResult));
				continue;
			}

            if (strncmp(tempBuffer.buffer, "{\"jobId\":", 9) != 0) {
				logMessage(__func__, LOG_LEVEL_ERROR, "Received unexpected job ID response (retrying)\n");
				continue;
            }

			// Stage 2 - Wait for results
            jobSize = strnlen(tempBuffer.buffer, 8192) - 12;

            if (jobSize > 49) {
				logMessage(__func__, LOG_LEVEL_ERROR, "Received unexpected job ID size (retrying)\n");
				continue;
            }

			strncpy(jobID, tempBuffer.buffer + 10, jobSize);
			sprintf(url, "https://rest.uniprot.org/idmapping/status/%s", jobID);
            queryString[0] = '\0';

			resetCURLBuffer(&tempBuffer);
			curl_easy_setopt(curlHandle, CURLOPT_URL, url);
            curl_easy_setopt(curlHandle, CURLOPT_HTTPGET, 1l);

			while (strcmp(tempBuffer.buffer, "{\"jobStatus\":\"FINISHED\"}") != 0) {
				sleep(1);
				resetCURLBuffer(&tempBuffer);
				curlResult = curl_easy_perform(curlHandle);
				if (curlResult != CURLE_OK) break;
			}

			if (curlResult != CURLE_OK) {
				logMessage(__func__, LOG_LEVEL_ERROR, "CURL: %s (retrying)\n", curl_easy_strerror(curlResult));
				continue;
			}

			// Stage 3 - Retrieve Results
			sprintf(queryString, "format=tsv&fields=accession,organism_name,protein_name,gene_names,cc_pathway,feature_count,go,reviewed,protein_existence,cc_function,xref_kegg,xref_geneid,xref_patric,xref_ensemblbacteria,organism_id,lineage");
			sprintf(url, "https://rest.uniprot.org/idmapping/uniprotkb/results/stream/%s?%s", jobID, queryString);
			curl_easy_setopt(curlHandle, CURLOPT_URL, url);
			curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, retBuffer);

			curlResult = curl_easy_perform(curlHandle);

			if (curlResult != CURLE_OK) {
				logMessage(__func__, LOG_LEVEL_ERROR, "CURL: %s\n", curl_easy_strerror(curlResult));
				continue;
			}

			break;
		}

        // Check if download failed max number of times
        if (errorIdx == UNIPROT_MAX_ERROR) {
            logMessage(__func__, LOG_LEVEL_ERROR, "Download failed after multiple retries.  Please check Internet connection and finalize with PALADIN-plugins\n");
            break;
        }
	}

	curl_easy_cleanup(curlHandle);
	freeCURLBuffer(&tempBuffer);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x7a5e0, %rsp          # imm = 0x7A5E0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
callq 0x3420
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl $0x2faf080, %esi        # imm = 0x2FAF080
callq 0xd550
leaq -0x58(%rbp), %rdi
movl $0x2faf080, %esi        # imm = 0x2FAF080
callq 0xd550
movq -0x8(%rbp), %rax
cmpl $0x1388, 0x8(%rax)      # imm = 0x1388
jge 0xc1b8
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x7a5cc(%rbp)
jmp 0xc1c5
movl $0x1388, %eax           # imm = 0x1388
movl %eax, -0x7a5cc(%rbp)
jmp 0xc1c5
movl -0x7a5cc(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0xc630
movb $0x0, -0x3d0f0(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl -0x2c(%rbp), %ecx
movb %al, -0x7a5cd(%rbp)
jge 0xc216
movl -0x1c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
setl %al
movb %al, -0x7a5cd(%rbp)
movb -0x7a5cd(%rbp), %al
testb $0x1, %al
jne 0xc225
jmp 0xc2c6
movl $0x0, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movq %rax, -0x7a5d8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x30f0
movq %rax, %rcx
movq -0x7a5d8(%rbp), %rax
cmpq %rcx, %rax
jae 0xc2b6
leaq -0x7a180(%rbp), %rdi
leaq -0x3d0f0(%rbp), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
leaq 0x2d147(%rip), %rsi     # 0x393d1
movb $0x0, %al
callq 0x3070
leaq -0x3d0f0(%rbp), %rdi
leaq -0x7a180(%rbp), %rdx
leaq 0x2dc84(%rip), %rsi     # 0x39f2a
movb $0x0, %al
callq 0x3070
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xc2b8
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xc1f3
movq -0x40(%rbp), %rdi
leaq -0x3d0f0(%rbp), %rsi
xorl %edx, %edx
callq 0x3670
movq %rax, -0x7a5c8(%rbp)
leaq -0x3d0f0(%rbp), %rdi
movq -0x7a5c8(%rbp), %rdx
leaq 0x2d0e3(%rip), %rsi     # 0x393d7
movb $0x0, %al
callq 0x3070
movq -0x7a5c8(%rbp), %rdi
callq 0x30a0
movl $0x0, -0x28(%rbp)
cmpl $0x5, -0x28(%rbp)
jge 0xc609
movl -0x1c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl 0x8(%rax), %r8d
leaq 0x2d0d6(%rip), %rdi     # 0x39400
movl $0x3, %esi
leaq 0x2d0e0(%rip), %rdx     # 0x39416
movb $0x0, %al
callq 0x12510
movq -0x40(%rbp), %rdi
movl $0x2712, %esi           # imm = 0x2712
leaq 0x2d0f4(%rip), %rdx     # 0x39441
movb $0x0, %al
callq 0x3340
movq -0x40(%rbp), %rdi
leaq -0x3d0f0(%rbp), %rdx
movl $0x271f, %esi           # imm = 0x271F
movb $0x0, %al
callq 0x3340
movq -0x40(%rbp), %rdi
movl $0x34, %esi
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0x3340
movq -0x40(%rbp), %rdi
movl $0x4e2b, %esi           # imm = 0x4E2B
leaq 0x1211(%rip), %rdx      # 0xd5a0
movb $0x0, %al
callq 0x3340
movq -0x40(%rbp), %rdi
movl $0x2711, %esi           # imm = 0x2711
leaq -0x58(%rbp), %rdx
movb $0x0, %al
callq 0x3340
cmpq $0x0, -0x18(%rbp)
je 0xc3c5
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x2714, %esi           # imm = 0x2714
movb $0x0, %al
callq 0x3340
leaq -0x58(%rbp), %rdi
callq 0xd670
movq -0x40(%rbp), %rdi
callq 0x3440
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xc40a
movl -0x44(%rbp), %edi
callq 0x3500
movq %rax, %rcx
leaq 0x2d00e(%rip), %rdi     # 0x39400
movl $0x1, %esi
leaq 0x2d06a(%rip), %rdx     # 0x39468
movb $0x0, %al
callq 0x12510
jmp 0xc5fb
movq -0x58(%rbp), %rdi
leaq 0x2d068(%rip), %rsi     # 0x3947d
movl $0x9, %edx
callq 0x3110
cmpl $0x0, %eax
je 0xc443
leaq 0x2cfd5(%rip), %rdi     # 0x39400
movl $0x1, %esi
leaq 0x2d050(%rip), %rdx     # 0x39487
movb $0x0, %al
callq 0x12510
jmp 0xc5fb
movq -0x58(%rbp), %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0x3580
subq $0xc, %rax
movq %rax, -0x38(%rbp)
cmpq $0x31, -0x38(%rbp)
jbe 0xc47f
leaq 0x2cf99(%rip), %rdi     # 0x39400
movl $0x1, %esi
leaq 0x2d044(%rip), %rdx     # 0x394b7
movb $0x0, %al
callq 0x12510
jmp 0xc5fb
leaq -0x7a1c0(%rbp), %rdi
movq -0x58(%rbp), %rsi
addq $0xa, %rsi
movq -0x38(%rbp), %rdx
callq 0x3120
leaq -0x7a5c0(%rbp), %rdi
leaq -0x7a1c0(%rbp), %rdx
leaq 0x2d037(%rip), %rsi     # 0x394e3
movb $0x0, %al
callq 0x3070
movb $0x0, -0x3d0f0(%rbp)
leaq -0x58(%rbp), %rdi
callq 0xd670
movq -0x40(%rbp), %rdi
leaq -0x7a5c0(%rbp), %rdx
movl $0x2712, %esi           # imm = 0x2712
movb $0x0, %al
callq 0x3340
movq -0x40(%rbp), %rdi
movl $0x50, %esi
movl $0x1, %edx
movb $0x0, %al
callq 0x3340
movq -0x58(%rbp), %rdi
leaq 0x2d016(%rip), %rsi     # 0x39510
callq 0x34b0
cmpl $0x0, %eax
je 0xc52d
movl $0x1, %edi
callq 0x31d0
leaq -0x58(%rbp), %rdi
callq 0xd670
movq -0x40(%rbp), %rdi
callq 0x3440
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xc52b
jmp 0xc52d
jmp 0xc4ef
cmpl $0x0, -0x44(%rbp)
je 0xc55d
movl -0x44(%rbp), %edi
callq 0x3500
movq %rax, %rcx
leaq 0x2cebb(%rip), %rdi     # 0x39400
movl $0x1, %esi
leaq 0x2cf17(%rip), %rdx     # 0x39468
movb $0x0, %al
callq 0x12510
jmp 0xc5fb
leaq -0x3d0f0(%rbp), %rdi
leaq 0x2cfbe(%rip), %rsi     # 0x39529
movb $0x0, %al
callq 0x3070
leaq -0x7a5c0(%rbp), %rdi
leaq -0x7a1c0(%rbp), %rdx
leaq -0x3d0f0(%rbp), %rcx
leaq 0x2d06b(%rip), %rsi     # 0x395f9
movb $0x0, %al
callq 0x3070
movq -0x40(%rbp), %rdi
leaq -0x7a5c0(%rbp), %rdx
movl $0x2712, %esi           # imm = 0x2712
movb $0x0, %al
callq 0x3340
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0x2711, %esi           # imm = 0x2711
movb $0x0, %al
callq 0x3340
movq -0x40(%rbp), %rdi
callq 0x3440
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0xc5f9
movl -0x44(%rbp), %edi
callq 0x3500
movq %rax, %rcx
leaq 0x2ce1c(%rip), %rdi     # 0x39400
movl $0x1, %esi
leaq 0x2d04b(%rip), %rdx     # 0x3963b
movb $0x0, %al
callq 0x12510
jmp 0xc5fb
jmp 0xc609
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xc30e
cmpl $0x5, -0x28(%rbp)
jne 0xc62b
leaq 0x2cdea(%rip), %rdi     # 0x39400
movl $0x1, %esi
leaq 0x2d023(%rip), %rdx     # 0x39645
movb $0x0, %al
callq 0x12510
jmp 0xc630
jmp 0xc1d5
movq -0x40(%rbp), %rdi
callq 0x33e0
leaq -0x58(%rbp), %rdi
callq 0xcd80
addq $0x7a5e0, %rsp          # imm = 0x7A5E0
popq %rbp
retq
nopl (%rax,%rax)