// File: flordan[P]ipc_benchmark/named_pipe/consumer.cpp
// Function: main
int main(int argc, char *argv[])
{
    std::cout.precision(17);
    std::cout << "\t\t\t\t\tConsumer running" << std::endl;
    char * pipe_name = argv[1];
    std::cout << "\t\t\t\t\tUsing pipe " << pipe_name << std::endl;

    std::cout << "\t\t\t\t\tSynchronizing with producer" << std::endl;
    int fd;
    ssize_t size;
    char str1[80];

    fd = open(pipe_name,O_RDONLY);
    size = 0;
    while (size == 0) {
        size = read(fd, str1, 80);
    } 
    close(fd); 

    fd = open(pipe_name,O_WRONLY); 
    size = write(fd, "ACK", 4); 
    close(fd); 

    std::cout << "\t\t\t\t\tSynchronized" << std::endl;


    fd = open(pipe_name,O_RDONLY); 
    struct timeval st;
    int max = 0;
    int min = 10000000;
    int total = 0;
    for (int i = 0; i < LATENCY_PROBES; i++)
    {
        size = read(fd, str1, 80);
        gettimeofday(&st, NULL);

        double send;
        std::sscanf(str1, "%lf", &send);
        int send_secs = (int)send;
        int send_usec = ((send - send_secs)*10000000)-1000000;
        send = (double)send_secs+((double)send_usec/(double)1000000);
        double now = (double)st.tv_sec + ((double)st.tv_usec)/(double)1000000;
        double diff = (now - send) * 1000000;

        if (diff < 0)
        {
            diff += 1000000;
        }
        total += diff;
        if (diff > max)
        {
            max = diff;
        }
        if (diff < min)
        {
            min = diff;
        }
    }

    std::cout << "Latency test results" << std::endl;
    std::cout << "Average " << total / LATENCY_PROBES << " us" << std::endl;
    std::cout << "Max time " << max << " us" << std::endl;
    std::cout << "Min time " << min << " us" << std::endl;

    std::cout << "\t\t\t\t\tFinished" << std::endl;
}

