// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-zoe/pss-demux.c
// Function: mpegDetectAndDumpPrivateStream
void mpegDetectAndDumpPrivateStream( FILE *f, unsigned int offset, FILE *aout, FILE *sout, FILE *pout, char *name ) {
	fseek( f, offset, SEEK_SET );
	uint32_t streamSize, headSize;
	char *outfilename = NULL;
	
	mpegPesHeader pesHead;
	mpegPrivate1ExtensionHead extHead;
	
	
	mpegPrivate1ExtensionKonami sonyExtHead;
	konamiPrivate1Header sonyPrivateHead;
	
	
	fread( &pesHead, sizeof(pesHead), 1, f );
	headSize = pesHead.size[0] << 8 | pesHead.size[1];
	fread( &extHead, sizeof(extHead), 1, f );
	fseek( f, extHead.size, SEEK_CUR );
	
	fread( &sonyExtHead, sizeof(sonyExtHead), 1, f );
	fread( &sonyPrivateHead, sizeof(sonyPrivateHead), 1, f );
	
	switch(sonyExtHead.streamId) {
		case ZOEPT_VAG: { // stream 0x01
			if( ftell(aout) == 0 ) {
				streamSize = headSize - 3 - extHead.size - 4 - 16 - (0x800-0x10); // subtract 3 for extHead size that is included in pesHead but not extHead, subtract 4 for sony extension, subtract 16 for sony header subtract 0x800-0x10 for alignment
				fseek( f, 0x800-0x10, SEEK_CUR ); // alignment stuffs
				audioSamplerate[2] = sonyPrivateHead.samplerate[0]; audioSamplerate[3] = sonyPrivateHead.samplerate[1];
				audioNumChannels = sonyPrivateHead.channels;
				unsigned char null[0x40] = {0};
				if( audioNumChannels == 1 ) fwrite( null, 1, 0x30, aout );
				else if( audioNumChannels == 2 ) fwrite( null, 1, 0x40, aout );
			}
			else {
				streamSize = headSize - 3 - extHead.size - 4;
				fseek( f, -0x10, SEEK_CUR ); // seek back size of sonyExtHead because only the first audio block contains it
			}
			writeFile( f, streamSize, aout );
			break;
		}
		case ZOEPT_DMX: { // stream 0x05
			fseek( f, -0x10, SEEK_CUR );
			writeFile( f, headSize, pout );
			break;
		}
		default: { // subtitle streams, always seem to fit into one PES
			streamSize = sonyPrivateHead.size; // for subs this is the actual size, so we can use it here
			outfilename = malloc( strlen( name ) + 7 + 2 + 4 + 1 );
			sprintf( outfilename, "%s-stream%02x.bin", name, sonyExtHead.streamId );
			if( !(sout = fopen( outfilename, "wb" ))) {
				printf("Couldnt open file %s\n", outfilename);
				return;
			}
			writeFile( f, streamSize, sout );
			fclose(sout);
			free(outfilename);
			outfilename = NULL;
			break;
		}
	}
	fseek( f, offset+headSize+6, SEEK_SET );
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10e0
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rcx
leaq -0x46(%rbp), %rdi
movl $0x6, %esi
movl $0x1, %edx
movq %rdx, -0xb0(%rbp)
callq 0x1060
movq -0xb0(%rbp), %rdx
movw -0x42(%rbp), %ax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rcx
leaq -0x49(%rbp), %rdi
movl $0x3, %esi
callq 0x1060
movq -0x8(%rbp), %rdi
movzbl -0x47(%rbp), %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x10e0
movq -0xb0(%rbp), %rdx
movq -0x8(%rbp), %rcx
leaq -0x4d(%rbp), %rdi
movl $0x4, %esi
callq 0x1060
movq -0xb0(%rbp), %rdx
movq -0x8(%rbp), %rcx
leaq -0x5d(%rbp), %rdi
movl $0x10, %esi
callq 0x1060
movzbl -0x4a(%rbp), %eax
movl %eax, -0xa4(%rbp)
subl $0x1, %eax
je 0x151a
jmp 0x1506
movl -0xa4(%rbp), %eax
subl $0x5, %eax
je 0x1616
jmp 0x1640
movq -0x18(%rbp), %rdi
callq 0x10b0
cmpq $0x0, %rax
jne 0x15da
movl -0x38(%rbp), %eax
subl $0x3, %eax
movzbl -0x47(%rbp), %ecx
subl %ecx, %eax
subl $0x4, %eax
subl $0x10, %eax
subl $0x7f0, %eax            # imm = 0x7F0
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl $0x7f0, %esi            # imm = 0x7F0
movl $0x1, %edx
callq 0x10e0
movb -0x57(%rbp), %al
movb %al, 0x2b60(%rip)       # 0x40c3
movb -0x56(%rbp), %al
movb %al, 0x2b58(%rip)       # 0x40c4
movb -0x55(%rbp), %al
movb %al, 0x2b50(%rip)       # 0x40c5
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x10a0
movzbl 0x2b36(%rip), %eax      # 0x40c5
cmpl $0x1, %eax
jne 0x15b0
leaq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl $0x1, %esi
movl $0x30, %edx
callq 0x1120
jmp 0x15d8
movzbl 0x2b0e(%rip), %eax      # 0x40c5
cmpl $0x2, %eax
jne 0x15d6
leaq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl $0x1, %esi
movl $0x40, %edx
callq 0x1120
jmp 0x15d8
jmp 0x1601
movl -0x38(%rbp), %eax
subl $0x3, %eax
movzbl -0x47(%rbp), %ecx
subl %ecx, %eax
subl $0x4, %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movq $-0x10, %rsi
movl $0x1, %edx
callq 0x10e0
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x18(%rbp), %rdx
callq 0x1230
jmp 0x16de
movq -0x8(%rbp), %rdi
movq $-0x10, %rsi
movl $0x1, %edx
callq 0x10e0
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x1230
jmp 0x16de
movzwl -0x59(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1080
movq %rax, %rdi
addq $0x7, %rdi
addq $0x2, %rdi
addq $0x4, %rdi
addq $0x1, %rdi
callq 0x10c0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rdx
movzbl -0x4a(%rbp), %ecx
leaq 0xa6f(%rip), %rsi       # 0x20ee
movb $0x0, %al
callq 0x1110
movq -0x40(%rbp), %rdi
leaq 0xa6f(%rip), %rsi       # 0x2100
callq 0x10f0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x16b4
movq -0x40(%rbp), %rsi
leaq 0xa58(%rip), %rdi       # 0x2103
movb $0x0, %al
callq 0x1090
jmp 0x16f6
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x20(%rbp), %rdx
callq 0x1230
movq -0x20(%rbp), %rdi
callq 0x1070
movq -0x40(%rbp), %rdi
callq 0x1030
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %eax
addl -0x38(%rbp), %eax
addl $0x6, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10e0
addq $0xb0, %rsp
popq %rbp
retq
nop