// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-zoe/sdx-extract.c
// Function: main
int main( int argc, char **argv ) {
	
	sdxHeader header;
	unsigned int fileSize = 0, bgmSize = 0, sfxSize = 0, seqSize1 = 0, seqSize2 = 0;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s SDX-file\n", argv[0]);
		return 1;
	}
	
	if( strlen( argv[1] ) > 511 ) {
		printf("Input filename too long, please rename it to be less then 511 chars!\n");
		return 1;
	}
	
	FILE *f, *o;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek( f, 0, SEEK_END );
	fileSize = ftell(f);
	fseek( f, 0, SEEK_SET );
	
	char filename[512] = {0};
	
	fread( &header, sizeof(header), 1, f );
	
	if( header.offsetSequenceMdx ) seqSize2 = fileSize - header.offsetSequenceMdx*BLOCKSIZE;
	if( header.offsetSequenceEfx ) seqSize1 = fileSize - seqSize2 - header.offsetSequenceEfx*BLOCKSIZE;
	if( header.offsetSfxWvx ) sfxSize  = fileSize - seqSize2 - seqSize1 - header.offsetSfxWvx*BLOCKSIZE;
	if( header.offsetBgmWvx ) bgmSize  = fileSize - seqSize2 - seqSize1 - sfxSize - header.offsetBgmWvx*BLOCKSIZE;
	printf("Music samples offset: %08x - Size %08x\nEffect samples offset: %08x - Size %08x\nSequence data 1 offset: %08x - Size %08x\nSequence data 2 offset: %08x - Size %08x\n", header.offsetBgmWvx*BLOCKSIZE, bgmSize, header.offsetSfxWvx*BLOCKSIZE, sfxSize, header.offsetSequenceEfx*BLOCKSIZE, seqSize1, header.offsetSequenceMdx*BLOCKSIZE, seqSize2 );
	
	if( bgmSize ) {
		fseek( f, (header.offsetBgmWvx * BLOCKSIZE), SEEK_SET );
		sprintf( filename, "%s-BGMsampleBlock.wvx", argv[1] );
		if( !(o = fopen( filename, "wb" ))) {
			printf("Couldnt open file %s\n", filename);
			return 1;
		}
		writeFile( f, bgmSize, o );
		fclose(o);
	}
	
	if( sfxSize ) {
		fseek( f, header.offsetSfxWvx*BLOCKSIZE, SEEK_SET );
		sprintf( filename, "%s-SFXsampleBlock.wvx", argv[1] );
		if( !(o = fopen( filename, "wb" ))) {
			printf("Couldnt open file %s\n", filename);
			return 1;
		}
		writeFile( f, sfxSize, o );
		fclose(o);
	}
	
	if( seqSize1 ) {
		fseek( f, header.offsetSequenceEfx*BLOCKSIZE, SEEK_SET );
		sprintf( filename, "%s-SequenceDataBlock1.efx", argv[1] );
		if( !(o = fopen( filename, "wb" ))) {
			printf("Couldnt open file %s\n", filename);
			return 1;
		}
		writeFile( f, seqSize1, o );
		fclose(o);
	}
	
	if( seqSize2 ) {
		fseek( f, header.offsetSequenceMdx*BLOCKSIZE, SEEK_SET );
		sprintf( filename, "%s-SequenceDataBlock2.mdx", argv[1] );
		if( !(o = fopen( filename, "wb" ))) {
			printf("Couldnt open file %s\n", filename);
			return 1;
		}
		writeFile( f, seqSize2, o );
		fclose(o);
	}
	
	fclose(f);
	printf("Done.\n");
	return 0;
	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rsi, %r14
cmpl $0x1, %edi
jg 0x12cb
movq (%r14), %rsi
leaq 0xd90(%rip), %rdi       # 0x2052
xorl %eax, %eax
callq 0x1070
jmp 0x12eb
movq 0x8(%r14), %rbx
movq %rbx, %rdi
callq 0x1060
cmpq $0x200, %rax            # imm = 0x200
jb 0x1300
leaq 0xeb3(%rip), %rdi       # 0x2199
callq 0x1030
pushq $0x1
popq %rax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd6e(%rip), %rsi       # 0x2075
movq %rbx, %rdi
callq 0x10b0
testq %rax, %rax
je 0x15a5
movq %rax, %r15
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r15, %rdi
callq 0x1090
movq %rax, %rbp
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
leaq 0x40(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x1080
leaq 0x20(%rsp), %rbx
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1040
movl 0x18(%rbx), %eax
movl %eax, %r11d
shll $0xb, %r11d
movl %ebp, %r10d
subl %r11d, %r10d
testl %eax, %eax
cmovel %eax, %r10d
movl 0x10(%rbx), %eax
movl %eax, %r9d
shll $0xb, %r9d
movl %ebp, %r12d
subl %r9d, %r12d
subl %r10d, %r12d
testl %eax, %eax
cmovel %eax, %r12d
movl (%rbx), %eax
movl 0x8(%rbx), %ecx
leal (%r12,%r10), %edx
movl %ecx, %ebx
shll $0xb, %ebx
movl %ebp, %r13d
subl %ebx, %r13d
subl %edx, %r13d
testl %ecx, %ecx
cmovel %ecx, %r13d
movq %r15, (%rsp)
movl %eax, %r15d
shll $0xb, %r15d
addl %r15d, %edx
addl %r13d, %edx
subl %edx, %ebp
testl %eax, %eax
cmovel %eax, %ebp
subq $0x8, %rsp
leaq 0xcb9(%rip), %rdi       # 0x208e
movl %r15d, %esi
movl %ebp, %edx
movl %ebx, %ecx
movl %r13d, %r8d
movq %r9, 0x18(%rsp)
xorl %eax, %eax
movq %r10, 0x20(%rsp)
pushq %r10
movq %r11, 0x18(%rsp)
pushq %r11
pushq %r12
callq 0x1070
addq $0x20, %rsp
testl %ebp, %ebp
je 0x145c
movl %r15d, %esi
movq (%rsp), %rdi
xorl %edx, %edx
callq 0x10a0
movq 0x8(%r14), %rdx
leaq 0xd14(%rip), %rsi       # 0x2130
leaq 0x40(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0xd14(%rip), %rsi       # 0x2146
movq %r15, %rdi
callq 0x10b0
testq %rax, %rax
je 0x1594
movq %rax, %r15
movq (%rsp), %rdi
movl %ebp, %esi
movq %rax, %rdx
callq 0x11ec
movq %r15, %rdi
callq 0x1050
testl %r13d, %r13d
movq (%rsp), %r15
je 0x14bc
movl %ebx, %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x10a0
movq 0x8(%r14), %rdx
leaq 0xccd(%rip), %rsi       # 0x2149
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0xcb4(%rip), %rsi       # 0x2146
movq %rbx, %rdi
callq 0x10b0
testq %rax, %rax
je 0x1594
movq %rax, %rbx
movq %r15, %rdi
movl %r13d, %esi
movq %rax, %rdx
callq 0x11ec
movq %rbx, %rdi
callq 0x1050
testl %r12d, %r12d
je 0x151a
movl 0x10(%rsp), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x10a0
movq 0x8(%r14), %rdx
leaq 0xc85(%rip), %rsi       # 0x215f
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0xc56(%rip), %rsi       # 0x2146
movq %rbx, %rdi
callq 0x10b0
testq %rax, %rax
je 0x1594
movq %rax, %rbx
movq %r15, %rdi
movl %r12d, %esi
movq %rax, %rdx
callq 0x11ec
movq %rbx, %rdi
callq 0x1050
movq 0x18(%rsp), %r12
testl %r12d, %r12d
je 0x1579
movl 0x8(%rsp), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x10a0
movq 0x8(%r14), %rdx
leaq 0xc3c(%rip), %rsi       # 0x2179
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0xbf3(%rip), %rsi       # 0x2146
movq %rbx, %rdi
callq 0x10b0
testq %rax, %rax
je 0x1594
movq %rax, %rbx
movq %r15, %rdi
movl %r12d, %esi
movq %rax, %rdx
callq 0x11ec
movq %rbx, %rdi
callq 0x1050
movq %r15, %rdi
callq 0x1050
leaq 0xc0b(%rip), %rdi       # 0x2193
callq 0x1030
xorl %eax, %eax
jmp 0x12ee
leaq 0xadd(%rip), %rdi       # 0x2078
leaq 0x40(%rsp), %rsi
jmp 0x12c2
movq 0x8(%r14), %rsi
leaq 0xac8(%rip), %rdi       # 0x2078
jmp 0x12c2