// Assembly:
subq $0xb8, %rsp
movl $0x0, 0xb4(%rsp)
movl %edi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq 0x2d90(%rip), %rax      # 0x3fd8
movq (%rax), %rax
movq 0x2d86(%rip), %rdi      # 0x3fd8
addq -0x18(%rax), %rdi
movl $0x11, %esi
callq 0x1670
movq 0x2d71(%rip), %rdi      # 0x3fd8
leaq 0xdb2(%rip), %rsi       # 0x2020
callq 0x1070
movq %rax, %rdi
movq 0x2d4b(%rip), %rsi      # 0x3fc8
callq 0x1080
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0x2d3b(%rip), %rdi      # 0x3fd8
leaq 0xd92(%rip), %rsi       # 0x2036
callq 0x1070
movq %rax, %rdi
movq 0xa0(%rsp), %rsi
callq 0x1070
movq %rax, %rdi
movq 0x2d05(%rip), %rsi      # 0x3fc8
callq 0x1080
movq 0x2d09(%rip), %rdi      # 0x3fd8
leaq 0xd71(%rip), %rsi       # 0x2047
callq 0x1070
movq %rax, %rdi
movq 0x2ce3(%rip), %rsi      # 0x3fc8
callq 0x1080
movq 0xa0(%rsp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1040
movl %eax, 0x9c(%rsp)
movq $0x0, 0x90(%rsp)
cmpq $0x0, 0x90(%rsp)
jne 0x1339
movl 0x9c(%rsp), %edi
leaq 0x40(%rsp), %rsi
movl $0x50, %edx
callq 0x1090
movq %rax, 0x90(%rsp)
jmp 0x130e
movl 0x9c(%rsp), %edi
callq 0x10d0
movq 0xa0(%rsp), %rdi
movl $0x1, %esi
movb $0x0, %al
callq 0x1040
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %edi
leaq 0xcfa(%rip), %rsi       # 0x2068
movl $0x4, %edx
callq 0x1030
movq %rax, 0x90(%rsp)
movl 0x9c(%rsp), %edi
callq 0x10d0
movq 0x2c45(%rip), %rdi      # 0x3fd8
leaq 0xcd2(%rip), %rsi       # 0x206c
callq 0x1070
movq %rax, %rdi
movq 0x2c1f(%rip), %rsi      # 0x3fc8
callq 0x1080
movq 0xa0(%rsp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1040
movl %eax, 0x9c(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0x989680, 0x28(%rsp)   # imm = 0x989680
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
cmpl $0x3e8, 0x20(%rsp)      # imm = 0x3E8
jge 0x154e
movl 0x9c(%rsp), %edi
leaq 0x40(%rsp), %rsi
movl $0x50, %edx
callq 0x1090
movq %rax, 0x90(%rsp)
leaq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10b0
leaq 0x40(%rsp), %rdi
leaq 0xc52(%rip), %rsi       # 0x207e
leaq 0x18(%rsp), %rdx
movb $0x0, %al
callq 0x1060
cvttsd2si 0x18(%rsp), %eax
movl %eax, 0x14(%rsp)
movsd 0x18(%rsp), %xmm0
cvtsi2sdl 0x14(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0xbb6(%rip), %xmm1      # 0x2010
mulsd %xmm1, %xmm0
movsd 0xbb2(%rip), %xmm1      # 0x2018
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x10(%rsp)
cvtsi2sdl 0x14(%rsp), %xmm0
cvtsi2sdl 0x10(%rsp), %xmm1
movsd 0xb82(%rip), %xmm2      # 0x2008
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
cvtsi2sdq 0x30(%rsp), %xmm0
cvtsi2sdq 0x38(%rsp), %xmm1
movsd 0xb5e(%rip), %xmm2      # 0x2008
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
subsd 0x18(%rsp), %xmm0
movsd 0xb3c(%rip), %xmm1      # 0x2008
mulsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
xorps %xmm0, %xmm0
ucomisd (%rsp), %xmm0
jbe 0x14f1
movsd 0xb21(%rip), %xmm0      # 0x2008
addsd (%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd (%rsp), %xmm1
cvtsi2sdl 0x24(%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x24(%rsp)
movsd (%rsp), %xmm0
cvtsi2sdl 0x2c(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1522
cvttsd2si (%rsp), %eax
movl %eax, 0x2c(%rsp)
movsd (%rsp), %xmm1
cvtsi2sdl 0x28(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x153c
cvttsd2si (%rsp), %eax
movl %eax, 0x28(%rsp)
jmp 0x153e
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x13e6
movq 0x2a83(%rip), %rdi      # 0x3fd8
leaq 0xb26(%rip), %rsi       # 0x2082
callq 0x1070
movq %rax, %rdi
movq 0x2a5d(%rip), %rsi      # 0x3fc8
callq 0x1080
movq 0x2a61(%rip), %rdi      # 0x3fd8
leaq 0xb19(%rip), %rsi       # 0x2097
callq 0x1070
movq %rax, %rdi
movl 0x24(%rsp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl %eax, %esi
callq 0x10c0
movq %rax, %rdi
leaq 0xafd(%rip), %rsi       # 0x20a0
callq 0x1070
movq %rax, %rdi
movq 0x2a16(%rip), %rsi      # 0x3fc8
callq 0x1080
movq 0x2a1a(%rip), %rdi      # 0x3fd8
leaq 0xadf(%rip), %rsi       # 0x20a4
callq 0x1070
movq %rax, %rdi
movl 0x2c(%rsp), %esi
callq 0x10c0
movq %rax, %rdi
leaq 0xac0(%rip), %rsi       # 0x20a0
callq 0x1070
movq %rax, %rdi
movq 0x29d9(%rip), %rsi      # 0x3fc8
callq 0x1080
movq 0x29dd(%rip), %rdi      # 0x3fd8
leaq 0xaac(%rip), %rsi       # 0x20ae
callq 0x1070
movq %rax, %rdi
movl 0x28(%rsp), %esi
callq 0x10c0
movq %rax, %rdi
leaq 0xa83(%rip), %rsi       # 0x20a0
callq 0x1070
movq %rax, %rdi
movq 0x299c(%rip), %rsi      # 0x3fc8
callq 0x1080
movq 0x29a0(%rip), %rdi      # 0x3fd8
leaq 0xa79(%rip), %rsi       # 0x20b8
callq 0x1070
movq %rax, %rdi
movq 0x297a(%rip), %rsi      # 0x3fc8
callq 0x1080
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)