// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x498, %rsp            # imm = 0x498
movl %r8d, 0x10(%rsp)
movq %rdx, 0x68(%rsp)
movq %rdi, %r15
movl %ecx, %eax
movq %rax, 0x70(%rsp)
shll $0x4, %ecx
leal (%rsi,%rcx), %eax
addl $0x20, %eax
movl %eax, 0xc(%rsp)
movl %esi, %eax
addl $0x10, %eax
movl %eax, 0x14(%rsp)
pushq $0x10
popq %r12
pushq $0x1
popq %rbp
xorl %r13d, %r13d
cmpq 0x70(%rsp), %r13
je 0x1489
movl %r13d, %esi
shll $0x4, %esi
addl 0x14(%rsp), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x1090
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
leaq 0xd9c(%rip), %rsi       # 0x2030
movq 0x68(%rsp), %rdx
movl %r13d, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x10c0
movq %rbx, %rdi
leaq 0xd91(%rip), %rsi       # 0x2041
callq 0x10a0
testq %rax, %rax
je 0x1473
movq %rax, %r14
pxor %xmm0, %xmm0
movdqa %xmm0, 0x50(%rsp)
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x1050
movl 0x50(%rsp), %ebx
movl 0x54(%rsp), %r12d
leaq 0xd6c(%rip), %rdi       # 0x205a
movl %ebx, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x1070
movq %r13, 0x78(%rsp)
testq %r13, %r13
movl 0x1c(%rsp), %eax
cmovel %ebx, %eax
movl %eax, 0x1c(%rsp)
subl %eax, %ebx
cmpl $0x7f000001, %r12d      # imm = 0x7F000001
pushq $0x10
popq %r12
movl $0x2b11, %eax           # imm = 0x2B11
movl $0x5622, %ecx           # imm = 0x5622
cmovael %ecx, %eax
movl $0x7d00, %edx           # imm = 0x7D00
movl $0xac44, %ecx           # imm = 0xAC44
cmovael %ecx, %edx
cmpl $0x0, 0x10(%rsp)
movl %ebx, 0x50(%rsp)
cmovel %eax, %edx
movl %edx, 0x18(%rsp)
addl 0xc(%rsp), %ebx
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1090
pxor %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x40(%rsp)
leaq 0x20(%rsp), %rdi
pushq $0x30
popq %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x10d0
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x80(%rsp)
pushq $-0x30
popq %rbx
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x1050
testq %rax, %rax
je 0x13e9
movdqa 0x80(%rsp), %xmm0
pcmpeqb 0x2cd3(%rip), %xmm0     # 0x4090
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
sete %al
cmpl $-0x30, %ebx
setne %cl
testb %cl, %al
jne 0x13e9
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x10d0
addl $-0x10, %ebx
jmp 0x1396
movb $0x20, 0x27(%rsp)
leaq 0x40(%rsp), %rcx
pxor %xmm0, %xmm0
movdqu %xmm0, (%rcx)
negl %ebx
movl %ebx, %eax
shrl $0x18, %eax
movb %al, 0x2c(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x2d(%rsp)
movb %bh, 0x2e(%rsp)
movb %bl, 0x2f(%rsp)
andw $0x0, 0x30(%rsp)
movl 0x18(%rsp), %eax
movb %ah, 0x32(%rsp)
movb %al, 0x33(%rsp)
andl $0x0, 0x28(%rsp)
andl $0x0, -0x4(%rcx)
andq $0x0, -0xc(%rcx)
movl $0x70474156, 0x20(%rsp) # imm = 0x70474156
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
leaq 0x20(%rsp), %rdi
pushq $0x30
popq %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x10d0
movq %r14, %rdi
callq 0x1060
movq 0x78(%rsp), %r13
incq %r13
jmp 0x1263
leaq 0xbca(%rip), %rdi       # 0x2044
leaq 0x90(%rsp), %rsi
xorl %eax, %eax
callq 0x1070
addq $0x498, %rsp            # imm = 0x498
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop