// File: flordan[P]ipc_benchmark/named_pipe/producer.cpp
// Function: main
int main(int argc, char *argv[])
{
    std::cout << "Producer running" << std::endl;
    char * pipe_name = argv[1];
    std::cout << "Using pipe " << pipe_name << std::endl;

    std::cout << "Synchronizing with consumer" << std::endl;
    int fd;
    ssize_t size;
    char str1[80];

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

    fd = open(pipe_name, O_RDONLY);
    size = read(fd, str1, sizeof(str1));
    std::cout << str1 <<std::endl;
    close(fd);

    std::cout << "Synchronized" << std::endl;

    fd = open(pipe_name, O_WRONLY);
    struct timeval st;
    std::cout << "Latency test" << std::endl;
    for (int i = 0; i < LATENCY_PROBES; i++)
    {
        gettimeofday(&st, NULL);
        std::ostringstream os ;
        os << st.tv_sec << "." << st.tv_usec+1000000;
        std::string mystr = os.str();
        size = write(fd, mystr.c_str(), mystr.length());
        usleep(250000);
    }
    std::cout << "Finished" << std::endl;
}

// Assembly:
subq $0x278, %rsp            # imm = 0x278
movl $0x0, 0x274(%rsp)
movl %edi, 0x270(%rsp)
movq %rsi, 0x268(%rsp)
movq 0x2d20(%rip), %rdi      # 0x3fd8
leaq 0xd45(%rip), %rsi       # 0x2004
callq 0x1090
movq %rax, %rdi
movq 0x2cfa(%rip), %rsi      # 0x3fc8
callq 0x10b0
movq 0x268(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x260(%rsp)
movq 0x2cea(%rip), %rdi      # 0x3fd8
leaq 0xd20(%rip), %rsi       # 0x2015
callq 0x1090
movq %rax, %rdi
movq 0x260(%rsp), %rsi
callq 0x1090
movq %rax, %rdi
movq 0x2cb4(%rip), %rsi      # 0x3fc8
callq 0x10b0
movq 0x2cb8(%rip), %rdi      # 0x3fd8
leaq 0xcfa(%rip), %rsi       # 0x2021
callq 0x1090
movq %rax, %rdi
movq 0x2c92(%rip), %rsi      # 0x3fc8
callq 0x10b0
movq 0x260(%rsp), %rdi
movl $0x1, %esi
movb $0x0, %al
callq 0x1070
movl %eax, 0x25c(%rsp)
movl 0x25c(%rsp), %edi
leaq 0xcd9(%rip), %rsi       # 0x203d
movl $0x4, %edx
callq 0x1040
movq %rax, 0x250(%rsp)
movl 0x25c(%rsp), %edi
callq 0x1140
movq 0x260(%rsp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1070
movl %eax, 0x25c(%rsp)
movl 0x25c(%rsp), %edi
leaq 0x200(%rsp), %rsi
movl $0x50, %edx
callq 0x10e0
movq %rax, 0x250(%rsp)
leaq 0x200(%rsp), %rsi
movq 0x2c0e(%rip), %rdi      # 0x3fd8
callq 0x1090
movq %rax, %rdi
movq 0x2bef(%rip), %rsi      # 0x3fc8
callq 0x10b0
movl 0x25c(%rsp), %edi
callq 0x1140
movq 0x2be7(%rip), %rdi      # 0x3fd8
leaq 0xc49(%rip), %rsi       # 0x2041
callq 0x1090
movq %rax, %rdi
movq 0x2bc1(%rip), %rsi      # 0x3fc8
callq 0x10b0
movq 0x260(%rsp), %rdi
movl $0x1, %esi
movb $0x0, %al
callq 0x1070
movl %eax, 0x25c(%rsp)
movq 0x2baa(%rip), %rdi      # 0x3fd8
leaq 0xc19(%rip), %rsi       # 0x204e
callq 0x1090
movq %rax, %rdi
movq 0x2b84(%rip), %rsi      # 0x3fc8
callq 0x10b0
movl $0x0, 0x1ec(%rsp)
cmpl $0x3e8, 0x1ec(%rsp) # imm = 0x3E8
jge 0x159a
xorl %eax, %eax
movl %eax, %esi
leaq 0x1f0(%rsp), %rdi
callq 0x1110
leaq 0x70(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x10c0
movq 0x30(%rsp), %rdi
movq 0x1f0(%rsp), %rsi
callq 0x1130
movq %rax, 0x38(%rsp)
jmp 0x149e
movq 0x38(%rsp), %rdi
leaq 0xbb1(%rip), %rsi       # 0x205b
callq 0x1090
movq %rax, 0x28(%rsp)
jmp 0x14b6
movq 0x28(%rsp), %rdi
movq 0x1f8(%rsp), %rsi
addq $0xf4240, %rsi          # imm = 0xF4240
callq 0x1130
jmp 0x14d1
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x10a0
jmp 0x14e2
movl 0x25c(%rsp), %eax
movl %eax, 0x14(%rsp)
leaq 0x40(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x1050
movq 0x8(%rsp), %rdi
movq %rax, 0x18(%rsp)
callq 0x10f0
movl 0x14(%rsp), %edi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
callq 0x1040
movq %rax, 0x20(%rsp)
jmp 0x1523
movq 0x20(%rsp), %rax
movq %rax, 0x250(%rsp)
movl $0x3d090, %edi          # imm = 0x3D090
callq 0x10d0
jmp 0x153c
leaq 0x40(%rsp), %rdi
callq 0x1060
leaq 0x70(%rsp), %rdi
callq 0x1030
movl 0x1ec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1ec(%rsp)
jmp 0x1454
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
jmp 0x158e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x1060
leaq 0x70(%rsp), %rdi
callq 0x1030
jmp 0x15cb
movq 0x2a37(%rip), %rdi      # 0x3fd8
leaq 0xab5(%rip), %rsi       # 0x205d
callq 0x1090
movq %rax, %rdi
movq 0x2a11(%rip), %rsi      # 0x3fc8
callq 0x10b0
movl 0x274(%rsp), %eax
addq $0x278, %rsp            # imm = 0x278
retq
movq 0x68(%rsp), %rdi
callq 0x1120