#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ SIGKILL;
unsigned long long fork();
int /*<<< orphan*/ kill(unsigned long long, int /*<<< orphan*/);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, unsigned long long);
unsigned long long rdtsc();
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  float avg_diff;
  unsigned long long coldstart = 0;
  unsigned long long start = 0;
  unsigned long long end = 0;
  unsigned long long child_pid = 0;
  int n;
  int tochild[2];
  int toparent[2];
  char buf[2];
  char *to_child_data = "p";
  char *to_parent_data = "c";
  pipe(tochild);
  pipe(toparent);
  child_pid = fork();
  if (child_pid != 0) {
    start = rdtsc();
    write(tochild[1], to_child_data, strlen(to_child_data));
  }
  if (child_pid == 0) {
    n = read(tochild[0], buf, 1);
    buf[n] = 0;
    write(toparent[1], to_parent_data, strlen(to_parent_data));
  }
  if (child_pid != 0) {
    n = read(toparent[0], buf, 1);
    end = rdtsc();
  }
  printf("%llu", (end - start));
  kill(child_pid, SIGKILL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    0xe40(%rip),%rax
mov    %rax,-0x58(%rbp)
lea    0xe37(%rip),%rax
mov    %rax,-0x60(%rbp)
lea    -0x44(%rbp),%rdi
call   1070 <pipe@plt>
lea    -0x4c(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1227 <main+0xa7>
mov    $0x0,%al
call   1060 <rdtsc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x58(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1274 <main+0xf4>
mov    -0x44(%rbp),%edi
lea    -0x4e(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    %eax,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
movb   $0x0,-0x4e(%rbp,%rax,1)
mov    -0x48(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmpq   $0x0,-0x38(%rbp)
je     129e <main+0x11e>
mov    -0x4c(%rbp),%edi
lea    -0x4e(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1060 <rdtsc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
sub    -0x28(%rbp),%rsi
lea    0xd57(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%r15
test   %rax,%rax
je     11f8 <main+0x78>
xor    %eax,%eax
call   1060 <rdtsc@plt>
mov    %rax,%r14
mov    0x14(%rsp),%ebp
lea    0xe3e(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x8(%rsp),%edi
lea    0x6(%rsp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
xor    %eax,%eax
call   1060 <rdtsc@plt>
mov    %rax,%rsi
sub    %r14,%rsi
jmp    1233 <main+0xb3>
mov    0x10(%rsp),%edi
lea    0x6(%rsp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cltq
movb   $0x0,0x6(%rsp,%rax,1)
mov    0xc(%rsp),%ebp
lea    0xde5(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %esi,%esi
lea    0xdca(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1090 <kill@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"p\00"
@.str.1 = [2 x i8] c"c\00"
@.str.2 = [5 x i8] c"%llu\00"
@SIGKILL = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca float
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca [2 x i32]
%13 = alloca [2 x i32]
%14 = alloca [2 x i8]
%15 = alloca i8*
%16 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
store i64 0, i64* %10
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %15
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %16
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%20 = call i32 @pipe(i32* %19)
%21 = call i64 (...) @fork()
store i64 %21, i64* %10
%22 = load i64, i64* %10
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %2
%25 = call i64 (...) @rdtsc()
store i64 %25, i64* %8
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%27 = load i32, i32* %26
%28 = load i8*, i8** %15
%29 = load i8*, i8** %15
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @write(i32 %27, i8* %28, i32 %30)
br label %32
32: ; preds = %24, %2
%33 = load i64, i64* %10
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %49
35: ; preds = %32
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds [2 x i8], [2 x i8]* %14, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 1)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [2 x i8], [2 x i8]* %14, i64 0, i64 %41
store i8 0, i8* %42
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 1
%44 = load i32, i32* %43
%45 = load i8*, i8** %16
%46 = load i8*, i8** %16
%47 = call i32 @strlen(i8* %46)
%48 = call i32 @write(i32 %44, i8* %45, i32 %47)
br label %49
49: ; preds = %35, %32
%50 = load i64, i64* %10
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %49
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %13, i64 0, i64 0
%54 = load i32, i32* %53
%55 = getelementptr inbounds [2 x i8], [2 x i8]* %14, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 1)
store i32 %56, i32* %11
%57 = call i64 (...) @rdtsc()
store i64 %57, i64* %9
br label %58
58: ; preds = %52, %49
%59 = load i64, i64* %9
%60 = load i64, i64* %8
%61 = sub i64 %59, %60
%62 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %61)
%63 = load i64, i64* %10
%64 = load i32, i32* @SIGKILL
%65 = call i32 @kill(i64 %63, i32 %64)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"p\00"
@.str.1 = [2 x i8] c"c\00"
@.str.2 = [5 x i8] c"%llu\00"
@SIGKILL = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = alloca [2 x i8]
%6 = bitcast [2 x i32]* %3 to i8*
%7 = bitcast [2 x i32]* %4 to i8*
%8 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %2
%16 = load i32, i32* %9
%17 = call i32 @read(i32 %16, i8* %8, i32 1)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 %18
store i8 0, i8* %19
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @write(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %34
24: ; preds = %2
%25 = call i64 (...) @rdtsc()
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @write(i32 %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %11
%31 = call i32 @read(i32 %30, i8* %8, i32 1)
%32 = call i64 (...) @rdtsc()
%33 = sub i64 %32, %25
br label %34
34: ; preds = %15, %24
%35 = phi i64 [ %33, %24 ], [ 0, %15 ]
%36 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %35)
%37 = load i32, i32* @SIGKILL
%38 = call i32 @kill(i64 %13, i32 %37)
ret i32 0
}