// File: /workspace/github_c_makefile_star2_O3/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 $0x258, %rsp            # imm = 0x258
movq %rsi, %r14
cmpl $0x1, %edi
jg 0x12d5
movq (%r14), %rsi
leaq 0xd86(%rip), %rdi       # 0x2052
xorl %eax, %eax
callq 0x1070
jmp 0x12f5
movq 0x8(%r14), %rbx
movq %rbx, %rdi
callq 0x1060
cmpq $0x200, %rax            # imm = 0x200
jb 0x130c
leaq 0xea9(%rip), %rdi       # 0x2199
callq 0x1030
movl $0x1, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd62(%rip), %rsi       # 0x2075
movq %rbx, %rdi
callq 0x10b0
testq %rax, %rax
je 0x1489
movq %rax, %r12
movq %r14, 0x8(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10a0
movq %r12, %rdi
callq 0x1090
movq %rax, %r14
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
leaq 0x50(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x1080
leaq 0x30(%rsp), %rbx
movl $0x20, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, (%rsp)
movq %r12, %rcx
callq 0x1040
movl 0x18(%rbx), %eax
movl %eax, %r11d
shll $0xb, %r11d
movl %r14d, %r10d
subl %r11d, %r10d
testl %eax, %eax
cmovel %eax, %r10d
movl 0x10(%rbx), %eax
movl %eax, %r9d
shll $0xb, %r9d
movl %r14d, %r15d
subl %r9d, %r15d
subl %r10d, %r15d
testl %eax, %eax
cmovel %eax, %r15d
movl (%rbx), %r13d
movl 0x8(%rbx), %eax
leal (%r15,%r10), %r12d
movl %eax, %ebx
shll $0xb, %ebx
movl %r14d, %ebp
subl %ebx, %ebp
subl %r12d, %ebp
testl %eax, %eax
cmovel %eax, %ebp
testl %r13d, %r13d
movq %r10, 0x20(%rsp)
movq %r11, 0x10(%rsp)
movq %r15, 0x28(%rsp)
movq %r9, 0x18(%rsp)
je 0x1499
shll $0xb, %r13d
addl %r13d, %r12d
addl %ebp, %r12d
subq $0x8, %rsp
leaq 0xc90(%rip), %rdi       # 0x208e
movl %r14d, %edx
subl %r12d, %edx
movl %r13d, %esi
movl %ebx, %ecx
movl %ebp, %r8d
xorl %eax, %eax
pushq %r10
pushq %r11
pushq %r15
callq 0x1070
addq $0x20, %rsp
subl %r12d, %r14d
movq (%rsp), %r12
je 0x14c2
movl %r13d, %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x10a0
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdx
leaq 0xce9(%rip), %rsi       # 0x2130
leaq 0x50(%rsp), %r13
movq %r13, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0xce9(%rip), %rsi       # 0x2146
movq %r13, %rdi
callq 0x10b0
testq %rax, %rax
je 0x15fe
movq %rax, %r13
movq %r12, %rdi
movl %r14d, %esi
movq %rax, %rdx
callq 0x11ec
movq %r13, %rdi
callq 0x1050
jmp 0x14c2
movq 0x8(%r14), %rsi
leaq 0xbe4(%rip), %rdi       # 0x2078
jmp 0x12cc
subq $0x8, %rsp
leaq 0xbea(%rip), %rdi       # 0x208e
xorl %esi, %esi
xorl %edx, %edx
movl %ebx, %ecx
movl %ebp, %r8d
xorl %eax, %eax
pushq %r10
pushq %r11
pushq %r15
callq 0x1070
addq $0x20, %rsp
movq (%rsp), %r12
testl %ebp, %ebp
movq 0x8(%rsp), %r14
je 0x1521
movl %ebx, %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x10a0
movq 0x8(%r14), %rdx
leaq 0xc67(%rip), %rsi       # 0x2149
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0xc4e(%rip), %rsi       # 0x2146
movq %rbx, %rdi
callq 0x10b0
testq %rax, %rax
je 0x15fe
movq %rax, %rbx
movq %r12, %rdi
movl %ebp, %esi
movq %rax, %rdx
callq 0x11ec
movq %rbx, %rdi
callq 0x1050
movq 0x28(%rsp), %r15
testl %r15d, %r15d
je 0x1584
movl 0x18(%rsp), %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x10a0
movq 0x8(%r14), %rdx
leaq 0xc1b(%rip), %rsi       # 0x215f
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0xbec(%rip), %rsi       # 0x2146
movq %rbx, %rdi
callq 0x10b0
testq %rax, %rax
je 0x15fe
movq %rax, %rbx
movq %r12, %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x11ec
movq %rbx, %rdi
callq 0x1050
movq 0x20(%rsp), %r15
testl %r15d, %r15d
je 0x15e3
movl 0x10(%rsp), %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x10a0
movq 0x8(%r14), %rdx
leaq 0xbd2(%rip), %rsi       # 0x2179
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10d0
leaq 0xb89(%rip), %rsi       # 0x2146
movq %rbx, %rdi
callq 0x10b0
testq %rax, %rax
je 0x15fe
movq %rax, %rbx
movq %r12, %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x11ec
movq %rbx, %rdi
callq 0x1050
movq %r12, %rdi
callq 0x1050
leaq 0xba1(%rip), %rdi       # 0x2193
callq 0x1030
xorl %eax, %eax
jmp 0x12fa
leaq 0xa73(%rip), %rdi       # 0x2078
leaq 0x50(%rsp), %rsi
jmp 0x12cc