// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x28(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
leaq -0x5d(%rbp), %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0x1090
leaq -0x6a(%rbp), %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0x1090
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1515
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xb17(%rip), %rdi       # 0x2019
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x188c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xb1d(%rip), %rsi       # 0x2041
callq 0x10f0
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x1555
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xb02(%rip), %rdi       # 0x2044
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x188c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1070
movq %rax, %rdi
addq $0x4, %rdi
addq $0x1, %rdi
callq 0x10c0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xacb(%rip), %rsi       # 0x205a
movb $0x0, %al
callq 0x1100
movq -0x88(%rbp), %rdi
leaq 0xabd(%rip), %rsi       # 0x2061
callq 0x10f0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x15d4
movq -0x88(%rbp), %rsi
leaq 0xa83(%rip), %rdi       # 0x2044
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x188c
movq -0x88(%rbp), %rdi
callq 0x1030
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10d0
movq -0x18(%rbp), %rdi
callq 0x10b0
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10d0
movq -0x18(%rbp), %rdi
callq 0x10b0
movl -0x24(%rbp), %ecx
cmpq %rcx, %rax
je 0x1873
movq -0x18(%rbp), %rcx
leaq -0x50(%rbp), %rdi
movl $0x10, %esi
movl $0x1, %edx
movq %rdx, -0xa0(%rbp)
callq 0x1050
movl -0x48(%rbp), %eax
movl %eax, %edi
callq 0x10c0
movq -0xa0(%rbp), %rdx
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movl -0x48(%rbp), %eax
movl %eax, %esi
movq -0x18(%rbp), %rcx
callq 0x1050
movl -0x50(%rbp), %eax
movl $0x88888889, %ecx       # imm = 0x88888889
movq %rcx, -0x98(%rbp)
imulq %rcx, %rax
shrq $0x25, %rax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
imulq %rcx, %rax
shrq $0x25, %rax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
imulq %rcx, %rax
shrq $0x25, %rax
movl %eax, -0x2c(%rbp)
movl -0x50(%rbp), %eax
cvtsi2ss %rax, %xmm0
movss 0x957(%rip), %xmm1      # 0x2004
movss %xmm1, -0x90(%rbp)
divss %xmm1, %xmm0
movl -0x34(%rbp), %eax
cvtsi2ss %rax, %xmm1
subss %xmm1, %xmm0
movss 0x93b(%rip), %xmm1      # 0x2008
movss %xmm1, -0x8c(%rbp)
mulss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, -0x38(%rbp)
movl -0x2c(%rbp), %eax
imull $0x3c, %eax, %ecx
movl -0x30(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
imull $0x3c, %eax, %ecx
movl -0x34(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %r8d
movl -0x38(%rbp), %r9d
leaq 0x952(%rip), %rsi       # 0x2064
xorl %eax, %eax
leaq -0x5d(%rbp), %rdi
callq 0x1100
movq -0x98(%rbp), %rcx
movss -0x90(%rbp), %xmm2
movss -0x8c(%rbp), %xmm1
movl -0x4c(%rbp), %eax
imulq %rcx, %rax
shrq $0x25, %rax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
imulq %rcx, %rax
shrq $0x25, %rax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
imulq %rcx, %rax
shrq $0x25, %rax
movl %eax, -0x2c(%rbp)
movl -0x4c(%rbp), %eax
cvtsi2ss %rax, %xmm0
divss %xmm2, %xmm0
movl -0x34(%rbp), %eax
cvtsi2ss %rax, %xmm2
subss %xmm2, %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, -0x38(%rbp)
imull $0x3c, -0x2c(%rbp), %ecx
movl -0x30(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x30(%rbp)
imull $0x3c, -0x30(%rbp), %ecx
movl -0x34(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
leaq -0x6a(%rbp), %rdi
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %r8d
movl -0x38(%rbp), %r9d
leaq 0x8b1(%rip), %rsi       # 0x2064
movb $0x0, %al
callq 0x1100
movq -0x78(%rbp), %rax
movsbl (%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x17e0
movl $0x1, -0x3c(%rbp)
leaq 0x89f(%rip), %rdi       # 0x2078
movb $0x0, %al
callq 0x1080
cmpl $0x0, -0x3c(%rbp)
je 0x180d
cmpl $0x0, -0x40(%rbp)
jne 0x180d
movq -0x20(%rbp), %rcx
leaq 0x28b1(%rip), %rdi      # 0x40a8
movl $0x3, %esi
movl $0x1, %edx
callq 0x1110
movl $0x1, -0x40(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x1825
movq -0x78(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x1410
movq %rax, -0x80(%rbp)
jmp 0x1832
movq -0x78(%rbp), %rdi
callq 0x1310
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rdi
movl -0x28(%rbp), %edx
leaq -0x5d(%rbp), %rcx
leaq -0x6a(%rbp), %r8
movq -0x80(%rbp), %r9
leaq 0x854(%rip), %rsi       # 0x20a0
movb $0x0, %al
callq 0x10a0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x80(%rbp), %rdi
callq 0x1030
movq -0x78(%rbp), %rdi
callq 0x1030
jmp 0x160d
movq -0x18(%rbp), %rdi
callq 0x1060
movq -0x20(%rbp), %rdi
callq 0x1060
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq