// File: /workspace/github_c_makefile_star2_O0/evilwombat[P]gopro-fw-tools/goprom.c
// Function: main
int main(int argc, char **argv)
{
	int		i, nfiles, update = 0;
	struct inode	d;
	FILE		*fd;

	if (argc != 3) {
		print_usage();
		exit(-1);
	}

	if (strcmp(argv[1], "--unpack") == 0) {
		fprintf(stderr, "Generating unpack script\n");
		update = 0;
	} else if (strcmp(argv[1], "--update") == 0) {
		fprintf(stderr, "Generating update script\n");
		fprintf(stderr, "You really better know what you are doing.\n");
		fprintf(stderr, "You can very easily destroy your camera if you misuse this!\n");
		update = 1;
	} else {
		print_usage();
		exit(-1);
	}
	
	fd = fopen(argv[2], "rb");

	if (!fd) {
		fprintf(stderr, "Could not open %s\n", argv[2]);
		exit(-1);
	}
	
	nfiles = read_word(fd);
	fprintf(stderr, "I see %d files\n", nfiles);

	if (nfiles < 0) {
		fprintf(stderr, "... and that certainly does not seem right\n");
		exit(-1);
	}
	
	fseek(fd, 0x800, SEEK_SET);
	
	for (i = 0; i < nfiles; i++) {
		fread(&d, sizeof(struct inode), 1, fd);

		if (d.magic != INODE_MAGIC) {
			fprintf(stderr, "Unknown inode magic: %08x\n", d.magic);
			exit(-1);
		}

		if (!update) {
			printf("mkdir -p `dirname %s`\n", d.name);
			printf("dd if=$1 bs=%d skip=1 | dd iflag=fullblock of=%s bs=%d count=1\n",
				d.offset, d.name, d.len);
		} else
			printf("dd if=%s of=$1 bs=1 seek=%d count=%d conv=notrunc\n",
				d.name, d.offset, d.len);
	}

	fclose(fd);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x11f5
callq 0x1440
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x10b0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xe00(%rip), %rsi       # 0x2004
callq 0x1070
cmpl $0x0, %eax
jne 0x122f
movq 0x2de3(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdee(%rip), %rsi       # 0x200d
movb $0x0, %al
callq 0x1080
movl $0x0, -0x1c(%rbp)
jmp 0x12aa
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xde9(%rip), %rsi       # 0x2027
callq 0x1070
cmpl $0x0, %eax
jne 0x1299
movq 0x2da9(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdd7(%rip), %rsi       # 0x2030
movb $0x0, %al
callq 0x1080
movq 0x2d91(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdd9(%rip), %rsi       # 0x204a
movb $0x0, %al
callq 0x1080
movq 0x2d79(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xded(%rip), %rsi       # 0x2076
movb $0x0, %al
callq 0x1080
movl $0x1, -0x1c(%rbp)
jmp 0x12a8
callq 0x1440
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x10b0
jmp 0x12aa
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0xdfa(%rip), %rsi       # 0x20b3
callq 0x10a0
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jne 0x12f9
movq 0x2d22(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0xdce(%rip), %rsi       # 0x20b6
movb $0x0, %al
callq 0x1080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x10b0
movq -0xa8(%rbp), %rdi
callq 0x14f0
movl %eax, -0x18(%rbp)
movq 0x2ce9(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x18(%rbp), %edx
leaq 0xdad(%rip), %rsi       # 0x20c9
movb $0x0, %al
callq 0x1080
cmpl $0x0, -0x18(%rbp)
jge 0x134b
movq 0x2cc8(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd9f(%rip), %rsi       # 0x20d9
movb $0x0, %al
callq 0x1080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x10b0
movq -0xa8(%rbp), %rdi
movl $0x800, %esi            # imm = 0x800
xorl %edx, %edx
callq 0x1090
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x141f
movq -0xa8(%rbp), %rcx
leaq -0x9c(%rbp), %rdi
movl $0x80, %esi
movl $0x1, %edx
callq 0x1030
cmpl $0x2387ab76, -0x20(%rbp) # imm = 0x2387AB76
je 0x13bc
movq 0x2c5a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x20(%rbp), %edx
leaq 0xd5a(%rip), %rsi       # 0x2105
movb $0x0, %al
callq 0x1080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x10b0
cmpl $0x0, -0x1c(%rbp)
jne 0x13f4
leaq -0x9c(%rbp), %rsi
leaq 0xd50(%rip), %rdi       # 0x2120
movb $0x0, %al
callq 0x1050
movl -0x28(%rbp), %esi
leaq -0x9c(%rbp), %rdx
movl -0x24(%rbp), %ecx
leaq 0xd4c(%rip), %rdi       # 0x2137
movb $0x0, %al
callq 0x1050
jmp 0x140f
leaq -0x9c(%rbp), %rsi
movl -0x28(%rbp), %edx
movl -0x24(%rbp), %ecx
leaq 0xd6f(%rip), %rdi       # 0x2177
movb $0x0, %al
callq 0x1050
jmp 0x1411
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1365
movq -0xa8(%rbp), %rdi
callq 0x1040
xorl %eax, %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)