// File: /workspace/github_c_makefile_star2_O0/Mikejmoffitt[P]romtools/bscoop/bscoop.c
// Function: main
int main(int argc, char **argv)
{
	if (argc < 5)
	{
		printf("usage: %s in_fname offset bytes out_fname\n", argv[0]);
		return 0;
	}

	const char *in_fname_arg = argv[1];
	const char *offset_arg = argv[2];
	const char *bytes_arg = argv[3];
	const char *out_fname_arg = argv[4];
	const size_t offset = strtoul(offset_arg, NULL, 0);
	const size_t bytes = strtoul(bytes_arg, NULL, 0);
	if (bytes == 0) return 0;

	FILE *fi = fopen(in_fname_arg, "rb");
	if (!fi)
	{
		fprintf(stderr, "Couldn't open %s for reading.\n", in_fname_arg);
		return 1;
	}

	FILE *fo = fopen(out_fname_arg, "wb");
	if (!fo)
	{
		fprintf(stderr, "Couldn't open %s for writing.\n", out_fname_arg);
		fclose(fi);
		return 1;
	}

	fseek(fi, offset, SEEK_SET);
	for (size_t i = 0; i < bytes; i++)
	{
		if (feof(fi)) break;
		fputc(fgetc(fi), fo);
	}

	const size_t bytes_read = ftell(fo);

	if (bytes_read != bytes)
	{
		fprintf(stderr, "Warning: Actual bytes read does not match expectation"
		        "(actual: $%X expected $%x)\n",
		        (int)bytes_read, (int)bytes);
	}
	else
	{
		printf("Copied %d ($%X) bytes to %s.\n",
		       (int)bytes_read, (int)bytes_read, out_fname_arg);
	}

	fclose(fi);
	fclose(fo);

	return 0;
}

// Assembly:
subq $0x68, %rsp
movl $0x0, 0x64(%rsp)
movl %edi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
cmpl $0x5, 0x60(%rsp)
jge 0x120f
movq 0x58(%rsp), %rax
movq (%rax), %rsi
leaq 0xe09(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1040
movl $0x0, 0x64(%rsp)
jmp 0x13fd
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x58(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x58(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x48(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10c0
movq %rax, 0x30(%rsp)
movq 0x40(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10c0
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x1286
movl $0x0, 0x64(%rsp)
jmp 0x13fd
movq 0x50(%rsp), %rdi
leaq 0xd9d(%rip), %rsi       # 0x202f
callq 0x10b0
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x12ce
movq 0x2d4d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x50(%rsp), %rdx
leaq 0xd78(%rip), %rsi       # 0x2032
movb $0x0, %al
callq 0x1070
movl $0x1, 0x64(%rsp)
jmp 0x13fd
movq 0x38(%rsp), %rdi
leaq 0xd77(%rip), %rsi       # 0x2051
callq 0x10b0
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x1320
movq 0x2d05(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x38(%rsp), %rdx
leaq 0xd52(%rip), %rsi       # 0x2054
movb $0x0, %al
callq 0x1070
movq 0x20(%rsp), %rdi
callq 0x1030
movl $0x1, 0x64(%rsp)
jmp 0x13fd
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x10a0
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpq 0x28(%rsp), %rax
jae 0x137d
movq 0x20(%rsp), %rdi
callq 0x1090
cmpl $0x0, %eax
je 0x1357
jmp 0x137d
movq 0x20(%rsp), %rdi
callq 0x1050
movl %eax, %edi
movq 0x18(%rsp), %rsi
callq 0x1060
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x133a
movq 0x18(%rsp), %rdi
callq 0x1080
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
cmpq 0x28(%rsp), %rax
je 0x13c0
movq 0x2c59(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x8(%rsp), %rax
movl %eax, %edx
movq 0x28(%rsp), %rax
movl %eax, %ecx
leaq 0xcbc(%rip), %rsi       # 0x2073
movb $0x0, %al
callq 0x1070
jmp 0x13e1
movq 0x8(%rsp), %rax
movl %eax, %esi
movq 0x8(%rsp), %rax
movl %eax, %edx
movq 0x38(%rsp), %rcx
leaq 0xcea(%rip), %rdi       # 0x20c4
movb $0x0, %al
callq 0x1040
movq 0x20(%rsp), %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq