// File: /workspace/github_c_makefile_star2_O1/albinoloverats[P]hide/src/hide.c
// Function: main
int main(int argc, char **argv)
{
	if (argc < 2 || argc > 5)
	{
		fprintf(stderr, "Usage: %s [-f] <source image> <file to hide> <output image>\n", argv[0]);
		fprintf(stderr, "       %s [-f] <image> <recovered file>\n", argv[0]);
		fprintf(stderr, "       %s <image>\n", argv[0]);
		find_supported_formats(DIR_LIBRARY, NULL);
		return EXIT_FAILURE;
	}
	else if (argc == 2)
	{
		struct stat s;
		if (stat(argv[1], &s) < 0 && errno == ENOENT)
		{
			fprintf(stderr, "Could not read file %s\n", argv[1]);
			return errno;
		}
		image_info_t image_info = { argv[1], NULL, NULL, NULL, NULL, 0, 0, 0, NULL, NULL };
#ifndef __DEBUG_JPEG__
		void *so = find_supported_formats(DIR_LIBRARY, &image_info);
		if (!so)
			return errno;
#else
		extern uint64_t info_jpeg(image_info_t *image_info);
		extern void free_jpeg(image_info_t image_info);
		image_info.info = info_jpeg;
		image_info.free = free_jpeg;
#endif
		if (!image_info.info)
		{
			fprintf(stderr, "Unsupported image format\n");
			find_supported_formats(DIR_LIBRARY, NULL);
			errno = EFTYPE;
		}
		else
		{
			setlocale(LC_NUMERIC, "");
			printf("File capacity: %'" PRIu64 " bytes\n", image_info.info(&image_info));
			image_info.free(image_info);
			errno = EXIT_SUCCESS;
		}
#ifndef __DEBUG_JPEG__
		dlclose(so);
#endif
		return errno;
	}

	int o = strcmp(argv[1], "-f") ? 0 : 1;

	hide_files_t files = { argv[o + 1], argv[o + 2], argv[o + 3] };
	process_options_t options = { files, o };

#ifndef __DEBUG__
	{
		cli_status_e ui_status = CLI_INIT;
		cli_progress_s ui_current = { 0, 1, NULL }; /* updated after reading image */
		cli_progress_s ui_total   = { 0, 3, NULL }; /* maximum of 3 steps (read, update, write) */
		ui.status = &ui_status;
		ui.current = &ui_current;
		ui.total = &ui_total;
	}
	/*
	 * TODO start process() in own thread, then call cli_display()
	 *
	 * remember that process() will have to set the status once it's finished
	 */
	pthread_t t;
	pthread_attr_t a;
	pthread_attr_init(&a);
	pthread_attr_setdetachstate(&a, PTHREAD_CREATE_JOINABLE);
	pthread_create(&t, &a, process, &options);
	pthread_attr_destroy(&a);

	cli_display(&ui);

	pthread_join(t, NULL);
#else
	process(&options);
#endif

	return errno;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x160, %rsp            # imm = 0x160
movq %rsi, %rbx
leal -0x6(%rdi), %eax
cmpl $-0x5, %eax
jbe 0x359a
cmpl $0x2, %edi
jne 0x3490
movq 0x8(%rbx), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x2120
testl %eax, %eax
jns 0x33eb
callq 0x2060
cmpl $0x2, (%rax)
je 0x35b8
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rsi
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
movq 0x8(%rbx), %rax
movq %rax, (%rsi)
leaq 0x1bed(%rip), %rdi      # 0x5004
callq 0x2d4a
testq %rax, %rax
je 0x3583
movq %rax, %rbx
cmpq $0x0, 0x98(%rsp)
je 0x35a9
leaq 0x1e68(%rip), %rsi      # 0x52a6
movl $0x1, %edi
callq 0x22d0
leaq 0x80(%rsp), %r14
movq %r14, %rdi
callq *0x18(%r14)
leaq 0x1cfc(%rip), %rdi      # 0x515a
xorl %ebp, %ebp
movq %rax, %rsi
xorl %eax, %eax
callq 0x2140
movl $0xa, %ecx
movq %rsp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq *0x20(%r14)
callq 0x2060
movl %ebp, (%rax)
movq %rbx, %rdi
callq 0x23b0
jmp 0x3583
movq 0x8(%rbx), %rdi
leaq 0x1cda(%rip), %rsi      # 0x5175
callq 0x21f0
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal (,%rcx,8), %edx
testl %eax, %eax
movq 0x10(%rbx,%rcx,8), %rax
movq 0x8(%rdx,%rbx), %rcx
movq 0x18(%rdx,%rbx), %rdx
leaq 0x80(%rsp), %rbx
movq %rcx, (%rbx)
movq %rax, 0x8(%rbx)
movq %rdx, 0x10(%rbx)
sete 0x18(%rbx)
leaq 0x5c(%rsp), %rax
movl $0x1, (%rax)
movq 0x1d18(%rip), %rcx      # 0x5200
leaq 0xd0(%rsp), %rdx
movq %rcx, 0x10(%rdx)
movups 0x1cf5(%rip), %xmm0     # 0x51f0
movaps %xmm0, (%rdx)
movq 0x1d13(%rip), %rcx      # 0x5218
leaq 0x60(%rsp), %rsi
movq %rcx, 0x10(%rsi)
movups 0x1cf3(%rip), %xmm0     # 0x5208
movaps %xmm0, (%rsi)
movq %rax, 0x4cf9(%rip)      # 0x8218
leaq 0x4cf2(%rip), %r14      # 0x8218
movq %rdx, 0x4cf3(%rip)      # 0x8220
movq %rsi, 0x4cf4(%rip)      # 0x8228
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
callq 0x21d0
movq %r15, %rdi
xorl %esi, %esi
callq 0x2240
leaq -0xab9(%rip), %rdx      # 0x2a9c
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x22f0
movq %r15, %rdi
callq 0x2320
movq %r14, %rdi
callq 0x2538
movq (%r12), %rdi
xorl %esi, %esi
callq 0x2370
callq 0x2060
movl (%rax), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x24d9
movl $0x1, %eax
jmp 0x358a
callq 0x24a9
movl $0x4f, %ebp
jmp 0x347c
movq %rax, %r14
movq 0x4a36(%rip), %rax      # 0x7ff8
movq (%rax), %rdi
movq 0x8(%rbx), %rdx
leaq 0x1b72(%rip), %rsi      # 0x5142
xorl %eax, %eax
callq 0x2220
movl (%r14), %eax
jmp 0x358a