// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movl %edi, %ebp
movq 0x708b(%rip), %r13      # 0x9fc8
movq (%r13), %r15
movq 0x7068(%rip), %rax      # 0x9fb0
movq (%rax), %rbx
movq 0x7086(%rip), %rax      # 0x9fd8
movq (%rax), %rsi
leaq 0x31b7(%rip), %rdi      # 0x6113
callq 0x21b0
cmpl $0x2, %ebp
jl 0x2f9f
movq 0x8(%r14), %r15
leaq 0x31b6(%rip), %rsi      # 0x6127
pushq $0x7
popq %rdx
movq %r15, %rdi
callq 0x2190
testl %eax, %eax
je 0x30e6
leaq 0x31d2(%rip), %rsi      # 0x615d
movq %r15, %rdi
callq 0x21c0
movq %rax, %r15
testq %rax, %rax
je 0x3134
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x24dc
movq %rsp, %rdi
leaq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0x50fc
movq %r12, %rdi
callq 0x45c0
cmpq (%r13), %r15
je 0x2fdf
movq %r15, %rdi
callq 0x2160
cmpl $0x3, %ebp
jl 0x3000
movq 0x10(%r14), %rdi
leaq 0x317b(%rip), %rsi      # 0x616a
callq 0x21c0
movq %rax, %rbx
testq %rax, %rax
je 0x3100
leaq 0x317b(%rip), %rdi      # 0x6182
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
movq %rbx, %r12
movq %rbx, %rcx
callq 0x22c0
cmpq $0x2, %rax
jne 0x30be
movq (%rsp), %r14
movq 0x8(%rsp), %rbx
addq $-0x20, %rbx
leaq 0x18(%rsp), %r15
cmpq 0x8(%rsp), %r14
je 0x308b
movq %r15, %rdi
movq %r14, %rsi
callq 0x34fc
movq %r12, %r13
cmpq %rbx, %r14
je 0x305d
movq %r15, %rdi
leaq 0x313a(%rip), %rsi      # 0x6192
callq 0x22b0
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rbp
pushq $0x1
popq %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x22c0
movq %rax, %r13
movq %r15, %rdi
callq 0x20b0
addq $0x20, %r14
cmpq %rbp, %r13
je 0x3034
jmp 0x30be
leaq 0x3103(%rip), %rdi      # 0x6195
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %r12, %rcx
callq 0x22c0
cmpq $0x1, %rax
jne 0x30be
xorl %ebp, %ebp
movq 0x6f01(%rip), %rax      # 0x9fb0
cmpq (%rax), %r12
je 0x30cd
movq %r12, %rdi
callq 0x2160
jmp 0x30cd
leaq 0x30c0(%rip), %rdi      # 0x6185
callq 0x2150
pushq $-0x1
popq %rbp
movq %rsp, %rdi
callq 0x45c0
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6eeb(%rip), %rax      # 0x9fd8
movq (%rax), %rsi
leaq 0x3037(%rip), %rdi      # 0x612e
callq 0x21b0
xorl %ebp, %ebp
jmp 0x30d5
movq 0x10(%r14), %rdi
callq 0x2150
movq 0x6ec8(%rip), %rax      # 0x9fd8
movq (%rax), %rcx
leaq 0x3053(%rip), %rdi      # 0x616d
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
callq 0x22c0
movq 0x6e84(%rip), %rax      # 0x9fb0
movq (%rax), %rbx
jmp 0x3000
movq 0x8(%r14), %rdi
callq 0x2150
pushq $0x1
popq %rbp
jmp 0x30d5
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x3197
movq %rbx, %rdi
callq 0x2090
movq 0x6e7f(%rip), %rcx      # 0x9fd8
movq (%rcx), %rbx
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x2ff4(%rip), %rsi      # 0x6160
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2210
callq 0x2260
jmp 0x30ca
jmp 0x3194
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x20b0
jmp 0x3197
movq %rax, %rbx
movq %rsp, %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x2290
nop