// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-zoe/subtitle-convert.c
// Function: main
int main( int argc, char **argv ) {
	
	FILE *f, *o;
	
	unsigned int fileSize, lineNumber = 1, lineHour, lineMinute, lineSecond, lineMs, jp = 0, bom = 0;
	subLineHeader header;
	char lineStartText[13] = { 0 }, lineEndText[13] = { 0 };
	char *readline = NULL, *outputline = NULL;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s subtitle file\n", argv[0]);
		return 1;
	}
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	char *outputname = malloc( strlen( argv[1] ) + 4 + 1 );
	sprintf( outputname, "%s.srt", argv[1] );
	
	if( !(o = fopen( outputname, "wb" ))) {
		printf("Couldnt open file %s\n", outputname);
		return 1;
	}
	
	free(outputname);
	
	fseek( f, 0, SEEK_END );
	fileSize = ftell(f);
	fseek( f, 0, SEEK_SET );
	
	while( ftell(f) != fileSize ) {
		fread( &header, sizeof(header), 1, f );
		readline = malloc( header.textLength );
		fread( readline, header.textLength, 1, f );
		lineSecond = header.startFrame/60;
		lineMinute = lineSecond/60;
		lineHour = lineMinute/60;
		lineMs = 1000 * ((float)header.startFrame/60 - lineSecond);
		lineMinute -= 60*lineHour;
		lineSecond -= 60*lineMinute;
		sprintf( lineStartText, "%02d:%02d:%02d,%03d", lineHour, lineMinute, lineSecond, lineMs );
		lineSecond = header.endFrame/60;
		lineMinute = lineSecond/60;
		lineHour = lineMinute/60;
		lineMs = 1000 * ((float)header.endFrame/60 - lineSecond);
		lineMinute -= 60*lineHour;
		lineSecond -= 60*lineMinute;
		sprintf( lineEndText, "%02d:%02d:%02d,%03d", lineHour, lineMinute, lineSecond, lineMs );
		if(readline[0] & 0x80) {
			jp = 1;
			printf("Subtitles contain EUC-JP encoded text.\n");
		}
		if(jp && (!bom)) {
			fwrite( utf8bom, 3, 1, o );
			bom = 1;
		}
		if(jp) outputline = processLineJp( readline, header.textLength );
		else outputline = processLine( readline );
		fprintf( o, "%d\n%s --> %s\n%s\n\n", lineNumber, lineStartText, lineEndText, outputline );
		lineNumber++;
		free(outputline);
		free(readline);
	}
	
	fclose(f);
	fclose(o);
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
andq $0x0, 0x35(%rsp)
movq %rsi, %r14
andq $0x0, 0x30(%rsp)
andq $0x0, 0x25(%rsp)
andq $0x0, 0x20(%rsp)
cmpl $0x1, %edi
jg 0x13da
movq (%r14), %rsi
leaq 0xc44(%rip), %rdi       # 0x2019
jmp 0x1687
movq 0x8(%r14), %rdi
leaq 0xc5c(%rip), %rsi       # 0x2041
callq 0x10f0
movq 0x8(%r14), %r14
testq %rax, %rax
je 0x1671
movq %rax, %rbx
movq %r14, %rdi
callq 0x1080
leaq 0x5(%rax), %rdi
callq 0x10c0
movq %rax, %r15
leaq 0xc45(%rip), %rsi       # 0x205a
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1100
leaq 0xc38(%rip), %rsi       # 0x2061
movq %r15, %rdi
callq 0x10f0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x167d
movq %r15, %rdi
callq 0x1030
pushq $0x2
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10d0
movq %rbx, %rdi
callq 0x10b0
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10d0
movl %r15d, %eax
movq %rax, 0x40(%rsp)
pushq $0x1
popq %rbp
xorl %r13d, %r13d
movl $0x0, 0x8(%rsp)
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
callq 0x10b0
cmpq 0x40(%rsp), %rax
je 0x165b
movl %ebp, 0xc(%rsp)
leaq 0x48(%rsp), %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
movq %rbx, %rcx
callq 0x1060
movl 0x50(%rsp), %r12d
movq %r12, %rdi
callq 0x10c0
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x1060
movl 0x48(%rsp), %ecx
movl %ecx, %eax
xorl %edx, %edx
pushq $0x3c
popq %r14
divl %r14d
movl %eax, %esi
movl %ecx, %eax
xorl %edx, %edx
movl $0xe10, %edi            # imm = 0xE10
divl %edi
movl %eax, %edi
movl %ecx, %eax
xorl %edx, %edx
movl $0x34bc0, %ebx          # imm = 0x34BC0
divl %ebx
cvtsi2ss %rcx, %xmm0
movss 0xaf9(%rip), %xmm1      # 0x2004
divss %xmm1, %xmm0
cvtsi2ss %esi, %xmm1
subss %xmm1, %xmm0
movss 0xae9(%rip), %xmm1      # 0x2008
mulss %xmm1, %xmm0
cvttss2si %xmm0, %r9
imull $-0x3c, %eax, %ecx
addl %edi, %ecx
imull $-0x3c, %ecx, %r8d
addl %esi, %r8d
leaq 0x30(%rsp), %rdi
leaq 0xb24(%rip), %rbp       # 0x2064
movq %rbp, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x1100
movl 0x4c(%rsp), %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r14d
movl %eax, %esi
movl %ecx, %eax
xorl %edx, %edx
movl $0xe10, %edi            # imm = 0xE10
divl %edi
movl %eax, %edi
movl %ecx, %eax
xorl %edx, %edx
divl %ebx
cvtsi2ss %rcx, %xmm0
divss 0xa8b(%rip), %xmm0      # 0x2004
cvtsi2ss %esi, %xmm1
subss %xmm1, %xmm0
mulss 0xa7f(%rip), %xmm0      # 0x2008
cvttss2si %xmm0, %r9
imull $-0x3c, %eax, %ecx
addl %edi, %ecx
imull $-0x3c, %ecx, %r8d
addl %esi, %r8d
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x1100
cmpb $0x0, (%r15)
jns 0x15c1
leaq 0xad2(%rip), %rdi       # 0x208a
callq 0x1050
pushq $0x1
popq %r13
testl %r13d, %r13d
sete %al
cmpl $0x0, 0x8(%rsp)
setne %cl
orb %al, %cl
je 0x15eb
testl %r13d, %r13d
movq 0x18(%rsp), %rbx
movl 0xc(%rsp), %ebp
jne 0x1614
movq %r15, %rdi
callq 0x12af
jmp 0x161f
leaq 0x2ab6(%rip), %rdi      # 0x40a8
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rcx
callq 0x1110
pushq $0x1
popq %r14
movl %r14d, 0x8(%rsp)
movq 0x18(%rsp), %rbx
movl 0xc(%rsp), %ebp
movq %r15, %rdi
movl %r12d, %esi
callq 0x1316
movq %rax, %r12
movq 0x10(%rsp), %rdi
leaq 0xa4a(%rip), %rsi       # 0x2078
movl %ebp, %edx
leaq 0x30(%rsp), %rcx
leaq 0x20(%rsp), %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x10a0
incl %ebp
movq %r12, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
jmp 0x1486
movq %rbx, %rdi
callq 0x1070
movq 0x10(%rsp), %rdi
callq 0x1070
xorl %eax, %eax
jmp 0x1691
leaq 0x9cc(%rip), %rdi       # 0x2044
movq %r14, %rsi
jmp 0x1687
leaq 0x9c0(%rip), %rdi       # 0x2044
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq