// File: /workspace/github_c_makefile_star2_O3/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
movq %rsi, %r14
xorl %eax, %eax
movq %rax, 0x1d(%rsp)
movq %rax, 0x18(%rsp)
movq %rax, 0x4d(%rsp)
movq %rax, 0x48(%rsp)
cmpl $0x1, %edi
jg 0x13dc
movq (%r14), %rsi
leaq 0xc42(%rip), %rdi       # 0x2019
jmp 0x16ca
movq 0x8(%r14), %rdi
leaq 0xc5a(%rip), %rsi       # 0x2041
callq 0x10f0
movq 0x8(%r14), %r14
testq %rax, %rax
je 0x16b4
movq %rax, %rbx
movq %r14, %rdi
callq 0x1080
leaq 0x5(%rax), %rdi
callq 0x10c0
movq %rax, %r15
leaq 0xc43(%rip), %rsi       # 0x205a
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1100
leaq 0xc36(%rip), %rsi       # 0x2061
movq %r15, %rdi
callq 0x10f0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x16c0
movq %r15, %rdi
callq 0x1030
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10d0
movq %rbx, %rdi
callq 0x10b0
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10d0
movq %rbx, %rdi
callq 0x10b0
movl %r15d, %ecx
movq %rcx, 0x30(%rsp)
cmpq %rcx, %rax
je 0x169e
movl $0x1, %ebp
leaq 0x18(%rsp), %r14
leaq 0x48(%rsp), %r15
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movq %rbx, 0x28(%rsp)
movl $0x10, %esi
movl $0x1, %edx
leaq 0x38(%rsp), %rdi
movq %rbx, %rcx
callq 0x1060
movl 0x40(%rsp), %r13d
movq %r13, %rdi
callq 0x10c0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
movq %rbx, %rcx
callq 0x1060
movl 0x38(%rsp), %ecx
movq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
movq %r14, %rdi
movl $0x88888889, %r14d      # imm = 0x88888889
imulq %r14, %rax
shrq $0x25, %rax
movq %rcx, %rsi
movl $0x91a2b3c5, %edx       # imm = 0x91A2B3C5
imulq %rdx, %rsi
shrq $0x2b, %rsi
shrl $0x6, %ecx
imulq $0x26d60dd, %rcx, %rdx  # imm = 0x26D60DD
shrq $0x25, %rdx
movss 0xad8(%rip), %xmm1      # 0x2004
divss %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
subss %xmm1, %xmm0
movss 0xac5(%rip), %xmm1      # 0x2008
mulss %xmm1, %xmm0
cvttss2si %xmm0, %r9
imull $-0x3c, %edx, %ecx
addl %esi, %ecx
imull $-0x3c, %ecx, %r8d
addl %eax, %r8d
leaq 0xb05(%rip), %rbx       # 0x2064
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1100
movl 0x3c(%rsp), %ecx
movq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
imulq %r14, %rax
shrq $0x25, %rax
movq %rcx, %rsi
movl $0x91a2b3c5, %edx       # imm = 0x91A2B3C5
imulq %rdx, %rsi
shrq $0x2b, %rsi
shrl $0x6, %ecx
imulq $0x26d60dd, %rcx, %rdx  # imm = 0x26D60DD
shrq $0x25, %rdx
divss 0xa5e(%rip), %xmm0      # 0x2004
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
subss %xmm1, %xmm0
mulss 0xa4f(%rip), %xmm0      # 0x2008
cvttss2si %xmm0, %r9
imull $-0x3c, %edx, %ecx
addl %esi, %ecx
imull $-0x3c, %ecx, %r8d
addl %eax, %r8d
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1100
cmpb $0x0, (%r12)
movl 0xc(%rsp), %edx
jns 0x15f3
leaq 0xaa1(%rip), %rdi       # 0x208a
callq 0x1050
movl $0x1, %edx
testl %edx, %edx
sete %al
cmpl $0x0, 0x8(%rsp)
setne %cl
orb %al, %cl
movl %edx, 0xc(%rsp)
je 0x1620
testl %edx, %edx
movq 0x28(%rsp), %rbx
leaq 0x18(%rsp), %r14
jne 0x164d
movq %r12, %rdi
callq 0x12af
jmp 0x1658
movl $0x3, %esi
movl $0x1, %edx
leaq 0x2a77(%rip), %rdi      # 0x40a8
movq 0x10(%rsp), %rcx
callq 0x1110
movl $0x1, 0x8(%rsp)
movq 0x28(%rsp), %rbx
leaq 0x18(%rsp), %r14
movq %r12, %rdi
movl %r13d, %esi
callq 0x131a
movq %rax, %r13
movq 0x10(%rsp), %rdi
leaq 0xa11(%rip), %rsi       # 0x2078
movl %ebp, %edx
movq %r14, %rcx
movq %r15, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x10a0
incl %ebp
movq %r13, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x10b0
cmpq 0x30(%rsp), %rax
jne 0x14ac
movq %rbx, %rdi
callq 0x1070
movq 0x10(%rsp), %rdi
callq 0x1070
xorl %eax, %eax
jmp 0x16d6
leaq 0x989(%rip), %rdi       # 0x2044
movq %r14, %rsi
jmp 0x16ca
leaq 0x97d(%rip), %rdi       # 0x2044
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq