// File: /workspace/github_cpp_makefile_star2_O3/MikuAuahDark[P]midi2sif/src/midi2sif.cpp
// Function: main
int main(int argc, char* argv[])
{
	FILE* input_file = stdin;
	FILE* output_file = stdout;

#ifdef WIN32
	_setmode(0, 0x8000);
	_setmode(1, 0x8000);
#endif

	fputs("MIDI2SIF converter\n", stderr);

	if(argc >= 2 && memcmp(argv[1], "--help", 7) == 0)
	{
		fputs("Usage: MIDI2SIF [input=stdin] [output=stdout]\n", stderr);

		return 0;
	}

	if(argc >= 2)
	{
		input_file = fopen(argv[1], "rb");

		if(input_file == NULL)
		{
			perror(argv[1]);

			return 1;
		}
	}

	std::vector<SIFBeatmap> beatmaps;

	try
	{
		beatmaps = midi2sif(input_file);
	}
	catch(std::exception& e)
	{
		fprintf(stderr, "Error: %s", e.what());

		return (-1);
	}

	if(input_file != stdin)
		fclose(input_file);

	if(argc >= 3)
	{
		output_file = fopen(argv[2], "wb");

		if(output_file == NULL)
		{
			perror(argv[2]);
			fprintf(stderr, "Writing to stdout...");

			output_file = stdout;
		}
	}

	if(fwrite("[\n", 1, 2, output_file) != 2)
	{
		write_fail:
		perror("Cannot write");
		return (-1);
	}

	std::vector<SIFBeatmap>::iterator last_val = beatmaps.end() - 1;
	for(std::vector<SIFBeatmap>::iterator i = beatmaps.begin(); i != beatmaps.end(); i++)
	{
		std::string str = *i;

		if(i != last_val)
			str += ",\n";

		size_t length = str.length();

		if(fwrite(str.c_str(), 1, length, output_file) != length)
			goto write_fail;
	}

	if(fwrite("]", 1, 1, output_file) != 1)
		goto write_fail;

	if(output_file != stdout)
		fclose(output_file);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r12
movl %edi, %ebp
movq 0x6cfe(%rip), %r15      # 0x8fc8
movq (%r15), %rbx
movq 0x6cdc(%rip), %rax      # 0x8fb0
movq (%rax), %r14
movq 0x6cfa(%rip), %r13      # 0x8fd8
movq (%r13), %rsi
leaq 0x3e2a(%rip), %rdi      # 0x6113
callq 0x21a0
cmpl $0x2, %ebp
jl 0x232d
movq 0x8(%r12), %rbx
leaq 0x3e28(%rip), %rsi      # 0x6127
pushq $0x7
popq %rdx
movq %rbx, %rdi
callq 0x2180
testl %eax, %eax
je 0x249a
leaq 0x3e44(%rip), %rsi      # 0x615d
movq %rbx, %rdi
callq 0x21b0
movq %rax, %rbx
testq %rax, %rax
je 0x24dd
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x27c4
movq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq (%r15), %rbx
je 0x2364
movq %rbx, %rdi
callq 0x2150
cmpl $0x3, %ebp
movq %r14, %rbp
jl 0x2389
movq 0x10(%r12), %rdi
leaq 0x3df2(%rip), %rsi      # 0x616a
callq 0x21b0
movq %rax, %rbp
testq %rax, %rax
je 0x24ae
leaq 0x3df2(%rip), %rdi      # 0x6182
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
movq %rbp, %rcx
callq 0x2290
cmpq $0x2, %rax
jne 0x2464
movq 0x8(%rsp), %rax
cmpq %rax, (%rsp)
je 0x2431
movq 0x8(%rsp), %rax
leaq -0x20(%rax), %r15
leaq 0x10(%rsp), %r12
movq (%rsp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3970
cmpq %r15, %rbx
je 0x23e4
movq %r12, %rdi
leaq 0x3db3(%rip), %rsi      # 0x6192
callq 0x2280
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r14
pushq $0x1
popq %rsi
movq %r14, %rdx
movq %rbp, %r13
movq %rbp, %rcx
callq 0x2290
movq %rax, %rbp
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x241e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2170
cmpq %r14, %rbp
jne 0x2464
movq %r13, %rbp
addq $0x20, %rbx
cmpq 0x8(%rsp), %rbx
jne 0x23c5
leaq 0x3d5d(%rip), %rdi      # 0x6195
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %rbp, %rcx
callq 0x2290
cmpq $0x1, %rax
jne 0x2464
xorl %ebx, %ebx
movq 0x6b5b(%rip), %rax      # 0x8fb0
cmpq (%rax), %rbp
je 0x2473
movq %rbp, %rdi
callq 0x2150
jmp 0x2473
leaq 0x3d1a(%rip), %rdi      # 0x6185
callq 0x2140
pushq $-0x1
popq %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2489
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rsi
leaq 0x3c89(%rip), %rdi      # 0x612e
callq 0x21a0
xorl %ebx, %ebx
jmp 0x2489
movq 0x10(%r12), %rdi
callq 0x2140
movq (%r13), %rcx
leaq 0x3caa(%rip), %rdi      # 0x616d
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
callq 0x2290
movq 0x6adb(%rip), %rax      # 0x8fb0
movq (%rax), %rbp
jmp 0x2389
movq 0x8(%r12), %rdi
callq 0x2140
pushq $0x1
popq %rbx
jmp 0x2489
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x2562
movq %rbx, %rdi
callq 0x2090
movq (%r13), %rbx
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x3c50(%rip), %rsi      # 0x6160
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21e0
callq 0x2230
pushq $-0x1
jmp 0x24e9
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x254a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2170
jmp 0x254a
movq %rax, %rbx
cmpq $0x0, (%rsp)
je 0x2562
movq (%rsp), %rdi
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2260