// File: 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
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x236a
movq 0x5c9a(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x2cad(%rip), %rsi      # 0x5004
movb $0x0, %al
callq 0x21b0
movl $0x1, -0x4(%rbp)
jmp 0x2569
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2c95(%rip), %rsi      # 0x500e
callq 0x2160
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x2395
movl $0x1, -0x4(%rbp)
jmp 0x2569
leaq 0x2c75(%rip), %rdi      # 0x5011
leaq 0x2e18(%rip), %rsi      # 0x51bb
callq 0x2160
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x23bf
movl $0x1, -0x4(%rbp)
jmp 0x2569
leaq -0x48(%rbp), %rdi
callq 0x2580
movq $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x2c40(%rip), %rsi      # 0x501b
movb $0x0, %al
callq 0x21b0
movq -0x18(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x28b0
movb %al, -0x6d(%rbp)
jmp 0x23f4
movb -0x6d(%rbp), %al
testb $0x1, %al
jne 0x23fd
jmp 0x2449
movzbl -0x44(%rbp), %eax
cmpl $0x1, %eax
jne 0x2447
movq -0x20(%rbp), %rdi
movq -0x50(%rbp), %rdx
movl -0x48(%rbp), %ecx
leaq 0x2c12(%rip), %rsi      # 0x502a
movb $0x0, %al
callq 0x21b0
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2447
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x25a0
jmp 0x2575
jmp 0x23e2
leaq 0x2be4(%rip), %rdi      # 0x5034
leaq 0x2d64(%rip), %rsi      # 0x51bb
callq 0x2160
movq %rax, -0x78(%rbp)
jmp 0x2462
movq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x2484
movl $0x1, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
jmp 0x2560
movq -0x68(%rbp), %rdi
leaq 0x2bb3(%rip), %rsi      # 0x5042
xorl %eax, %eax
movb %al, -0x79(%rbp)
callq 0x21b0
movb -0x79(%rbp), %al
movq -0x68(%rbp), %rdi
leaq 0x2ba2(%rip), %rsi      # 0x5049
callq 0x21b0
movb -0x79(%rbp), %al
movq -0x68(%rbp), %rdi
leaq 0x2bab(%rip), %rsi      # 0x5065
callq 0x21b0
movb -0x79(%rbp), %al
movq -0x68(%rbp), %rdi
leaq 0x2bb0(%rip), %rsi      # 0x507d
callq 0x21b0
movb -0x79(%rbp), %al
movq -0x68(%rbp), %rdi
leaq 0x2ba7(%rip), %rsi      # 0x5087
callq 0x21b0
movb -0x79(%rbp), %al
movq -0x68(%rbp), %rdi
leaq 0x2ba3(%rip), %rsi      # 0x5096
callq 0x21b0
movb -0x79(%rbp), %al
movq -0x68(%rbp), %rdi
leaq 0x2bac(%rip), %rsi      # 0x50b2
callq 0x21b0
movb -0x79(%rbp), %al
movq -0x68(%rbp), %rdi
leaq 0x2bae(%rip), %rsi      # 0x50c7
callq 0x21b0
movb -0x79(%rbp), %al
movq -0x68(%rbp), %rdi
leaq 0x2bc0(%rip), %rsi      # 0x50ec
callq 0x21b0
movq -0x68(%rbp), %rdi
callq 0x2130
jmp 0x253c
movq -0x20(%rbp), %rdi
callq 0x2130
jmp 0x2547
movq -0x18(%rbp), %rdi
callq 0x2130
jmp 0x2552
movl $0x0, -0x4(%rbp)
movl $0x1, -0x6c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x25a0
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x2200
nop