// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x104f8(%rip), %rax     # 0x13fc8
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq 0x104d2(%rip), %rax     # 0x13fb0
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq 0x104ec(%rip), %rax     # 0x13fd8
movq (%rax), %rsi
leaq 0x961f(%rip), %rdi      # 0xd115
callq 0x22a0
cmpl $0x2, -0x8(%rbp)
jl 0x3b41
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x9619(%rip), %rsi      # 0xd129
movl $0x7, %edx
callq 0x20f0
cmpl $0x0, %eax
jne 0x3b41
movq 0x104b2(%rip), %rax     # 0x13fd8
movq (%rax), %rsi
leaq 0x9600(%rip), %rdi      # 0xd130
callq 0x22a0
movl $0x0, -0x4(%rbp)
jmp 0x3eed
cmpl $0x2, -0x8(%rbp)
jl 0x3b81
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x9609(%rip), %rsi      # 0xd15f
callq 0x22b0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x3b7f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x21d0
movl $0x1, -0x4(%rbp)
jmp 0x3eed
jmp 0x3b81
leaq -0x38(%rbp), %rdi
callq 0x4940
movq -0x18(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x2680
jmp 0x3b99
leaq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4f20
leaq -0x50(%rbp), %rdi
callq 0x4ea0
jmp 0x3c3e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3ee2
movq -0x58(%rbp), %rdi
callq 0x20b0
movq %rax, -0x68(%rbp)
movq 0x103f4(%rip), %rax     # 0x13fd8
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x68(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq -0xc8(%rbp), %rdi
movq %rax, %rdx
leaq 0x9556(%rip), %rsi      # 0xd162
xorl %eax, %eax
callq 0x2300
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x6c(%rbp)
callq 0x2360
jmp 0x3c28
jmp 0x3ed7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x3ee2
movq -0x18(%rbp), %rax
movq 0x1037f(%rip), %rcx     # 0x13fc8
cmpq (%rcx), %rax
je 0x3c5b
movq -0x18(%rbp), %rdi
callq 0x2210
jmp 0x3c59
jmp 0x3c5b
cmpl $0x3, -0x8(%rbp)
jl 0x3cc7
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x94fc(%rip), %rsi      # 0xd16c
callq 0x22b0
movq %rax, -0xd0(%rbp)
jmp 0x3c7e
movq -0xd0(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3cc5
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x21d0
jmp 0x3c9f
movq 0x10332(%rip), %rax     # 0x13fd8
movq (%rax), %rdi
leaq 0x94bf(%rip), %rsi      # 0xd16f
movb $0x0, %al
callq 0x2300
movq 0x102f2(%rip), %rax     # 0x13fb0
movq (%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x3cc7
movq -0x20(%rbp), %rcx
leaq 0x94b2(%rip), %rdi      # 0xd184
movl $0x1, %esi
movl $0x2, %edx
callq 0x23c0
movq %rax, -0xd8(%rbp)
jmp 0x3cea
movq -0xd8(%rbp), %rax
cmpq $0x2, %rax
je 0x3d1a
jmp 0x3cf9
leaq 0x9487(%rip), %rdi      # 0xd187
callq 0x21d0
jmp 0x3d07
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x6c(%rbp)
jmp 0x3ed7
leaq -0x38(%rbp), %rdi
callq 0x4e50
movq %rax, -0x80(%rbp)
leaq -0x80(%rbp), %rdi
movl $0x1, %esi
callq 0x4f50
movq %rax, -0x78(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4e20
movq %rax, -0x88(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4e50
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x4f90
testb $0x1, %al
jne 0x3d75
jmp 0x3e79
leaq -0x88(%rbp), %rdi
callq 0x4fd0
movq %rax, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x4ff0
jmp 0x3d92
leaq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x4f90
testb $0x1, %al
jne 0x3da8
jmp 0x3ddc
leaq 0x93e5(%rip), %rsi      # 0xd194
leaq -0xb0(%rbp), %rdi
callq 0x2200
jmp 0x3dbd
jmp 0x3ddc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x2160
jmp 0x3ee2
leaq -0xb0(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x2330
movq -0xe8(%rbp), %rdi
movq %rax, -0xb8(%rbp)
callq 0x20c0
movq %rax, %rdi
movq -0xb8(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x23c0
movq %rax, -0xe0(%rbp)
jmp 0x3e23
movq -0xe0(%rbp), %rax
cmpq -0xb8(%rbp), %rax
je 0x3e3c
movl $0x2, -0x6c(%rbp)
jmp 0x3e43
movl $0x0, -0x6c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x2160
movl -0x6c(%rbp), %eax
testl %eax, %eax
je 0x3e5d
jmp 0x3e58
jmp 0x3cf9
jmp 0x3e5f
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0x5130
movq %rax, -0xc0(%rbp)
jmp 0x3d49
movq -0x20(%rbp), %rcx
leaq 0x9313(%rip), %rdi      # 0xd197
movl $0x1, %edx
movq %rdx, %rsi
callq 0x23c0
movq %rax, -0xf0(%rbp)
jmp 0x3e9a
movq -0xf0(%rbp), %rax
cmpq $0x1, %rax
je 0x3eac
jmp 0x3cf9
movq -0x20(%rbp), %rax
movq 0x100f9(%rip), %rcx     # 0x13fb0
cmpq (%rcx), %rax
je 0x3ec9
movq -0x20(%rbp), %rdi
callq 0x2210
jmp 0x3ec7
jmp 0x3ec9
movl $0x0, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4ea0
jmp 0x3eed
leaq -0x38(%rbp), %rdi
callq 0x4ea0
jmp 0x3ef9
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x2390
nopw %cs:(%rax,%rax)
nopl (%rax)