// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-mgs/dat-merge.c
// Function: main
int main(int argc, char** argv) {
	FILE *f = NULL;
	FILE *o = NULL;
	
	uint32_t numvfsfiles;
	vfsfile* vfsfiles;
	
	unsigned int i;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s dat-file\n", argv[0]);
		return 1;
	}
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fread(&numvfsfiles, 4, 1, f);
	vfsfiles = malloc(sizeof(vfsfile)*numvfsfiles);
	fread(vfsfiles, sizeof(vfsfile)*numvfsfiles, 1, f);
	
	for(i = 0; i < numvfsfiles; i++) {
		printf("File %s offset %08x size %08x sectors\n", vfsfiles[i].name, vfsfiles[i].offset, vfsfiles[i].size);
		fseek(f, vfsfiles[i].offset*0x800, SEEK_SET);
		if( !(o = fopen(vfsfiles[i].name, "wb" ))) {
			printf("Couldnt open files %s\n", vfsfiles[i].name);
			return 1;
		}
		writeFile(f, vfsfiles[i].size*0x800, o);
		fclose(o);
	}
	
	free(vfsfiles);
	fclose(f);
	
	printf("Done.\n");
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x134d
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xd18(%rip), %rdi       # 0x2052
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x14e9
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd19(%rip), %rsi       # 0x2075
callq 0x1090
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x138d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xcfe(%rip), %rdi       # 0x2078
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x14e9
movq -0x18(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1040
movl -0x24(%rbp), %eax
imulq $0x18, %rax, %rdi
callq 0x1070
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movl -0x24(%rbp), %eax
imulq $0x18, %rax, %rsi
movq -0x18(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x14c2
movq -0x30(%rbp), %rsi
movl -0x34(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rsi
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %edx
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %ecx
leaq 0xc77(%rip), %rdi       # 0x208e
movb $0x0, %al
callq 0x1060
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
shll $0xb, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1080
movq -0x30(%rbp), %rdi
movl -0x34(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xc5f(%rip), %rsi       # 0x20b5
callq 0x1090
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x148a
movq -0x30(%rbp), %rsi
movl -0x34(%rbp), %eax
imulq $0x18, %rax, %rax
addq %rax, %rsi
leaq 0xc3e(%rip), %rdi       # 0x20b8
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x14e9
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %esi
shll $0xb, %esi
movq -0x20(%rbp), %rdx
callq 0x11c0
movq -0x20(%rbp), %rdi
callq 0x1050
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x13d4
movq -0x30(%rbp), %rdi
callq 0x1030
movq -0x18(%rbp), %rdi
callq 0x1050
leaq 0xbf4(%rip), %rdi       # 0x20cf
movb $0x0, %al
callq 0x1060
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq