// File: /workspace/github_c_makefile_star2_O2/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, %ebp
movq 0x8(%rsi), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsi), %rdi
callq 0x2230
movl %eax, 0x1c(%rsp)
movq 0x18(%r15), %rdi
leaq 0x3b49(%rip), %rsi      # 0x605c
movq %rdi, 0x20(%rsp)
callq 0x2290
testq %rax, %rax
setne 0x6d59(%rip)            # 0x9280
addl $-0x4, %ebp
pushq $0x7
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
xorl %r13d, %r13d
testl %eax, %eax
cmovlel %r13d, %eax
movq %rax, 0x8(%rsp)
leaq 0x6d55(%rip), %r14      # 0x929c
cmpq 0x8(%rsp), %r13
je 0x2653
imulq $0x7, %r13, %r12
imulq $0x48, %r13, %rax
leaq 0x6d2f(%rip), %rcx      # 0x9290
movl $0x1000001, (%rax,%rcx) # imm = 0x1000001
movb $0x1, 0x4(%rax,%rcx)
andl $0x0, 0x2c(%rax,%rcx)
leaq (%rcx,%rax), %rbx
andl $0x0, 0x34(%rax,%rcx)
andq $0x0, 0x40(%rax,%rcx)
movq 0x20(%r15,%r12,8), %rbp
movq %rbp, %rdi
leaq 0x3ad7(%rip), %rsi      # 0x6067
callq 0x21c0
testl %eax, %eax
jne 0x259d
movb $0x1, 0x1(%rbx)
movq %rbp, %rdi
leaq 0x3ac6(%rip), %rsi      # 0x606d
callq 0x21c0
testl %eax, %eax
jne 0x25b4
movb $0x1, 0x2(%rbx)
movq 0x28(%r15,%r12,8), %rdi
callq 0x2230
movl %eax, 0x38(%rbx)
movq 0x30(%r15,%r12,8), %rdi
callq 0x2230
movl %eax, 0x3c(%rbx)
movq 0x38(%r15,%r12,8), %rdi
callq 0x2230
movl %eax, 0x30(%rbx)
movq 0x40(%r15,%r12,8), %rdi
callq 0x291f
movl %eax, 0x28(%rbx)
movq 0x48(%r15,%r12,8), %rdi
callq 0x2968
movq %rax, 0x18(%rbx)
movq 0x50(%r15,%r12,8), %rdi
callq 0x2968
movq %rax, 0x20(%rbx)
movl 0x6c02(%rip), %ecx      # 0x920c
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x2647
movq 0x18(%rbx), %rsi
movq (%rsi,%rdx,8), %rdi
movb $0x1, %sil
cmpb $0x0, (%rdi)
movb $0x1, %dil
je 0x262a
movb $0x0, 0x3(%rbx)
xorl %edi, %edi
movb %dil, -0x7(%r14,%rdx)
movq (%rax,%rdx,8), %rdi
cmpb $0x0, (%rdi)
je 0x263e
movb $0x0, 0x4(%rbx)
xorl %esi, %esi
movb %sil, (%r14,%rdx)
incq %rdx
jmp 0x260c
incq %r13
addq $0x48, %r14
jmp 0x2547
pushq $0x1
popq %rdx
movl $0x7a120, %ecx          # imm = 0x7A120
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %esi
callq 0x44a2
cmpl $0x0, (%rax)
jne 0x2903
movq 0x98(%rax), %rcx
leaq 0x6b90(%rip), %rdx      # 0x9210
movq %rdx, 0x128(%rcx)
movq %rax, %rdi
movq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x4982
leaq 0x39ec(%rip), %r14      # 0x6089
pushq $0x1
popq %rbp
xorl %r15d, %r15d
leaq 0x6be6(%rip), %rbx      # 0x9290
cmpq $0x64, %r15
je 0x28f2
imulq $0x48, %r15, %r12
cmpb $0x0, (%r12,%rbx)
je 0x287c
movq %r15, 0x10(%rsp)
addq %rbx, %r12
cmpb $0x1, 0x1(%r12)
jne 0x26e6
movl 0x2c(%r12), %esi
leaq 0x39ad(%rip), %rdi      # 0x608c
xorl %eax, %eax
callq 0x2100
cmpb $0x1, 0x2(%r12)
jne 0x28cb
leaq 0x40(%r12), %r15
movq 0x40(%r12), %rax
testq %rax, %rax
je 0x28bc
leaq 0x10(%rax), %r14
movl %ebp, %edx
movl %ebp, %ecx
movq %r12, 0x8(%rsp)
testl %edx, %edx
je 0x2884
movl %ecx, %edi
negl %edi
xorl %esi, %esi
movq %r14, %rdx
xorl %r14d, %r14d
xorl %r12d, %r12d
testq %rdx, %rdx
je 0x284d
incl %esi
movl %ebp, %r9d
movq %rdx, %r10
leal (%rdi,%r9), %r8d
cmpl $0x1, %r8d
je 0x275a
movq 0x10(%r10), %r10
testq %r10, %r10
je 0x275f
movq 0x10(%rax), %r8
addq 0x20(%r8), %r10
incl %r9d
jmp 0x273a
movl %ecx, %r9d
jmp 0x2762
xorl %r10d, %r10d
movl %ecx, %r11d
testl %r11d, %r11d
setne %bl
testq %r10, %r10
setne %r8b
andb %bl, %r8b
testl %r9d, %r9d
je 0x27b3
testb %r8b, %r8b
je 0x27cf
movq 0x10(%rax), %r8
movq 0x20(%r8), %rbx
movq %rdx, %r8
subq %rbx, %r8
movq %r10, %r13
subq %rbx, %r13
movl 0x8(%r13), %r13d
cmpl 0x8(%r8), %r13d
jle 0x27e2
movq 0x10(%r10), %r8
addq %r8, %rbx
testq %r8, %r8
movq %rbx, %r13
cmoveq %r8, %r13
decl %r11d
jmp 0x27fb
testb %r8b, %r8b
je 0x2845
movq 0x10(%r10), %r13
testq %r13, %r13
je 0x27f2
movq 0x10(%rax), %r8
addq 0x20(%r8), %r13
jmp 0x27f5
movq 0x10(%rdx), %rbx
testq %rbx, %rbx
je 0x2803
movq 0x10(%rax), %r8
addq 0x20(%r8), %rbx
jmp 0x2805
movq 0x10(%rdx), %r8
addq %r8, %rbx
testq %r8, %r8
cmoveq %r8, %rbx
jmp 0x2805
xorl %r13d, %r13d
decl %r11d
xorl %r9d, %r9d
movq %rdx, %rbx
movq %r10, %r8
jmp 0x280e
xorl %ebx, %ebx
decl %r9d
movq %r10, %r13
movq %rdx, %r8
testq %r12, %r12
je 0x282d
movq 0x10(%rax), %rdx
movq %r8, %r10
subq 0x20(%rdx), %r10
movq %r10, 0x10(%r12)
movq 0x10(%rax), %rdx
subq 0x20(%rdx), %r12
jmp 0x2833
movq %r8, %r14
xorl %r12d, %r12d
movq %r12, 0x8(%r8)
movq %rbx, %rdx
movq %r13, %r10
movq %r8, %r12
jmp 0x2765
movq %r10, %rdx
jmp 0x2729
andq $0x0, 0x10(%r12)
cmpl $0x1, %esi
ja 0x286e
movq 0x10(%rax), %rdx
movq %r12, 0x18(%rdx)
movq %r14, %rax
subq 0x20(%rdx), %rax
movq %rax, (%r15)
xorl %edx, %edx
jmp 0x2870
movl %ebp, %edx
movq 0x8(%rsp), %r12
addl %ecx, %ecx
jmp 0x2712
incq %r15
jmp 0x26aa
leaq 0x6a05(%rip), %rbx      # 0x9290
leaq 0x37f7(%rip), %r14      # 0x6089
jmp 0x28bc
movq %r12, %rdi
callq 0x31b2
testb %al, %al
jne 0x28b8
movq (%r15), %rsi
movl 0x8(%r15), %edx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2100
movq 0x85f8(%rip), %r15      # 0xaeb0
addq $0x20, %r15
movq (%r15), %r15
movq %r15, 0x85ea(%rip)      # 0xaeb0
testq %r15, %r15
jne 0x2894
movq 0x10(%rsp), %r15
incq %r15
imulq $0x48, %r15, %rax
cmpb $0x1, (%rax,%rbx)
jne 0x26aa
leaq 0x37ac(%rip), %rdi      # 0x6094
callq 0x2080
jmp 0x26aa
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x4, %rax
leaq 0x3765(%rip), %rdi      # 0x6073
movq %rax, %rsi
xorl %eax, %eax
callq 0x2100
xorl %edi, %edi
callq 0x2250