// File: /workspace/github_c_makefile_star2_O1/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 $0x488, %rsp            # imm = 0x488
movl %r8d, 0x14(%rsp)
movq %rdx, 0x68(%rsp)
testl %ecx, %ecx
je 0x14aa
movq %rdi, %r15
movl %ecx, %eax
movq %rax, 0x60(%rsp)
shll $0x4, %ecx
movl %esi, %eax
addl $0x10, %esi
movl %esi, 0x10(%rsp)
addl %ecx, %eax
addl $0x20, %eax
movl %eax, 0xc(%rsp)
leaq 0x70(%rsp), %rbp
xorl %r14d, %r14d
movl %r14d, %esi
shll $0x4, %esi
addl 0x10(%rsp), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x1090
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
leaq 0xd9e(%rip), %rsi       # 0x2030
movq 0x68(%rsp), %rdx
movl %r14d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x10c0
movq %rbx, %rdi
leaq 0xd93(%rip), %rsi       # 0x2041
callq 0x10a0
testq %rax, %rax
je 0x1494
movq %rax, %r13
pxor %xmm0, %xmm0
movdqa %xmm0, 0x50(%rsp)
movl $0x10, %esi
movl $0x1, %edx
leaq 0x50(%rsp), %rdi
movq %r15, %rcx
callq 0x1050
movl 0x50(%rsp), %r12d
movq %rbp, %rbx
movl 0x54(%rsp), %ebp
leaq 0xd67(%rip), %rdi       # 0x205a
movl %r12d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x1070
testq %r14, %r14
movl 0x1c(%rsp), %eax
cmovel %r12d, %eax
movl %eax, 0x1c(%rsp)
subl %eax, %r12d
cmpl $0x7f000001, %ebp       # imm = 0x7F000001
movq %rbx, %rbp
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, 0x14(%rsp)
movl %r12d, 0x50(%rsp)
cmovel %eax, %edx
movl %edx, 0x18(%rsp)
addl 0xc(%rsp), %r12d
movq %r15, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %r12
xorl %edx, %edx
callq 0x1090
pxor %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x40(%rsp)
movl $0x30, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rcx
callq 0x10d0
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x70(%rsp)
movl $0x10, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1050
movl $0x30, %ebx
testq %rax, %rax
je 0x1403
movdqa 0x70(%rsp), %xmm0
pcmpeqb 0x2cd2(%rip), %xmm0     # 0x4090
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
sete %al
cmpl $0x30, %ebx
setne %cl
testb %cl, %al
jne 0x1403
movl $0x10, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r13, %rcx
callq 0x10d0
addl $0x10, %ebx
movl $0x10, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x1050
jmp 0x13ab
movb $0x20, 0x27(%rsp)
leaq 0x40(%rsp), %rcx
pxor %xmm0, %xmm0
movdqu %xmm0, (%rcx)
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)
movw $0x0, 0x30(%rsp)
movl 0x18(%rsp), %eax
movb %ah, 0x32(%rsp)
movb %al, 0x33(%rsp)
xorl %eax, %eax
movl %eax, 0x28(%rsp)
movl %eax, -0x4(%rcx)
movq $0x0, -0xc(%rcx)
movl $0x70474156, 0x20(%rsp) # imm = 0x70474156
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
movl $0x30, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rcx
callq 0x10d0
movq %r13, %rdi
callq 0x1060
incq %r14
cmpq 0x60(%rsp), %r14
jne 0x126c
jmp 0x14aa
leaq 0xba9(%rip), %rdi       # 0x2044
leaq 0x80(%rsp), %rsi
xorl %eax, %eax
callq 0x1070
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq