// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1383
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xce2(%rip), %rdi       # 0x2052
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1757
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1050
cmpq $0x1ff, %rax            # imm = 0x1FF
jbe 0x13b2
leaq 0xcd6(%rip), %rdi       # 0x2075
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1757
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xcfa(%rip), %rsi       # 0x20bb
callq 0x10a0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x13f2
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xcdf(%rip), %rdi       # 0x20be
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1757
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1090
movq -0x50(%rbp), %rdi
callq 0x1080
movl %eax, -0x34(%rbp)
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
leaq -0x260(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1070
movq -0x50(%rbp), %rcx
leaq -0x30(%rbp), %rdi
movl $0x20, %esi
movl $0x1, %edx
callq 0x1030
cmpl $0x0, -0x18(%rbp)
je 0x145d
movl -0x34(%rbp), %eax
movl -0x18(%rbp), %ecx
shll $0xb, %ecx
subl %ecx, %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x1474
movl -0x34(%rbp), %eax
subl -0x44(%rbp), %eax
movl -0x20(%rbp), %ecx
shll $0xb, %ecx
subl %ecx, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x148e
movl -0x34(%rbp), %eax
subl -0x44(%rbp), %eax
subl -0x40(%rbp), %eax
movl -0x28(%rbp), %ecx
shll $0xb, %ecx
subl %ecx, %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x14ab
movl -0x34(%rbp), %eax
subl -0x44(%rbp), %eax
subl -0x40(%rbp), %eax
subl -0x3c(%rbp), %eax
movl -0x30(%rbp), %ecx
shll $0xb, %ecx
subl %ecx, %eax
movl %eax, -0x38(%rbp)
movl -0x30(%rbp), %esi
shll $0xb, %esi
movl -0x38(%rbp), %edx
movl -0x28(%rbp), %ecx
shll $0xb, %ecx
movl -0x3c(%rbp), %r8d
movl -0x20(%rbp), %r9d
shll $0xb, %r9d
movl -0x40(%rbp), %r11d
movl -0x18(%rbp), %r10d
shll $0xb, %r10d
movl -0x44(%rbp), %eax
leaq 0xbf8(%rip), %rdi       # 0x20d4
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x1060
cmpl $0x0, -0x38(%rbp)
je 0x1583
movq -0x50(%rbp), %rdi
movl -0x30(%rbp), %eax
shll $0xb, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
leaq -0x260(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xc51(%rip), %rsi       # 0x2176
movb $0x0, %al
callq 0x10c0
leaq -0x260(%rbp), %rdi
leaq 0xc52(%rip), %rsi       # 0x218c
callq 0x10a0
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x156a
leaq -0x260(%rbp), %rsi
leaq 0xb67(%rip), %rdi       # 0x20be
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1757
movq -0x50(%rbp), %rdi
movl -0x38(%rbp), %esi
movq -0x58(%rbp), %rdx
callq 0x11e0
movq -0x58(%rbp), %rdi
callq 0x1040
cmpl $0x0, -0x3c(%rbp)
je 0x1616
movq -0x50(%rbp), %rdi
movl -0x28(%rbp), %eax
shll $0xb, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
leaq -0x260(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xbd7(%rip), %rsi       # 0x218f
movb $0x0, %al
callq 0x10c0
leaq -0x260(%rbp), %rdi
leaq 0xbbf(%rip), %rsi       # 0x218c
callq 0x10a0
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x15fd
leaq -0x260(%rbp), %rsi
leaq 0xad4(%rip), %rdi       # 0x20be
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1757
movq -0x50(%rbp), %rdi
movl -0x3c(%rbp), %esi
movq -0x58(%rbp), %rdx
callq 0x11e0
movq -0x58(%rbp), %rdi
callq 0x1040
cmpl $0x0, -0x40(%rbp)
je 0x16a9
movq -0x50(%rbp), %rdi
movl -0x20(%rbp), %eax
shll $0xb, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
leaq -0x260(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xb5a(%rip), %rsi       # 0x21a5
movb $0x0, %al
callq 0x10c0
leaq -0x260(%rbp), %rdi
leaq 0xb2c(%rip), %rsi       # 0x218c
callq 0x10a0
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x1690
leaq -0x260(%rbp), %rsi
leaq 0xa41(%rip), %rdi       # 0x20be
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1757
movq -0x50(%rbp), %rdi
movl -0x40(%rbp), %esi
movq -0x58(%rbp), %rdx
callq 0x11e0
movq -0x58(%rbp), %rdi
callq 0x1040
cmpl $0x0, -0x44(%rbp)
je 0x1739
movq -0x50(%rbp), %rdi
movl -0x18(%rbp), %eax
shll $0xb, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
leaq -0x260(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xae1(%rip), %rsi       # 0x21bf
movb $0x0, %al
callq 0x10c0
leaq -0x260(%rbp), %rdi
leaq 0xa99(%rip), %rsi       # 0x218c
callq 0x10a0
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x1720
leaq -0x260(%rbp), %rsi
leaq 0x9ae(%rip), %rdi       # 0x20be
movb $0x0, %al
callq 0x1060
movl $0x1, -0x4(%rbp)
jmp 0x1757
movq -0x50(%rbp), %rdi
movl -0x44(%rbp), %esi
movq -0x58(%rbp), %rdx
callq 0x11e0
movq -0x58(%rbp), %rdi
callq 0x1040
movq -0x50(%rbp), %rdi
callq 0x1040
leaq 0xa90(%rip), %rdi       # 0x21d9
movb $0x0, %al
callq 0x1060
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq