// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-zoe/kojimasound/kojimasound.c
// Function: processWvx
void processWvx( FILE *f, unsigned int baseoffset, char *folder, unsigned int numSamples, unsigned int wvxgame ) {
	FILE *o;
	unsigned int i, offsub, samplerate, vagsize, psx = 0;
	char filename[1024];
	unsigned char vagbuf[0x10];
	sampleTableEntry sampleentry;
	vagHeader vagheader;
	
	switch(wvxgame) {
		case mgs1: {
			psx = 1;
			break;
		}
		case zoe1:
		default: {
			break;
		}
	}
	for( i = 0; i < numSamples; i++ ) {
		fseek(f, baseoffset+0x10+(i*0x10), SEEK_SET);
		
		sprintf(filename, "%s/%08d-%08x.vag", folder, i, i);
		if( !(o = fopen( filename, "wb" ))) {
				printf("Couldnt open file %s\n", filename);
				return;
		}
		
		memset(&sampleentry, 0, 0x10);
		fread(&sampleentry, 0x10, 1, f);
		printf("Sample offset: %08x - Flags %08x\n", sampleentry.offset, sampleentry.flags);
		if( i == 0 ) offsub = sampleentry.offset;
		sampleentry.offset -= offsub;
		if(psx) {
			if(sampleentry.flags > 0x7F000000) samplerate = 22050;
			else samplerate = 11025;
		}
		else {
			if(sampleentry.flags > 0x7F000000) samplerate = 44100;
			else samplerate = 32000;
		}
		fseek(f, baseoffset+0x20+(numSamples*0x10)+sampleentry.offset, SEEK_SET);
		
		memset(&vagheader, 0, 0x30);
		fwrite(&vagheader, 0x30, 1, o);
		vagsize = 0x30;
		memset(vagbuf, 0xFF, 0x10);
		while(1) {
			if(!fread(vagbuf, 0x10, 1, f)) break;
			if(!memcmp(vagbuf, vagEnd, 0x10) && (vagsize!=0x30)) break;
			fwrite(vagbuf, 0x10, 1, o);
			vagsize+=0x10;
		}
		vagPrepareHeader(&vagheader, vagsize, samplerate);
		fseek(o, 0, SEEK_SET);
		fwrite(&vagheader, 0x30, 1, o);
		fclose(o);
	}
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4a0, %rsp            # imm = 0x4A0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x494(%rbp)
testl %eax, %eax
je 0x1320
jmp 0x1313
movl -0x494(%rbp), %eax
subl $0x1, %eax
je 0x1329
jmp 0x132b
movl $0x1, -0x3c(%rbp)
jmp 0x132d
jmp 0x132b
jmp 0x132d
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x1594
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %eax
addl $0x10, %eax
movl -0x2c(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
leaq -0x440(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl -0x2c(%rbp), %ecx
movl -0x2c(%rbp), %r8d
leaq 0xc93(%rip), %rsi       # 0x2009
movb $0x0, %al
callq 0x10c0
leaq -0x440(%rbp), %rdi
leaq 0xc8f(%rip), %rsi       # 0x201a
callq 0x10a0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x13b4
leaq -0x440(%rbp), %rsi
leaq 0xc75(%rip), %rdi       # 0x201d
movb $0x0, %al
callq 0x1070
jmp 0x1596
leaq -0x460(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1080
movq -0x8(%rbp), %rcx
leaq -0x460(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
callq 0x1050
movl -0x460(%rbp), %esi
movl -0x45c(%rbp), %edx
leaq 0xc3f(%rip), %rdi       # 0x2033
movb $0x0, %al
callq 0x1070
cmpl $0x0, -0x2c(%rbp)
jne 0x140a
movl -0x460(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %ecx
movl -0x460(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x460(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x143f
cmpl $0x7f000000, -0x45c(%rbp) # imm = 0x7F000000
jbe 0x1436
movl $0x5622, -0x34(%rbp)    # imm = 0x5622
jmp 0x143d
movl $0x2b11, -0x34(%rbp)    # imm = 0x2B11
jmp 0x145d
cmpl $0x7f000000, -0x45c(%rbp) # imm = 0x7F000000
jbe 0x1454
movl $0xac44, -0x34(%rbp)    # imm = 0xAC44
jmp 0x145b
movl $0x7d00, -0x34(%rbp)    # imm = 0x7D00
jmp 0x145d
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %eax
addl $0x20, %eax
movl -0x1c(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
addl -0x460(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
leaq -0x490(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x1080
movq -0x28(%rbp), %rcx
leaq -0x490(%rbp), %rdi
movl $0x30, %esi
movl $0x1, %edx
callq 0x10d0
movl $0x30, -0x38(%rbp)
leaq -0x450(%rbp), %rdi
movl $0xff, %esi
movl $0x10, %edx
callq 0x1080
leaq -0x450(%rbp), %rdi
movq -0x8(%rbp), %rcx
movl $0x10, %esi
movl $0x1, %edx
callq 0x1050
cmpq $0x0, %rax
jne 0x14ec
jmp 0x1542
leaq -0x450(%rbp), %rax
movups (%rax), %xmm0
movups 0x2b93(%rip), %xmm1     # 0x4090
pcmpeqb %xmm1, %xmm0
pmovmskb %xmm0, %eax
subl $0xffff, %eax           # imm = 0xFFFF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x151d
cmpl $0x30, -0x38(%rbp)
je 0x151d
jmp 0x1542
leaq -0x450(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x10, %esi
movl $0x1, %edx
callq 0x10d0
movl -0x38(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x38(%rbp)
jmp 0x14ca
movl -0x38(%rbp), %esi
movl -0x34(%rbp), %edx
leaq -0x490(%rbp), %rdi
callq 0x11e0
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
movq -0x28(%rbp), %rcx
leaq -0x490(%rbp), %rdi
movl $0x30, %esi
movl $0x1, %edx
callq 0x10d0
movq -0x28(%rbp), %rdi
callq 0x1060
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1334
jmp 0x1596
addq $0x4a0, %rsp            # imm = 0x4A0
popq %rbp
retq
nop