// File: /workspace/github_cpp_makefile_star2_O2/joncampbell123[P]floppytools/kryofluxplot.cpp
// Function: main
int main(int argc,char **argv) {
    if (argc < 2) {
        fprintf(stderr,"%s <raw>\n",argv[0]);
        return 1;
    }

    FILE *fp = fopen(argv[1],"rb");
    if (fp == NULL) return 1;

    FILE *csv_fp = fopen("graph.csv","w");
    if (csv_fp == NULL) return 1;

    struct kryoflux_event ev;
    unsigned long count = 0;

    fprintf(csv_fp,"# index, flux\n");
    while (kryoflux_read(ev,fp)) {
        if (ev.message == MSG_FLUX) {
            fprintf(csv_fp,"%lu, %lu\n",count,ev.flux_interval);
            count++;
        }
    }

    FILE *graph_fp = fopen("graph.gnuplot","w");
    if (graph_fp == NULL) return 1;

    fprintf(graph_fp,"reset\n");
    fprintf(graph_fp,"set term png size 1024,256\n");
    fprintf(graph_fp,"set output 'graph.png'\n");
    fprintf(graph_fp,"set grid\n");
    fprintf(graph_fp,"set autoscale\n");
    fprintf(graph_fp,"set title 'Flux reversals'\n");
    fprintf(graph_fp,"set xlabel 'Sample'\n");
    fprintf(graph_fp,"set ylabel 'Flux reversal distance'\n");
    fprintf(graph_fp,"plot 'graph.csv' using 1:2 with points pointtype 3 pointsize 0.1 title 'Distance'\n");

    fclose(graph_fp);
    fclose(csv_fp);
    fclose(fp);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x1, %edi
jle 0x1484
movq 0x8(%rsi), %rdi
leaq 0x1d3d(%rip), %rsi      # 0x300e
callq 0x1100
pushq $0x1
popq %r14
testq %rax, %rax
je 0x1472
movq %rax, %rbx
leaq 0x1d24(%rip), %rdi      # 0x3011
leaq 0x20d7(%rip), %rsi      # 0x33cb
callq 0x1100
testq %rax, %rax
je 0x1472
movq %rax, %r15
leaq -0x40(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x1d00(%rip), %rdi      # 0x301b
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x11a0
leaq -0x50(%rbp), %r12
leaq 0x1cf6(%rip), %r14      # 0x302a
xorl %r13d, %r13d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x14d0
testb %al, %al
je 0x1364
cmpb $0x1, -0x4c(%rbp)
jne 0x1337
movl -0x50(%rbp), %ecx
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1150
incq %r13
jmp 0x1337
leaq 0x1cc9(%rip), %rdi      # 0x3034
leaq 0x2059(%rip), %rsi      # 0x33cb
callq 0x1100
testq %rax, %rax
je 0x1465
movq %rax, %r12
leaq 0x1cb8(%rip), %rdi      # 0x3042
pushq $0x6
popq %rsi
pushq $0x1
popq %r13
movq %r13, %rdx
movq %rax, %rcx
callq 0x11a0
leaq 0x1ca6(%rip), %rdi      # 0x3049
pushq $0x1b
popq %r14
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x11a0
leaq 0x1ca9(%rip), %rdi      # 0x3065
pushq $0x17
popq %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x11a0
leaq 0x1cac(%rip), %rdi      # 0x307d
pushq $0x9
popq %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x11a0
leaq 0x1ca1(%rip), %rdi      # 0x3087
pushq $0xe
popq %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x11a0
leaq 0x1c9b(%rip), %rdi      # 0x3096
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x11a0
leaq 0x1ca2(%rip), %rdi      # 0x30b2
pushq $0x14
popq %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x11a0
leaq 0x1ca2(%rip), %rdi      # 0x30c7
pushq $0x24
popq %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x11a0
leaq 0x1cb2(%rip), %rdi      # 0x30ec
pushq $0x52
popq %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x11a0
movq %r12, %rdi
callq 0x10d0
movq %r15, %rdi
callq 0x10d0
movq %rbx, %rdi
callq 0x10d0
xorl %r14d, %r14d
jmp 0x1469
pushq $0x1
popq %r14
leaq -0x40(%rbp), %rdi
callq 0x14ba
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3b55(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0x1b6c(%rip), %rsi      # 0x3004
xorl %eax, %eax
callq 0x1150
pushq $0x1
popq %r14
jmp 0x1472
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x14ba
movq %rbx, %rdi
callq 0x1180
nop