// File: /workspace/github_cpp_makefile_star2_O1/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), %r14
movq 0x6cdc(%rip), %rax      # 0x8fb0
movq (%rax), %r13
movq 0x6cfa(%rip), %rax      # 0x8fd8
movq (%rax), %rsi
leaq 0x3e2b(%rip), %rdi      # 0x6113
callq 0x21a0
cmpl $0x2, %ebp
jl 0x232c
movq 0x8(%r12), %rbx
leaq 0x3e29(%rip), %rsi      # 0x6127
pushq $0x7
popq %rdx
movq %rbx, %rdi
callq 0x2180
testl %eax, %eax
je 0x24ca
leaq 0x3e45(%rip), %rsi      # 0x615d
movq %rbx, %rdi
callq 0x21b0
movq %rax, %r14
testq %rax, %rax
je 0x2519
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x2834
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
cmpq (%r15), %r14
je 0x235e
movq %r14, %rdi
callq 0x2150
cmpl $0x3, %ebp
movq %r13, %rbp
jl 0x2383
movq 0x10(%r12), %rdi
leaq 0x3df8(%rip), %rsi      # 0x616a
callq 0x21b0
movq %rax, %rbp
testq %rax, %rax
je 0x24e4
leaq 0x3df8(%rip), %rdi      # 0x6182
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
movq %rbp, %rcx
callq 0x2290
cmpq $0x2, %rax
jne 0x24b9
pushq $0x3
popq %r14
movq 0x30(%rsp), %rax
cmpq %rax, %rbx
movq %rbx, 0x8(%rsp)
je 0x243d
leaq -0x20(%rax), %r15
movq %rbx, %r14
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x3a4a
cmpq %r15, %r14
je 0x23e3
leaq 0x10(%rsp), %rdi
leaq 0x3db4(%rip), %rsi      # 0x6192
callq 0x2280
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rbx
pushq $0x1
popq %rsi
movq %rbx, %rdx
movq %rbp, %r12
movq %rbp, %rcx
callq 0x2290
movq %rax, %rbp
cmpq %rbx, %rax
sete %r13b
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x2424
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2170
cmpq %rbx, %rbp
jne 0x2442
addq $0x20, %r14
cmpq 0x30(%rsp), %r14
movq %r12, %rbp
jne 0x23c0
pushq $0x3
popq %r14
jmp 0x2449
movb $0x1, %r13b
jmp 0x2449
pushq $0x2
popq %r14
movq %r12, %rbp
testb %r13b, %r13b
movq 0x8(%rsp), %rbx
je 0x248c
leaq 0x3d3b(%rip), %rdi      # 0x6195
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %rbp, %rcx
callq 0x2290
cmpq $0x1, %rax
jne 0x2488
pushq $0x1
popq %r14
movq 0x6b37(%rip), %rax      # 0x8fb0
cmpq (%rax), %rbp
je 0x248c
movq %rbp, %rdi
callq 0x2150
jmp 0x248c
pushq $0x2
popq %r14
xorl %ebp, %ebp
cmpl $0x2, %r14d
je 0x24b9
testq %rbx, %rbx
je 0x24a8
movq (%rsp), %rsi
subq %rbx, %rsi
movq %rbx, %rdi
callq 0x2170
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3cc5(%rip), %rdi      # 0x6185
callq 0x2140
pushq $-0x1
popq %rbp
jmp 0x2494
movq 0x6b07(%rip), %rax      # 0x8fd8
movq (%rax), %rsi
leaq 0x3c53(%rip), %rdi      # 0x612e
callq 0x21a0
xorl %ebp, %ebp
jmp 0x24a8
movq 0x10(%r12), %rdi
callq 0x2140
movq 0x6ae3(%rip), %rax      # 0x8fd8
movq (%rax), %rcx
leaq 0x3c6e(%rip), %rdi      # 0x616d
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
callq 0x2290
movq 0x6a9f(%rip), %rax      # 0x8fb0
movq (%rax), %rbp
jmp 0x2383
movq 0x8(%r12), %rdi
callq 0x2140
pushq $0x1
popq %rbp
jmp 0x24a8
movq %rax, %r15
cmpl $0x1, %edx
jne 0x2579
movq %r15, %rdi
callq 0x2090
movq 0x6a99(%rip), %rcx      # 0x8fd8
movq (%rcx), %rbx
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x3c0e(%rip), %rsi      # 0x6160
movq $0x0, (%rsp)
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21e0
callq 0x2230
pushq $-0x1
popq %rbp
xorl %ebx, %ebx
jmp 0x2494
movq %rax, %r15
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
jmp 0x25b0
movq %rax, %r15
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x25b0
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2170
jmp 0x25b0
movq %rax, %r15
cmpq $0x0, 0x8(%rsp)
je 0x25c9
movq (%rsp), %rsi
movq 0x8(%rsp), %rdi
subq %rdi, %rsi
callq 0x2170
movq %r15, %rdi
callq 0x2260