#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 fork();
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;
  int child_pid = 0;
  int n;
  int tochild[2];
  int toparent[2];
  char buf[1025];
  char *to_child_data = "hello son";
  char *to_parent_data = "hello dad";
  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, 1024);
    buf[n] = 0;
    write(toparent[1], to_parent_data, strlen(to_parent_data));
  }
  if (child_pid != 0) {
    n = read(toparent[0], buf, 1024);
    end = rdtsc();
    printf("%d", (end - start));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%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)
movl   $0x0,-0x34(%rbp)
lea    0xe51(%rip),%rax
mov    %rax,-0x458(%rbp)
lea    0xe4d(%rip),%rax
mov    %rax,-0x460(%rbp)
lea    -0x40(%rbp),%rdi
call   1070 <pipe@plt>
lea    -0x48(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     122c <main+0xbc>
mov    $0x0,%al
call   1060 <rdtsc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x46c(%rbp)
mov    -0x458(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x458(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x46c(%rbp),%edi
mov    -0x468(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmpl   $0x0,-0x34(%rbp)
jne    1290 <main+0x120>
mov    -0x40(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x38(%rbp)
movslq -0x38(%rbp),%rax
movb   $0x0,-0x450(%rbp,%rax,1)
mov    -0x44(%rbp),%eax
mov    %eax,-0x47c(%rbp)
mov    -0x460(%rbp),%rax
mov    %rax,-0x478(%rbp)
mov    -0x460(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x47c(%rbp),%edi
mov    -0x478(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmpl   $0x0,-0x34(%rbp)
je     12d0 <main+0x160>
mov    -0x48(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1060 <rdtsc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
sub    -0x28(%rbp),%rsi
lea    0xd49(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
mov    %rsp,%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11ee <main+0x7e>
xor    %eax,%eax
call   1060 <rdtsc@plt>
mov    %rax,%r14
mov    0xc(%rsp),%ebp
lea    0xe53(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    (%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
xor    %eax,%eax
call   1060 <rdtsc@plt>
sub    %r14,%rax
lea    0xe30(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
jmp    1227 <main+0xb7>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x4(%rsp),%ebx
lea    0xdf7(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"hello son\00"
@.str.1 = [10 x i8] c"hello dad\00"
@.str.2 = [3 x i8] c"%d\00"
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 i32
%11 = alloca i32
%12 = alloca [2 x i32]
%13 = alloca [2 x i32]
%14 = alloca [1025 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 i32 0, i32* %10
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %15
store i8* getelementptr inbounds ([10 x i8], [10 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 i32 (...) @fork()
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp ne i32 %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 i32, i32* %10
%34 = icmp eq i32 %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 [1025 x i8], [1025 x i8]* %14, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 1024)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [1025 x i8], [1025 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 i32, i32* %10
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %62
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 [1025 x i8], [1025 x i8]* %14, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 1024)
store i32 %56, i32* %11
%57 = call i64 (...) @rdtsc()
store i64 %57, i64* %9
%58 = load i64, i64* %9
%59 = load i64, i64* %8
%60 = sub i64 %58, %59
%61 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %60)
br label %62
62: ; preds = %52, %49
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"hello son\00"
@.str.1 = [10 x i8] c"hello dad\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = alloca [1025 x i8]
%6 = bitcast [2 x i32]* %3 to i8*
%7 = bitcast [2 x i32]* %4 to i8*
%8 = getelementptr inbounds [1025 x i8], [1025 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 i32 (...) @fork()
%14 = icmp eq i32 %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 1024)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [1025 x i8], [1025 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 ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @write(i32 %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %35
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 ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @write(i32 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %11
%31 = call i32 @read(i32 %30, i8* %8, i32 1024)
%32 = call i64 (...) @rdtsc()
%33 = sub i64 %32, %25
%34 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %33)
br label %35
35: ; preds = %15, %24
ret i32 0
}