// File: grisu48[P]geoJson/geoJson.cpp
// Function: main
int main(int argc, char** argv) {
    if(argc < 2) {
#ifdef __linux__ 
	// Print message only if terminal, not if pipe or file
	if (::isatty(STDIN_FILENO))
        	cerr << "Reading from stdin until eof" << endl;
#endif
        processStream(cin, cout);
    } else {
        string o_filename;      // Output filename, if empty stdout
        
        for(int i=1;i<argc;i++) {
            string arg = argv[i];
            if(arg == "") continue;
            if(arg.at(0) == '-') {
                if(arg == "-h" || arg == "--help") {
                    cout << "GPX to GeoJson converter || 2017, phoenix" << endl << endl;
                    
                    cout << "Usage: " << argv[0] << " [OPTIONS] [FILES]" << endl;
                    cout << "OPTIONS:" << endl;
                    cout << "  -h   --help         Print this help message" << endl;
                    cout << "       --version      Print program version" << endl;
                    cout << "  -o FILE             Write output to FILE" << endl;
                    cout << "                      -o must be before a FILE and matches for all successive files" << endl;
                    cout << "                      It's possible to write different input files to different output files by chaining the commands:" << endl;
                    cout << "                      " << argv[0] << " -o FILE1.json FILE1.gpx -o FILE2.json FILE2.gpx ..." << endl;
                    return EXIT_SUCCESS;
                } else if(arg == "--version") {
                    cout << "1.0" << endl;
                    return EXIT_SUCCESS;
                } else if(arg == "-o") {
                    if(i >= argc-1) {
                        cerr << "Missing argument: Output file" << endl;
                        return EXIT_FAILURE;
                    }
                    o_filename = argv[++i];
                } else {
                    cerr << "Illegal argument: " << arg << endl;
                    return EXIT_FAILURE;
                }
            } else {
                ifstream f_in(argv[i]);
                if(f_in.is_open()) {
                    
                    if (o_filename.size() > 0) {
                        ofstream f_out(o_filename.c_str(), ofstream::out | ofstream::app);
                        if (!f_out.is_open()) {
                            cerr << "Error opening output file " << o_filename << endl;
                            return EXIT_FAILURE;
                        }
                        processStream(f_in, f_out);
                        f_out.close();

                    } else {
                         processStream(f_in, cout);
                    }

                    f_in.close();
                    
                } else {
                    cerr << "Error opening " << argv[i] << endl;
                    return EXIT_FAILURE;
                }   
            }
        }

    }
    return EXIT_SUCCESS;
}

// Assembly:
subq $0x588, %rsp            # imm = 0x588
movl $0x0, 0x584(%rsp)
movl %edi, 0x580(%rsp)
movq %rsi, 0x578(%rsp)
cmpl $0x2, 0x580(%rsp)
jge 0x2571
xorl %edi, %edi
callq 0x2360
cmpl $0x0, %eax
je 0x2559
movq 0xcab2(%rip), %rdi      # 0xeff0
leaq 0x7abf(%rip), %rsi      # 0xa004
callq 0x2200
movq %rax, %rdi
movq 0xca54(%rip), %rsi      # 0xefa8
callq 0x2230
movq 0xca78(%rip), %rdi      # 0xefd8
movq 0xca61(%rip), %rsi      # 0xefc8
callq 0x2e40
jmp 0x2e0b
leaq 0x558(%rsp), %rdi
callq 0x22d0
movl $0x1, 0x554(%rsp)
movl 0x554(%rsp), %eax
cmpl 0x580(%rsp), %eax
jge 0x2dd3
movq 0x578(%rsp), %rax
movslq 0x554(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x100(%rsp)
leaq 0x52f(%rsp), %rdi
movq %rdi, 0x108(%rsp)
callq 0x2380
movq 0x100(%rsp), %rsi
movq 0x108(%rsp), %rdx
leaq 0x530(%rsp), %rdi
callq 0x22c0
jmp 0x25ed
leaq 0x52f(%rsp), %rdi
callq 0x2240
leaq 0x80b4(%rip), %rsi      # 0xa6b5
leaq 0x530(%rsp), %rdi
callq 0x3e30
movb %al, 0xff(%rsp)
jmp 0x2617
movb 0xff(%rsp), %al
testb $0x1, %al
jne 0x2624
jmp 0x2673
movl $0x4, 0x518(%rsp)
jmp 0x2d83
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x520(%rsp)
movl %eax, 0x51c(%rsp)
leaq 0x52f(%rsp), %rdi
callq 0x2240
jmp 0x2dfc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x520(%rsp)
movl %eax, 0x51c(%rsp)
jmp 0x2dc4
xorl %eax, %eax
movl %eax, %esi
leaq 0x530(%rsp), %rdi
callq 0x23b0
movq %rax, 0xf0(%rsp)
jmp 0x268e
movq 0xf0(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x2b02
leaq 0x7978(%rip), %rsi      # 0xa021
leaq 0x530(%rsp), %rdi
callq 0x3e30
movb %al, 0xef(%rsp)
jmp 0x26bf
movb 0xef(%rsp), %al
testb $0x1, %al
jne 0x26f9
jmp 0x26cc
leaq 0x7951(%rip), %rsi      # 0xa024
leaq 0x530(%rsp), %rdi
callq 0x3e30
movb %al, 0xee(%rsp)
jmp 0x26e9
movb 0xee(%rsp), %al
testb $0x1, %al
jne 0x26f9
jmp 0x2977
movq 0xc8c8(%rip), %rdi      # 0xefc8
leaq 0x7924(%rip), %rsi      # 0xa02b
callq 0x2200
movq %rax, 0xe0(%rsp)
jmp 0x2716
movq 0xe0(%rsp), %rdi
movq 0xc883(%rip), %rsi      # 0xefa8
callq 0x2230
movq %rax, 0xd8(%rsp)
jmp 0x2734
movq 0xd8(%rsp), %rdi
movq 0xc865(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x274a
movq 0xc877(%rip), %rdi      # 0xefc8
leaq 0x78fd(%rip), %rsi      # 0xa055
callq 0x2200
movq %rax, 0xd0(%rsp)
jmp 0x2767
movq 0xd0(%rsp), %rdi
movq 0x578(%rsp), %rax
movq (%rax), %rsi
callq 0x2200
movq %rax, 0xc8(%rsp)
jmp 0x2789
movq 0xc8(%rsp), %rdi
leaq 0x78c5(%rip), %rsi      # 0xa05d
callq 0x2200
movq %rax, 0xc0(%rsp)
jmp 0x27a7
movq 0xc0(%rsp), %rdi
movq 0xc7f2(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x27bd
movq 0xc804(%rip), %rdi      # 0xefc8
leaq 0x78a5(%rip), %rsi      # 0xa070
callq 0x2200
movq %rax, 0xb8(%rsp)
jmp 0x27da
movq 0xb8(%rsp), %rdi
movq 0xc7bf(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x27f0
movq 0xc7d1(%rip), %rdi      # 0xefc8
leaq 0x787b(%rip), %rsi      # 0xa079
callq 0x2200
movq %rax, 0xb0(%rsp)
jmp 0x280d
movq 0xb0(%rsp), %rdi
movq 0xc78c(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x2823
movq 0xc79e(%rip), %rdi      # 0xefc8
leaq 0x7876(%rip), %rsi      # 0xa0a7
callq 0x2200
movq %rax, 0xa8(%rsp)
jmp 0x2840
movq 0xa8(%rsp), %rdi
movq 0xc759(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x2856
movq 0xc76b(%rip), %rdi      # 0xefc8
leaq 0x786f(%rip), %rsi      # 0xa0d3
callq 0x2200
movq %rax, 0xa0(%rsp)
jmp 0x2873
movq 0xa0(%rsp), %rdi
movq 0xc726(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x2889
movq 0xc738(%rip), %rdi      # 0xefc8
leaq 0x7867(%rip), %rsi      # 0xa0fe
callq 0x2200
movq %rax, 0x98(%rsp)
jmp 0x28a6
movq 0x98(%rsp), %rdi
movq 0xc6f3(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x28bc
movq 0xc705(%rip), %rdi      # 0xefc8
leaq 0x7888(%rip), %rsi      # 0xa152
callq 0x2200
movq %rax, 0x90(%rsp)
jmp 0x28d9
movq 0x90(%rsp), %rdi
movq 0xc6c0(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x28ef
movq 0xc6d2(%rip), %rdi      # 0xefc8
leaq 0x7da2(%rip), %rsi      # 0xa69f
callq 0x2200
movq %rax, 0x88(%rsp)
jmp 0x290c
movq 0x88(%rsp), %rdi
movq 0x578(%rsp), %rax
movq (%rax), %rsi
callq 0x2200
movq %rax, 0x80(%rsp)
jmp 0x292e
movq 0x80(%rsp), %rdi
leaq 0x788c(%rip), %rsi      # 0xa1c9
callq 0x2200
movq %rax, 0x78(%rsp)
jmp 0x2949
movq 0x78(%rsp), %rdi
movq 0xc653(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x295c
movl $0x0, 0x584(%rsp)
movl $0x1, 0x518(%rsp)
jmp 0x2d83
leaq 0x7880(%rip), %rsi      # 0xa1fe
leaq 0x530(%rsp), %rdi
callq 0x3e30
movb %al, 0x77(%rsp)
jmp 0x2991
movb 0x77(%rsp), %al
testb $0x1, %al
jne 0x299b
jmp 0x29e3
movq 0xc626(%rip), %rdi      # 0xefc8
leaq 0x785f(%rip), %rsi      # 0xa208
callq 0x2200
movq %rax, 0x68(%rsp)
jmp 0x29b5
movq 0x68(%rsp), %rdi
movq 0xc5e7(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x29c8
movl $0x0, 0x584(%rsp)
movl $0x1, 0x518(%rsp)
jmp 0x2d83
leaq 0x7822(%rip), %rsi      # 0xa20c
leaq 0x530(%rsp), %rdi
callq 0x3e30
movb %al, 0x67(%rsp)
jmp 0x29fd
movb 0x67(%rsp), %al
testb $0x1, %al
jne 0x2a0a
jmp 0x2a98
movl 0x554(%rsp), %eax
movl 0x580(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jl 0x2a67
movq 0xc5ca(%rip), %rdi      # 0xeff0
leaq 0x77e2(%rip), %rsi      # 0xa20f
callq 0x2200
movq %rax, 0x58(%rsp)
jmp 0x2a39
movq 0x58(%rsp), %rdi
movq 0xc563(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x2a4c
movl $0x1, 0x584(%rsp)
movl $0x1, 0x518(%rsp)
jmp 0x2d83
movq 0x578(%rsp), %rax
movslq 0x554(%rsp), %rcx
movl %ecx, %edx
incl %edx
movl %edx, 0x554(%rsp)
movq 0x8(%rax,%rcx,8), %rsi
leaq 0x558(%rsp), %rdi
callq 0x22b0
jmp 0x2a96
jmp 0x2af9
movq 0xc551(%rip), %rdi      # 0xeff0
leaq 0x7787(%rip), %rsi      # 0xa22d
callq 0x2200
movq %rax, 0x50(%rsp)
jmp 0x2ab2
movq 0x50(%rsp), %rdi
leaq 0x530(%rsp), %rsi
callq 0x21d0
movq %rax, 0x48(%rsp)
jmp 0x2acb
movq 0x48(%rsp), %rdi
movq 0xc4d1(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x2ade
movl $0x1, 0x584(%rsp)
movl $0x1, 0x518(%rsp)
jmp 0x2d83
jmp 0x2afb
jmp 0x2afd
jmp 0x2d78
movq 0x578(%rsp), %rax
movslq 0x554(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
leaq 0x310(%rsp), %rdi
movl $0x8, %edx
callq 0x2390
jmp 0x2b2a
leaq 0x310(%rsp), %rdi
callq 0x2330
movb %al, 0x47(%rsp)
jmp 0x2b3d
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x2b4a
jmp 0x2cd8
leaq 0x558(%rsp), %rdi
callq 0x2150
cmpq $0x0, %rax
jbe 0x2caf
leaq 0x558(%rsp), %rdi
callq 0x20e0
movq %rax, 0x38(%rsp)
movl $0x10, %edi
movl $0x1, %esi
callq 0x3e60
movl %eax, 0x40(%rsp)
jmp 0x2b88
movl 0x40(%rsp), %edx
movq 0x38(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x2250
jmp 0x2ba0
leaq 0x110(%rsp), %rdi
callq 0x21a0
movb %al, 0x37(%rsp)
jmp 0x2bb3
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x2c5e
jmp 0x2bc1
movq 0xc428(%rip), %rdi      # 0xeff0
leaq 0x7671(%rip), %rsi      # 0xa240
callq 0x2200
movq %rax, 0x28(%rsp)
jmp 0x2bdb
movq 0x28(%rsp), %rdi
leaq 0x558(%rsp), %rsi
callq 0x21d0
movq %rax, 0x20(%rsp)
jmp 0x2bf4
movq 0x20(%rsp), %rdi
movq 0xc3a8(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x2c07
movl $0x1, 0x584(%rsp)
movl $0x1, 0x518(%rsp)
jmp 0x2c8f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x520(%rsp)
movl %eax, 0x51c(%rsp)
jmp 0x2d69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x520(%rsp)
movl %eax, 0x51c(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x2300
jmp 0x2d69
leaq 0x310(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x2e40
jmp 0x2c75
leaq 0x110(%rsp), %rdi
callq 0x22e0
jmp 0x2c84
movl $0x0, 0x518(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x2300
movl 0x518(%rsp), %eax
testl %eax, %eax
jne 0x2d4d
jmp 0x2cad
jmp 0x2cc7
movq 0xc312(%rip), %rsi      # 0xefc8
leaq 0x310(%rsp), %rdi
callq 0x2e40
jmp 0x2cc5
jmp 0x2cc7
leaq 0x310(%rsp), %rdi
callq 0x2080
jmp 0x2cd6
jmp 0x2d42
movq 0xc311(%rip), %rdi      # 0xeff0
leaq 0x7575(%rip), %rsi      # 0xa25b
callq 0x2200
movq %rax, 0x18(%rsp)
jmp 0x2cf2
movq 0x18(%rsp), %rdi
movq 0x578(%rsp), %rax
movslq 0x554(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x2200
movq %rax, 0x10(%rsp)
jmp 0x2d17
movq 0x10(%rsp), %rdi
movq 0xc285(%rip), %rsi      # 0xefa8
callq 0x2230
jmp 0x2d2a
movl $0x1, 0x584(%rsp)
movl $0x1, 0x518(%rsp)
jmp 0x2d4d
movl $0x0, 0x518(%rsp)
leaq 0x310(%rsp), %rdi
callq 0x2090
movl 0x518(%rsp), %eax
testl %eax, %eax
jne 0x2d83
jmp 0x2d67
jmp 0x2d78
leaq 0x310(%rsp), %rdi
callq 0x2090
jmp 0x2dc4
movl $0x0, 0x518(%rsp)
leaq 0x530(%rsp), %rdi
callq 0x2120
movl 0x518(%rsp), %eax
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x2dac
jmp 0x2da1
movl 0xc(%rsp), %eax
subl $0x4, %eax
je 0x2dae
jmp 0x2dde
jmp 0x2dae
movl 0x554(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x554(%rsp)
jmp 0x2589
leaq 0x530(%rsp), %rdi
callq 0x2120
jmp 0x2dfc
movl $0x0, 0x518(%rsp)
leaq 0x558(%rsp), %rdi
callq 0x2120
movl 0x518(%rsp), %eax
testl %eax, %eax
je 0x2dfa
jmp 0x2df8
jmp 0x2e16
jmp 0x2e0b
leaq 0x558(%rsp), %rdi
callq 0x2120
jmp 0x2e25
movl $0x0, 0x584(%rsp)
movl 0x584(%rsp), %eax
addq $0x588, %rsp            # imm = 0x588
retq
movq 0x520(%rsp), %rdi
callq 0x2370
nopw %cs:(%rax,%rax)