// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-zoe/pss-demux.c
// Function: main
int main( int argc, char **argv ) {
	
	mpegPackHeader packHead;
	mpegPesHeader tempHead;
	
	uint32_t curId = 0, headSize = 0;
	unsigned char temp[4];
	FILE *pss = NULL, *pout = NULL, *sout = NULL, *aout = NULL, *vout = NULL;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s PSS-file\n", argv[0]);
		return 1;
	}
	
	if( !(pss = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	char *outfilename = malloc( strlen( argv[1] ) + 7 + 2 + 4 + 1 );
	
	sprintf( outfilename, "%s-stream00.m2v", argv[1] );
	if( !(vout = fopen( outfilename, "wb" ))) {
			printf("Couldnt open file %s\n", outfilename);
			return 1;
	}
	sprintf( outfilename, "%s-stream01.vag", argv[1] );
	if( !(aout = fopen( outfilename, "wb+" ))) {
			printf("Couldnt open file %s\n", outfilename);
			return 1;
	}
	sprintf( outfilename, "%s-stream05.dmx", argv[1] );
	if( !(pout = fopen( outfilename, "wb+" ))) {
			printf("Couldnt open file %s\n", outfilename);
			return 1;
	}
	
	fread( temp, 4, 1, pss );
	fseek( pss, - 4, SEEK_CUR );
	curId = temp[0] << 24 | temp[1] << 16 | temp[2] << 8 | temp[3];
	
	while( curId != MPEG_PROGRAM_END ) {
		switch (curId) {
			case MPEG_VIDEO: {
				// printf( "Encountered VIDEO header at %08x\n", (unsigned int)ftell(pss));
				mpegDumpVideoStream( pss, ftell(pss), vout );
				break;
			}
			case MPEG_PRIVATE_STREAM1: {
				// printf( "Encountered PRIVATE1 header at %08x\n", (unsigned int)ftell(pss));
				mpegDetectAndDumpPrivateStream( pss, ftell(pss), aout, sout, pout, argv[1] );
				break;
			}
			// SYSTEM, PADDING and PACKs are ignored for now
			case MPEG_SYSTEM_HEAD: {
				// printf( "Encountered SYSTEM header at %08x", (unsigned int)ftell(pss));
				fread( &tempHead, sizeof(tempHead), 1, pss );
				headSize = tempHead.size[0] << 8 | tempHead.size[1];
				// printf( " of %04x bytes\n", headSize );
				fseek( pss, headSize, SEEK_CUR );
				break;
			}
			case MPEG_PADDING: {
				// printf( "Encountered padding stream at %08x", (unsigned int)ftell(pss));
				fread( &tempHead, sizeof(tempHead), 1, pss );
				headSize = tempHead.size[0] << 8 | tempHead.size[1];
				// printf( " of %04x bytes\n", headSize );
				fseek( pss, headSize, SEEK_CUR );
				break;
			}
			case MPEG_PACK_HEAD: {
				// printf( "Encountered PACK header at %08x", (unsigned int)ftell(pss));
				fread( &packHead, sizeof(packHead), 1, pss );
				// printf( " with %1x bytes padding\n", packHead.reservedStuffing & 0x07 );
				fseek( pss, packHead.reservedStuffing & 0x07, SEEK_CUR );
				break;
			}
			default: {
				printf("Encountered unknown ID %08x at %08x, stopping...\n", curId, (unsigned int)ftell(pss));
				return 1;
			}
		}
		fread( temp, 4, 1, pss );
		fseek( pss, - 4, SEEK_CUR );
		curId = temp[0] << 24 | temp[1] << 16 | temp[2] << 8 | temp[3];
	}
	
	unsigned int haveAudio = ftell(aout), haveVideo = ftell(vout), havePoly = ftell(pout);
	
	if( ftell(aout) > 0 ) {
		unsigned int headSize;
		if( audioNumChannels == 1 ) headSize = 0x30;
		else if( audioNumChannels == 2 ) headSize = 0x40;
		fseek( aout, headSize, SEEK_SET );
		unsigned char readbuf[0x10] = {0};
		vagHeader header;
		while( fread( readbuf, 0x10, 1, aout )) {
			if( !memcmp( readbuf, vagEnd, 0x10 ) ) break;
		}
		uint32_t numBytes = ftell(aout)/audioNumChannels - headSize;
		vagPrepareHeader( &header, numBytes, argv[1] );
		fseek( aout, 0, SEEK_SET );
		vagWriteHeader( header, aout );
	}
	fclose(aout);
	fclose(vout);
	fclose(pout);
	fflush(stdout);
	if( !haveAudio ) {
		sprintf( outfilename, "%s-stream01.vag", argv[1] );
		if( remove( outfilename )) printf("Couldnt delete unused audio stream %s\n", outfilename );
	}
	if( !haveVideo ) {
		sprintf( outfilename, "%s-stream00.m2v", argv[1] );
		if( remove( outfilename )) printf("Couldnt delete unused video file %s\n", outfilename );
	}
	if( !havePoly ) {
		sprintf( outfilename, "%s-stream05.dmx", argv[1] );
		if( remove( outfilename )) printf("Couldnt delete unused poly file %s\n", outfilename );
	}
	
	free(outfilename);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1976
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x7c0(%rip), %rdi       # 0x2123
movb $0x0, %al
callq 0x1090
movl $0x1, -0x4(%rbp)
jmp 0x1f38
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x7c1(%rip), %rsi       # 0x2146
callq 0x10f0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x19b6
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x760(%rip), %rdi       # 0x2103
movb $0x0, %al
callq 0x1090
movl $0x1, -0x4(%rbp)
jmp 0x1f38
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1080
movq %rax, %rdi
addq $0x7, %rdi
addq $0x2, %rdi
addq $0x4, %rdi
addq $0x1, %rdi
callq 0x10c0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x757(%rip), %rsi       # 0x2149
movb $0x0, %al
callq 0x1110
movq -0x60(%rbp), %rdi
leaq 0x6fc(%rip), %rsi       # 0x2100
callq 0x10f0
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x1a31
movq -0x60(%rbp), %rsi
leaq 0x6e5(%rip), %rdi       # 0x2103
movb $0x0, %al
callq 0x1090
movl $0x1, -0x4(%rbp)
jmp 0x1f38
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x715(%rip), %rsi       # 0x2159
movb $0x0, %al
callq 0x1110
movq -0x60(%rbp), %rdi
leaq 0x713(%rip), %rsi       # 0x2169
callq 0x10f0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x1a83
movq -0x60(%rbp), %rsi
leaq 0x693(%rip), %rdi       # 0x2103
movb $0x0, %al
callq 0x1090
movl $0x1, -0x4(%rbp)
jmp 0x1f38
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x6d7(%rip), %rsi       # 0x216d
movb $0x0, %al
callq 0x1110
movq -0x60(%rbp), %rdi
leaq 0x6c1(%rip), %rsi       # 0x2169
callq 0x10f0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x1ad5
movq -0x60(%rbp), %rsi
leaq 0x641(%rip), %rdi       # 0x2103
movb $0x0, %al
callq 0x1090
movl $0x1, -0x4(%rbp)
jmp 0x1f38
leaq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x1060
movq -0x38(%rbp), %rdi
movq $-0x4, %rsi
movl $0x1, %edx
callq 0x10e0
movzbl -0x30(%rbp), %eax
shll $0x18, %eax
movzbl -0x2f(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x2e(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x2d(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x28(%rbp)
cmpl $0x1b9, -0x28(%rbp)     # imm = 0x1B9
je 0x1cfa
movl -0x28(%rbp), %eax
addl $0xfffffe46, %eax       # imm = 0xFFFFFE46
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x26, %eax
ja 0x1c73
movq -0xc0(%rbp), %rax
leaq 0x4ac(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x38(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x38(%rbp), %rdi
callq 0x10b0
movq -0xc8(%rbp), %rdi
movl %eax, %esi
movq -0x58(%rbp), %rdx
callq 0x1370
jmp 0x1ca7
movq -0x38(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x38(%rbp), %rdi
callq 0x10b0
movq -0xd0(%rbp), %rdi
movl %eax, %esi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %r9
callq 0x1430
jmp 0x1ca7
movq -0x38(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x6, %esi
movl $0x1, %edx
callq 0x1060
movzbl -0x20(%rbp), %eax
shll $0x8, %eax
movzbl -0x1f(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x10e0
jmp 0x1ca7
movq -0x38(%rbp), %rcx
leaq -0x24(%rbp), %rdi
movl $0x6, %esi
movl $0x1, %edx
callq 0x1060
movzbl -0x20(%rbp), %eax
shll $0x8, %eax
movzbl -0x1f(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x10e0
jmp 0x1ca7
movq -0x38(%rbp), %rcx
leaq -0x1e(%rbp), %rdi
movl $0xe, %esi
movl $0x1, %edx
callq 0x1060
movq -0x38(%rbp), %rdi
movzbl -0x11(%rbp), %eax
andl $0x7, %eax
movslq %eax, %rsi
movl $0x1, %edx
callq 0x10e0
jmp 0x1ca7
movl -0x28(%rbp), %eax
movl %eax, -0xd4(%rbp)
movq -0x38(%rbp), %rdi
callq 0x10b0
movl -0xd4(%rbp), %esi
movl %eax, %edx
leaq 0x4e9(%rip), %rdi       # 0x217d
movb $0x0, %al
callq 0x1090
movl $0x1, -0x4(%rbp)
jmp 0x1f38
leaq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x1060
movq -0x38(%rbp), %rdi
movq $-0x4, %rsi
movl $0x1, %edx
callq 0x10e0
movzbl -0x30(%rbp), %eax
shll $0x18, %eax
movzbl -0x2f(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x2e(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x2d(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x28(%rbp)
jmp 0x1b23
movq -0x50(%rbp), %rdi
callq 0x10b0
movl %eax, -0x64(%rbp)
movq -0x58(%rbp), %rdi
callq 0x10b0
movl %eax, -0x68(%rbp)
movq -0x40(%rbp), %rdi
callq 0x10b0
movl %eax, -0x6c(%rbp)
movq -0x50(%rbp), %rdi
callq 0x10b0
cmpq $0x0, %rax
jle 0x1e38
movzbl 0x238d(%rip), %eax      # 0x40c5
cmpl $0x1, %eax
jne 0x1d46
movl $0x30, -0x70(%rbp)
jmp 0x1d5b
movzbl 0x2378(%rip), %eax      # 0x40c5
cmpl $0x2, %eax
jne 0x1d59
movl $0x40, -0x70(%rbp)
jmp 0x1d5b
movq -0x50(%rbp), %rdi
movl -0x70(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10e0
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10a0
leaq -0x80(%rbp), %rdi
movq -0x50(%rbp), %rcx
movl $0x10, %esi
movl $0x1, %edx
callq 0x1060
cmpq $0x0, %rax
je 0x1dc2
leaq -0x80(%rbp), %rax
movups (%rax), %xmm0
movups 0x230a(%rip), %xmm1     # 0x40b0
pcmpeqb %xmm1, %xmm0
pmovmskb %xmm0, %eax
subl $0xffff, %eax           # imm = 0xFFFF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1dc0
jmp 0x1dc2
jmp 0x1d7b
movq -0x50(%rbp), %rdi
callq 0x10b0
movzbl 0x22f3(%rip), %ecx      # 0x40c5
cqto
idivq %rcx
movl -0x70(%rbp), %ecx
subq %rcx, %rax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %esi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq -0xb0(%rbp), %rdi
callq 0x1700
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10e0
movq -0x50(%rbp), %rdi
movups -0xb0(%rbp), %xmm0
movups -0xa0(%rbp), %xmm1
movups -0x90(%rbp), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x1820
movq -0x50(%rbp), %rdi
callq 0x1070
movq -0x58(%rbp), %rdi
callq 0x1070
movq -0x40(%rbp), %rdi
callq 0x1070
movq 0x2186(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x10d0
cmpl $0x0, -0x64(%rbp)
jne 0x1ea4
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x2de(%rip), %rsi       # 0x2159
movb $0x0, %al
callq 0x1110
movq -0x60(%rbp), %rdi
callq 0x1050
cmpl $0x0, %eax
je 0x1ea2
movq -0x60(%rbp), %rsi
leaq 0x314(%rip), %rdi       # 0x21af
movb $0x0, %al
callq 0x1090
jmp 0x1ea4
cmpl $0x0, -0x68(%rbp)
jne 0x1ee6
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x28c(%rip), %rsi       # 0x2149
movb $0x0, %al
callq 0x1110
movq -0x60(%rbp), %rdi
callq 0x1050
cmpl $0x0, %eax
je 0x1ee4
movq -0x60(%rbp), %rsi
leaq 0x2f9(%rip), %rdi       # 0x21d6
movb $0x0, %al
callq 0x1090
jmp 0x1ee6
cmpl $0x0, -0x6c(%rbp)
jne 0x1f28
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x26e(%rip), %rsi       # 0x216d
movb $0x0, %al
callq 0x1110
movq -0x60(%rbp), %rdi
callq 0x1050
cmpl $0x0, %eax
je 0x1f26
movq -0x60(%rbp), %rsi
leaq 0x2dc(%rip), %rdi       # 0x21fb
movb $0x0, %al
callq 0x1090
jmp 0x1f28
movq -0x60(%rbp), %rdi
callq 0x1030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq