// File: /workspace/github_c_makefile_star2_O0/wkoszek[P]mini_gzip/mini_gzip_testprog.c
// Function: main
int
main(int argc, char **argv)
{
	struct mini_gzip gz;
	struct stat st;
	char	in_fn[MAX_PATH_LEN], out_fn[MAX_PATH_LEN];
	char	*sptr, *mem_in, *mem_out;
	int	level, flag_c, o, in_fd, out_fd, ret, is_gzipped, out_len;

	level = 6;
	flag_c = 0;
	while ((o = getopt(argc, argv, "cd123456789")) != -1) {
		switch (o) {
		case 'd':
			level = 0;
			break;
		case 'c':
			flag_c = 1;
			break;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			assert(o != '0');
			level = o - '0';
			break;
		default:
			abort();
			break;
		}
	}
	
	argc -= optind;
	argv += optind;

	GZAS(argc == 2, "2 file names must be passed: input and output file");

	is_gzipped = 0;
	sptr = strstr(argv[0], ".gz");
	if (sptr != NULL) {
		is_gzipped = 1;
	}
	printf("flag_c: %d is_gzipped: %d\n", flag_c, is_gzipped);

	if (is_gzipped) {
		GZAS(flag_c == 0, "Requesting to compress .gz file? Looks wrong");
	} else {
		GZAS(flag_c == 1, "Requesting to decompress normal file?");
	}

	snprintf(in_fn, sizeof(in_fn) - 1, "%s", argv[0]);
	snprintf(out_fn, sizeof(out_fn) - 1, "%s", argv[1]);

	printf("in_fn: %s out_fn: %s level %d\n", in_fn, out_fn, level);

	mem_in = calloc(1024*1024, 1);
	mem_out = calloc(1024*1024, 1);
	GZAS(mem_in != NULL, "Couldn't allocate memory for input file");
	GZAS(mem_out != NULL, "Couldn't allocate memory for output file");

	in_fd = open(in_fn, O_RDONLY);
	GZAS(in_fd != -1, "Couldn't open file %s for reading", in_fn);
	ret = fstat(in_fd, &st);
	GZAS(ret == 0, "Couldn't call fstat(), %d returned", ret);
	ret = read(in_fd, mem_in, st.st_size);
	GZAS(ret == st.st_size, "Read only %d bytes, %jd expected", ret,
							(uintmax_t)st.st_size);
	out_fd = open(out_fn, O_WRONLY|O_CREAT, st.st_mode);
	GZAS(out_fd != -1, "Couldn't create output file '%s' for writing",
								out_fn);

	if (flag_c) {
		abort();
#if 0
		mini_gz_init(&gz);
		printf("COMP\n");
		out_len = mini_gz_pack(&gz, level, mem_in, st.st_size, mem_out,
								1024*1024);
		printf("out_len = %d\n", out_len);
		ret = write(out_fd, mem_out, out_len);
		printf("ret = %d\n", ret);
		GZAS(ret == out_len, "Wrote only %d bytes, expected %d", ret, out_len);
#endif
	} else {
		printf("--- testing decompression --\n");
		ret = mini_gz_start(&gz, mem_in, st.st_size);
		GZAS(ret == 0, "mini_gz_start() failed, ret=%d", ret);
		out_len = mini_gz_unpack(&gz, mem_out, 1024*1024);
		printf("out_len = %d\n", out_len);
		ret = write(out_fd, mem_out, out_len);
		printf("ret = %d\n", ret);
		GZAS(ret == out_len, "Wrote only %d bytes, expected %d", ret, out_len);
	}
	close(in_fd);
	close(out_fd);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x930, %rsp            # imm = 0x930
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x6, -0x90c(%rbp)
movl $0x0, -0x910(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xa9ab(%rip), %rdx      # 0xc116
callq 0x1110
movl %eax, -0x914(%rbp)
cmpl $-0x1, %eax
je 0x180c
movl -0x914(%rbp), %eax
movl %eax, -0x92c(%rbp)
addl $-0x30, %eax
subl $0xa, %eax
jb 0x17c7
jmp 0x1795
movl -0x92c(%rbp), %eax
subl $0x63, %eax
je 0x17bb
jmp 0x17a2
movl -0x92c(%rbp), %eax
subl $0x64, %eax
jne 0x1802
jmp 0x17af
movl $0x0, -0x90c(%rbp)
jmp 0x1807
movl $0x1, -0x910(%rbp)
jmp 0x1807
cmpl $0x30, -0x914(%rbp)
je 0x17d2
jmp 0x17f1
leaq 0xa949(%rip), %rdi      # 0xc122
leaq 0xa94b(%rip), %rsi      # 0xc12b
movl $0x2e, %edx
leaq 0xa954(%rip), %rcx      # 0xc140
callq 0x1080
movl -0x914(%rbp), %eax
subl $0x30, %eax
movl %eax, -0x90c(%rbp)
jmp 0x1807
callq 0x1040
jmp 0x175d
movq 0xd7cd(%rip), %rax      # 0xefe0
movl (%rax), %ecx
movl -0x8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x8(%rbp)
movq 0xd7bc(%rip), %rax      # 0xefe0
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movl $0x0, -0x924(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xa907(%rip), %rsi      # 0xc157
callq 0x1130
movq %rax, -0x8f8(%rbp)
cmpq $0x0, -0x8f8(%rbp)
je 0x1870
movl $0x1, -0x924(%rbp)
movl -0x910(%rbp), %esi
movl -0x924(%rbp), %edx
leaq 0xa8d8(%rip), %rdi      # 0xc15b
movb $0x0, %al
callq 0x1060
cmpl $0x0, -0x924(%rbp)
je 0x1895
jmp 0x1897
jmp 0x1897
leaq -0x4f0(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x3ff, %esi            # imm = 0x3FF
leaq 0xa8c5(%rip), %rdx      # 0xc176
movb $0x0, %al
callq 0x1070
leaq -0x8f0(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movl $0x3ff, %esi            # imm = 0x3FF
leaq 0xa8a3(%rip), %rdx      # 0xc176
movb $0x0, %al
callq 0x1070
leaq -0x4f0(%rbp), %rsi
leaq -0x8f0(%rbp), %rdx
movl -0x90c(%rbp), %ecx
leaq 0xa884(%rip), %rdi      # 0xc179
movb $0x0, %al
callq 0x1060
movl $0x100000, %edi         # imm = 0x100000
movl $0x1, %esi
callq 0x10c0
movq %rax, -0x900(%rbp)
movl $0x100000, %edi         # imm = 0x100000
movl $0x1, %esi
callq 0x10c0
movq %rax, -0x908(%rbp)
leaq -0x4f0(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1100
movl %eax, -0x918(%rbp)
movl -0x918(%rbp), %edi
leaq -0xf0(%rbp), %rsi
callq 0x1120
movl %eax, -0x920(%rbp)
movl -0x918(%rbp), %edi
movq -0x900(%rbp), %rsi
movq -0xc0(%rbp), %rdx
callq 0x10b0
movl %eax, -0x920(%rbp)
leaq -0x8f0(%rbp), %rdi
movl -0xd8(%rbp), %edx
movl $0x41, %esi
movb $0x0, %al
callq 0x1100
movl %eax, -0x91c(%rbp)
cmpl $0x0, -0x910(%rbp)
je 0x19a2
callq 0x1040
leaq 0xa7ef(%rip), %rdi      # 0xc198
movb $0x0, %al
callq 0x1060
movq -0x900(%rbp), %rsi
movq -0xc0(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x1240
movl %eax, -0x920(%rbp)
movq -0x908(%rbp), %rsi
leaq -0x60(%rbp), %rdi
movl $0x100000, %edx         # imm = 0x100000
callq 0x1510
movl %eax, -0x928(%rbp)
movl -0x928(%rbp), %esi
leaq 0xa7c1(%rip), %rdi      # 0xc1b6
movb $0x0, %al
callq 0x1060
movl -0x91c(%rbp), %edi
movq -0x908(%rbp), %rsi
movslq -0x928(%rbp), %rdx
callq 0x1050
movl %eax, -0x920(%rbp)
movl -0x920(%rbp), %esi
leaq 0xa79c(%rip), %rdi      # 0xc1c4
movb $0x0, %al
callq 0x1060
movl -0x918(%rbp), %edi
callq 0x10a0
movl -0x91c(%rbp), %edi
callq 0x10a0
xorl %eax, %eax
addq $0x930, %rsp            # imm = 0x930
popq %rbp
retq