// File: /workspace/github_c_makefile_star2_O3/winton[P]redis_parse/redis_parse.c
// Function: main
int main(int argc, char *argv[]) {
	// Argv vars
	char *host = argv[1];
	int port = atoi(argv[2]);
	char *cmd = argv[3];

	with_scores = strstr(cmd, "WITHSCORES") ? true : false;

	int x, y, start;
	for (x = 0; x < ((argc - 4) / 7); x++) {
		start = x * 7 + 4;

		queries[x].used = true;
		queries[x].display_count = false;
		queries[x].display_group = false;
		queries[x].query_empty = true;
		queries[x].exclude_empty = true;
		queries[x].count = 0;
		queries[x].displayed = 0;
		queries[x].hash = NULL;

		if (strcmp(argv[start], "COUNT") == 0)
			queries[x].display_count = true;

		if (strcmp(argv[start], "GROUP") == 0)
			queries[x].display_group = true;

		queries[x].start = atoi(argv[start+1]);
		queries[x].finish = atoi(argv[start+2]);
		queries[x].limit = atoi(argv[start+3]);
		queries[x].column = column_to_index(argv[start+4]);
		queries[x].query = to_fields(argv[start+5]);
		queries[x].exclude = to_fields(argv[start+6]);

		// Query empty?
		for (y = 0; y < columns_count; y++) {
			if (strlen(queries[x].query[y]) > 0) {
				queries[x].query_empty = false;
				queries[x].query_empty_at[y] = false;
			} else
				queries[x].query_empty_at[y] = true;
				
			if (strlen(queries[x].exclude[y]) > 0) {
				queries[x].exclude_empty = false;
				queries[x].exclude_empty_at[y] = false;
			} else
				queries[x].exclude_empty_at[y] = true;
		}
	}

	// Connect to redis
	struct timeval timeout = { 1, 500000 }; // 1.5 seconds
	redisContext *c = redisConnectWithTimeout(host, port, timeout);
	if (c->err) {
		printf("Connection error: %s\n", c->errstr);
		exit(0);
	}

	// Use custom Hiredis reader functions
	c->reader->fn = &redisExtReplyObjectFunctions;

	// Execute command
	redisReply *reply = redisCommand(c, cmd);

	// Display results
	int q;
	for (q = 0; q < 100; q++) {
		if (queries[q].used != true)
			continue;

		if (queries[q].display_count) {
			printf("%i\n", queries[q].count);
		}

		if (queries[q].display_group) {
			HASH_SORT(queries[q].hash, count_sort);
			for(hash_record = queries[q].hash; hash_record != NULL; hash_record = hash_record->hh.next) {
				if (!limit_reached(&queries[q]))
					printf("%s|%i\n", hash_record->field, hash_record->count);
			}	
		}

		if (queries[q + 1].used)
			printf("[END]\n");
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movl %edi, %ebx
movq 0x8(%rsi), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rsi), %rdi
callq 0x2230
movl %eax, 0xc(%rsp)
movq 0x18(%r15), %rdi
leaq 0x4b3d(%rip), %rsi      # 0x705c
movq %rdi, 0x10(%rsp)
callq 0x2290
testq %rax, %rax
setne 0x7d4d(%rip)            # 0xa280
cmpl $0xb, %ebx
jl 0x2684
movq %rbx, %rcx
addl $-0x4, %ecx
imulq $0x24924925, %rcx, %rax # imm = 0x24924925
shrq $0x20, %rax
subl %eax, %ecx
shrl %ecx
addl %eax, %ecx
shrl $0x2, %ecx
movq %rcx, 0x20(%rsp)
leaq 0x7d3a(%rip), %rbx      # 0xa29c
xorl %r14d, %r14d
leaq (,%r14,8), %r12
subq %r14, %r12
leaq (%r14,%r14,8), %rax
leaq 0x7d15(%rip), %rcx      # 0xa290
leaq (%rcx,%rax,8), %r13
movl $0x1000001, (%r13)      # imm = 0x1000001
movb $0x1, 0x4(%r13)
xorl %eax, %eax
movl %eax, 0x2c(%r13)
movl %eax, 0x34(%r13)
movq $0x0, 0x40(%r13)
movq 0x20(%r15,%r12,8), %rbp
movq %rbp, %rdi
leaq 0x4aba(%rip), %rsi      # 0x7067
callq 0x21c0
testl %eax, %eax
jne 0x25bb
movb $0x1, 0x1(%r13)
movq %rbp, %rdi
leaq 0x4aa8(%rip), %rsi      # 0x706d
callq 0x21c0
testl %eax, %eax
jne 0x25d3
movb $0x1, 0x2(%r13)
movq 0x28(%r15,%r12,8), %rdi
callq 0x2230
movl %eax, 0x38(%r13)
movq 0x30(%r15,%r12,8), %rdi
callq 0x2230
movl %eax, 0x3c(%r13)
movq 0x38(%r15,%r12,8), %rdi
callq 0x2230
movl %eax, 0x30(%r13)
movq 0x40(%r15,%r12,8), %rdi
callq 0x2961
movl %eax, 0x28(%r13)
movq 0x48(%r15,%r12,8), %rdi
callq 0x29b6
movq %rax, 0x18(%r13)
movq 0x50(%r15,%r12,8), %rdi
callq 0x29b6
movq %rax, 0x20(%r13)
movl 0x7bdf(%rip), %ecx      # 0xa20c
testq %rcx, %rcx
je 0x2672
movq 0x18(%r13), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %r8
movb $0x1, %dil
cmpb $0x0, (%r8)
movb $0x1, %r8b
je 0x2650
movb $0x0, 0x3(%r13)
xorl %r8d, %r8d
movb %r8b, -0x7(%rbx,%rsi)
movq (%rax,%rsi,8), %r8
cmpb $0x0, (%r8)
je 0x2666
movb $0x0, 0x4(%r13)
xorl %edi, %edi
movb %dil, (%rbx,%rsi)
incq %rsi
cmpq %rsi, %rcx
jne 0x2638
incq %r14
addq $0x48, %rbx
cmpq 0x20(%rsp), %r14
jne 0x2565
movl $0x1, %edx
movl $0x7a120, %ecx          # imm = 0x7A120
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x456a
cmpl $0x0, (%rax)
jne 0x2945
movq 0x98(%rax), %rcx
leaq 0x7b5d(%rip), %rdx      # 0xa210
movq %rdx, 0x128(%rcx)
movq %rax, %rdi
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x4bb4
leaq 0x7bc0(%rip), %rbx      # 0xa290
xorl %r13d, %r13d
leaq (,%r13,8), %rax
addq %r13, %rax
cmpb $0x0, (%rbx,%rax,8)
je 0x28fa
leaq (%rbx,%rax,8), %rbp
cmpb $0x1, 0x1(%rbp)
jne 0x2703
movl 0x2c(%rbp), %esi
leaq 0x4990(%rip), %rdi      # 0x708c
xorl %eax, %eax
callq 0x2100
cmpb $0x1, 0x2(%rbp)
jne 0x290a
movq 0x40(%rbp), %rax
testq %rax, %rax
je 0x28ff
leaq 0x10(%rax), %r14
movl $0x1, %ebx
movq %r14, %r15
movl %ebx, %edi
negl %edi
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r8d, %r8d
movq %r15, %rcx
movl $0x0, %r10d
testl %ebx, %ebx
je 0x2774
movl $0x1, %r10d
movq %r15, %rcx
movq 0x10(%rcx), %rcx
testq %rcx, %rcx
je 0x2772
movq 0x10(%rax), %rdx
addq 0x20(%rdx), %rcx
leal (%rdi,%r10), %edx
incl %edx
movl %r10d, %esi
incl %esi
movl %esi, %r10d
cmpl $0x1, %edx
jne 0x2749
movl %ebx, %r10d
jmp 0x2774
xorl %ecx, %ecx
incl %r8d
movl %ebx, %edx
testl %edx, %edx
setne %r9b
testq %rcx, %rcx
setne %sil
andb %r9b, %sil
testl %r10d, %r10d
je 0x27c6
testb %sil, %sil
je 0x27e2
movq 0x10(%rax), %rsi
movq 0x20(%rsi), %r9
movq %r15, %rsi
subq %r9, %rsi
movq %rcx, %r11
subq %r9, %r11
movl 0x8(%r11), %r11d
cmpl 0x8(%rsi), %r11d
jle 0x27f5
movq 0x10(%rcx), %rsi
addq %rsi, %r9
testq %rsi, %rsi
movq %r9, %r11
cmoveq %rsi, %r11
decl %edx
jmp 0x280d
testb %sil, %sil
je 0x2858
movq 0x10(%rcx), %r11
testq %r11, %r11
je 0x2805
movq 0x10(%rax), %rsi
addq 0x20(%rsi), %r11
jmp 0x2808
movq 0x10(%r15), %r9
testq %r9, %r9
je 0x2815
movq 0x10(%rax), %rsi
addq 0x20(%rsi), %r9
jmp 0x2818
movq 0x10(%r15), %rsi
addq %rsi, %r9
testq %rsi, %rsi
cmoveq %rsi, %r9
jmp 0x2818
xorl %r11d, %r11d
decl %edx
xorl %r10d, %r10d
movq %r15, %r9
movq %rcx, %rsi
jmp 0x2821
xorl %r9d, %r9d
decl %r10d
movq %rcx, %r11
movq %r15, %rsi
testq %r12, %r12
je 0x2840
movq 0x10(%rax), %rcx
movq %rsi, %r15
subq 0x20(%rcx), %r15
movq %r15, 0x10(%r12)
movq 0x10(%rax), %rcx
subq 0x20(%rcx), %r12
jmp 0x2846
movq %rsi, %r14
xorl %r12d, %r12d
movq %r12, 0x8(%rsi)
movq %r9, %r15
movq %r11, %rcx
movq %rsi, %r12
jmp 0x2779
movq %rcx, %r15
testq %rcx, %rcx
jne 0x2736
movq $0x0, 0x10(%r12)
cmpl $0x1, %r8d
jbe 0x2890
addl %ebx, %ebx
testq %r14, %r14
jne 0x2723
movq $0x0, 0x10
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x10(%rax), %rcx
movq %r12, 0x18(%rcx)
movq %r14, %rax
subq 0x20(%rcx), %rax
movq %rax, 0x40(%rbp)
movq %rax, 0x9606(%rip)      # 0xbeb0
testq %r14, %r14
leaq 0x79dc(%rip), %rbx      # 0xa290
leaq 0x47ce(%rip), %r14      # 0x7089
je 0x290a
movl 0x30(%rbp), %edx
cmpl $-0x1, %edx
je 0x28e8
movl 0x34(%rbp), %ecx
cmpl %edx, %ecx
jge 0x28e8
incl %ecx
movl %ecx, 0x34(%rbp)
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2100
movq 0x95c8(%rip), %rax      # 0xbeb0
movq 0x20(%rax), %rax
movq %rax, 0x95bd(%rip)      # 0xbeb0
testq %rax, %rax
jne 0x28bd
jmp 0x290a
incq %r13
jmp 0x292a
movq $0x0, 0x95a6(%rip) # 0xbeb0
incq %r13
leaq (,%r13,8), %rax
addq %r13, %rax
cmpb $0x1, (%rbx,%rax,8)
jne 0x292a
leaq 0x476f(%rip), %rdi      # 0x7094
callq 0x2080
cmpq $0x64, %r13
jne 0x26d3
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x4, %rax
leaq 0x4723(%rip), %rdi      # 0x7073
movq %rax, %rsi
xorl %eax, %eax
callq 0x2100
xorl %edi, %edi
callq 0x2250