// File: /workspace/github_cpp_makefile_star2_O1/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 0x153e
movq 0x8(%rsi), %rdi
leaq 0x1c9d(%rip), %rsi      # 0x300e
callq 0x1100
movl $0x1, %r15d
testq %rax, %rax
je 0x154c
movq %rax, %rbx
leaq 0x1c82(%rip), %rdi      # 0x3011
leaq 0x2035(%rip), %rsi      # 0x33cb
callq 0x1100
testq %rax, %rax
je 0x154c
movq %rax, %r14
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %r15
movups %xmm0, 0x10(%r15)
movq $0x0, 0x20(%r15)
leaq 0x1c59(%rip), %rdi      # 0x301b
movl $0xe, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x1190
leaq 0x1c4f(%rip), %r12      # 0x302a
xorl %r13d, %r13d
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1578
testb %al, %al
je 0x140b
cmpb $0x1, -0x4c(%rbp)
jne 0x13de
movl -0x50(%rbp), %ecx
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1130
incq %r13
jmp 0x13de
leaq 0x1c22(%rip), %rdi      # 0x3034
leaq 0x1fb2(%rip), %rsi      # 0x33cb
callq 0x1100
testq %rax, %rax
je 0x1528
movq %rax, %r15
leaq 0x1c11(%rip), %rdi      # 0x3042
movl $0x6, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x1190
leaq 0x1bff(%rip), %rdi      # 0x3049
movl $0x1b, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x1190
leaq 0x1c02(%rip), %rdi      # 0x3065
movl $0x17, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x1190
leaq 0x1c01(%rip), %rdi      # 0x307d
movl $0x9, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x1190
leaq 0x1bf2(%rip), %rdi      # 0x3087
movl $0xe, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x1190
leaq 0x1be8(%rip), %rdi      # 0x3096
movl $0x1b, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x1190
leaq 0x1beb(%rip), %rdi      # 0x30b2
movl $0x14, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x1190
leaq 0x1be7(%rip), %rdi      # 0x30c7
movl $0x24, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x1190
leaq 0x1bf3(%rip), %rdi      # 0x30ec
movl $0x52, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x1190
movq %r15, %rdi
callq 0x10d0
movq %r14, %rdi
callq 0x10d0
movq %rbx, %rdi
callq 0x10d0
xorl %r15d, %r15d
jmp 0x152e
movl $0x1, %r15d
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x154c
callq 0x10c0
jmp 0x154c
movq %rsi, %rdi
callq 0x11b0
movl $0x1, %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x156f
callq 0x10c0
movq %rbx, %rdi
callq 0x1160
nop