// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movq 0x8(%rsi), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%rsi), %rdi
callq 0x2230
movl %eax, 0x14(%rsp)
movq 0x18(%r15), %rdi
leaq 0x4b35(%rip), %rsi      # 0x705c
movq %rdi, 0x18(%rsp)
callq 0x2290
testq %rax, %rax
setne 0x7d45(%rip)            # 0xa280
cmpl $0xb, %r12d
jl 0x26b7
movl %r12d, %eax
addl $-0x4, %eax
cltq
imulq $-0x6db6db6d, %rax, %rax # imm = 0x92492493
shrq $0x20, %rax
addl %r12d, %eax
addl $-0x4, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x2, %eax
addl %ecx, %eax
cmpl $0x2, %eax
movl $0x1, %ecx
cmovgel %eax, %ecx
movq %rcx, 0x8(%rsp)
leaq 0x7d1d(%rip), %rbx      # 0xa29c
xorl %r13d, %r13d
leaq (,%r13,8), %rbp
subq %r13, %rbp
leaq (,%r13,8), %rax
addq %r13, %rax
leaq 0x7cf1(%rip), %rcx      # 0xa290
leaq (%rcx,%rax,8), %r12
movl $0x1000001, (%r12)      # imm = 0x1000001
movb $0x1, 0x4(%r12)
xorl %eax, %eax
movl %eax, 0x2c(%r12)
movl %eax, 0x34(%r12)
movq $0x0, 0x40(%r12)
movq 0x20(%r15,%rbp,8), %r14
movq %r14, %rdi
leaq 0x4a92(%rip), %rsi      # 0x7067
callq 0x21c0
testl %eax, %eax
jne 0x25e4
movb $0x1, 0x1(%r12)
movq %r14, %rdi
leaq 0x4a7f(%rip), %rsi      # 0x706d
callq 0x21c0
testl %eax, %eax
jne 0x25fd
movb $0x1, 0x2(%r12)
movq 0x28(%r15,%rbp,8), %rdi
callq 0x2230
movl %eax, 0x38(%r12)
movq 0x30(%r15,%rbp,8), %rdi
callq 0x2230
movl %eax, 0x3c(%r12)
movq 0x38(%r15,%rbp,8), %rdi
callq 0x2230
movl %eax, 0x30(%r12)
movq 0x40(%r15,%rbp,8), %rdi
callq 0x29c4
movl %eax, 0x28(%r12)
movq 0x48(%r15,%rbp,8), %rdi
callq 0x2a19
movq %rax, 0x18(%r12)
movq 0x50(%r15,%rbp,8), %rdi
callq 0x2a19
movq %rax, 0x20(%r12)
movl 0x7baf(%rip), %ecx      # 0xa20c
testq %rcx, %rcx
je 0x26a5
movq 0x18(%r12), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %r8
movb $0x1, %dil
cmpb $0x0, (%r8)
movb $0x1, %r8b
je 0x2682
movb $0x0, 0x3(%r12)
xorl %r8d, %r8d
movb %r8b, -0x7(%rbx,%rsi)
movq (%rax,%rsi,8), %r8
cmpb $0x0, (%r8)
je 0x2699
movb $0x0, 0x4(%r12)
xorl %edi, %edi
movb %dil, (%rbx,%rsi)
incq %rsi
cmpq %rsi, %rcx
jne 0x2669
incq %r13
addq $0x48, %rbx
cmpq 0x8(%rsp), %r13
jne 0x2582
movl $0x1, %edx
movl $0x7a120, %ecx          # imm = 0x7A120
movq 0x20(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x46db
cmpl $0x0, (%rax)
jne 0x29a8
movq 0x98(%rax), %rcx
leaq 0x7b2a(%rip), %rdx      # 0xa210
movq %rdx, 0x128(%rcx)
movq %rax, %rdi
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x4d2b
leaq 0x7b8d(%rip), %r14      # 0xa290
xorl %r13d, %r13d
leaq (,%r13,8), %r12
addq %r13, %r12
cmpb $0x0, (%r14,%r12,8)
je 0x298a
leaq (%r14,%r12,8), %rbx
cmpb $0x1, 0x1(%rbx)
jne 0x2737
movl 0x2c(%rbx), %esi
leaq 0x495c(%rip), %rdi      # 0x708c
xorl %eax, %eax
callq 0x2100
cmpb $0x1, 0x2(%rbx)
jne 0x2976
movq %r12, 0x8(%rsp)
movq 0x40(%rbx), %r15
testq %r15, %r15
je 0x2918
addq $0x10, %r15
movl $0x1, %eax
testq %r15, %r15
jne 0x276e
movb $0x1, %cl
xorl %r15d, %r15d
xorl %r12d, %r12d
jmp 0x28ea
movl %eax, %edx
negl %edx
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %esi, %esi
movq %r15, %r8
movl $0x0, %r9d
testl %eax, %eax
je 0x27c9
movl $0x1, %r9d
movq %r15, %r8
movq 0x10(%r8), %r8
testq %r8, %r8
je 0x27a6
movq 0x40(%rbx), %rcx
movq 0x10(%rcx), %rcx
addq 0x20(%rcx), %r8
jmp 0x27a9
xorl %r8d, %r8d
testq %r8, %r8
je 0x27c6
leal (%rdx,%r9), %ecx
incl %ecx
movl %r9d, %edi
incl %edi
movl %edi, %r9d
cmpl $0x1, %ecx
jne 0x278f
movl %eax, %r9d
jmp 0x27c9
xorl %r8d, %r8d
incl %esi
movl %eax, %r11d
testl %r11d, %r11d
setne %dil
testq %r8, %r8
setne %cl
andb %dil, %cl
testl %r9d, %r9d
je 0x281f
testb %cl, %cl
je 0x283e
movq 0x40(%rbx), %rcx
movq 0x10(%rcx), %rcx
movq 0x20(%rcx), %rdi
movq %r15, %rcx
subq %rdi, %rcx
movq %r8, %r10
subq %rdi, %r10
movl 0x8(%r10), %r10d
cmpl 0x8(%rcx), %r10d
jle 0x2855
movq 0x10(%r8), %rcx
addq %rcx, %rdi
testq %rcx, %rcx
movq %rdi, %r10
cmoveq %rcx, %r10
decl %r11d
jmp 0x286e
testb %cl, %cl
je 0x28d5
movq 0x10(%r8), %r10
testq %r10, %r10
je 0x2865
movq 0x40(%rbx), %rcx
movq 0x10(%rcx), %rcx
addq 0x20(%rcx), %r10
jmp 0x2868
movq 0x10(%r15), %rdi
testq %rdi, %rdi
je 0x2876
movq 0x40(%rbx), %rcx
movq 0x10(%rcx), %rcx
addq 0x20(%rcx), %rdi
jmp 0x2878
movq 0x10(%r15), %rcx
addq %rcx, %rdi
testq %rcx, %rcx
cmoveq %rcx, %rdi
jmp 0x2878
xorl %r10d, %r10d
decl %r11d
xorl %r9d, %r9d
movq %r15, %rdi
movq %r8, %rcx
jmp 0x2881
xorl %edi, %edi
decl %r9d
movq %r8, %r10
movq %r15, %rcx
movq %rcx, %r14
testq %r12, %r12
je 0x28aa
testq %rcx, %rcx
je 0x289f
movq 0x40(%rbx), %r8
movq 0x10(%r8), %r14
movq %rcx, %r8
subq 0x20(%r14), %r8
jmp 0x28a2
xorl %r8d, %r8d
movq %r8, 0x10(%r12)
movq %rbp, %r14
testq %r12, %r12
je 0x28bd
movq 0x40(%rbx), %r8
movq 0x10(%r8), %r8
subq 0x20(%r8), %r12
jmp 0x28c0
xorl %r12d, %r12d
movq %r12, 0x8(%rcx)
movq %rdi, %r15
movq %r10, %r8
movq %r14, %rbp
movq %rcx, %r12
jmp 0x27ce
movq %r8, %r15
testq %r8, %r8
jne 0x277c
cmpl $0x2, %esi
setb %cl
movq %rbp, %r15
movq $0x0, 0x10(%r12)
testb %cl, %cl
je 0x290e
movq 0x40(%rbx), %rdx
movq 0x10(%rdx), %rdx
movq %r12, 0x18(%rdx)
movq %r15, %rsi
subq 0x20(%rdx), %rsi
movq %rsi, 0x40(%rbx)
addl %eax, %eax
testb %cl, %cl
je 0x275c
movq 0x40(%rbx), %rax
movq %rax, 0x958d(%rip)      # 0xbeb0
testq %rax, %rax
leaq 0x7963(%rip), %r14      # 0xa290
leaq 0x4755(%rip), %r15      # 0x7089
movq 0x8(%rsp), %r12
je 0x2976
movl 0x30(%rbx), %edx
cmpl $-0x1, %edx
je 0x295f
movl 0x34(%rbx), %ecx
cmpl %edx, %ecx
jge 0x295f
incl %ecx
movl %ecx, 0x34(%rbx)
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %r15, %rdi
xorl %eax, %eax
callq 0x2100
movq 0x954a(%rip), %rax      # 0xbeb0
movq 0x20(%rax), %rax
movq %rax, 0x953f(%rip)      # 0xbeb0
testq %rax, %rax
jne 0x293b
cmpb $0x1, 0x48(%r14,%r12,8)
jne 0x298a
leaq 0x470f(%rip), %rdi      # 0x7094
callq 0x2080
incq %r13
cmpq $0x64, %r13
jne 0x2706
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x4, %rax
leaq 0x46c0(%rip), %rdi      # 0x7073
movq %rax, %rsi
xorl %eax, %eax
callq 0x2100
xorl %edi, %edi
callq 0x2250