#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*/ errExit(char *);
int /*<<< orphan*/ exit(int);
int pipe(int *);
int /*<<< orphan*/ printf(char *, unsigned long long const, ...);
int read(int, char *, int);
unsigned long long const time_ns();
int write(int, char *, int);
void measure_self_pipe(int num_iterations) {
  int pfd[2];
  char writebuf[2];
  char readbuf[2];
  if (pipe(pfd) == -1) {
    errExit("pipe");
  }
  writebuf[0] = 'j';
  if (write(pfd[1], writebuf, 1) != 1) {
    errExit("write");
  }
  if (read(pfd[0], readbuf, 1) != 1) {
    errExit("read");
  }
  if (readbuf[0] != 'j') {
    printf("Boo, got %c back from the pipe\n", readbuf[0]);
    exit(1);
  }
  const long long unsigned t1 = time_ns();
  for (int i = 0; i < num_iterations; ++i) {
    if (write(pfd[1], writebuf, 1) != 1) {
      errExit("write");
    }
    if (read(pfd[0], readbuf, 1) != 1) {
      errExit("read");
    }
  }
  const long long unsigned elapsed = time_ns() - t1;
  printf("measure_self_pipe: %llu ns for %d iterations (%.2lf ns / iter)\n",
         elapsed, num_iterations, (double)elapsed / num_iterations);
}


x86 assembly (clang, -O0):
<measure_self_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1199 <measure_self_pipe+0x29>
lea    0xe8c(%rip),%rdi
call   1040 <errExit@plt>
movb   $0x6a,-0xe(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11c3 <measure_self_pipe+0x53>
lea    0xe67(%rip),%rdi
call   1040 <errExit@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
je     11e9 <measure_self_pipe+0x79>
lea    0xe47(%rip),%rdi
call   1040 <errExit@plt>
movsbl -0x10(%rbp),%eax
cmp    $0x6a,%eax
je     1213 <measure_self_pipe+0xa3>
movsbq -0x10(%rbp),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <time_ns@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1290 <measure_self_pipe+0x120>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1257 <measure_self_pipe+0xe7>
lea    0xdd3(%rip),%rdi
call   1040 <errExit@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
je     127d <measure_self_pipe+0x10d>
lea    0xdb3(%rip),%rdi
call   1040 <errExit@plt>
jmp    1282 <measure_self_pipe+0x112>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1225 <measure_self_pipe+0xb5>
xor    %eax,%eax
call   1090 <time_ns@plt>
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edx
movq   %rsi,%xmm0
movaps 0xd4b(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xd4f(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
cvtsi2sdl -0x4(%rbp),%xmm1
divsd  %xmm1,%xmm0
lea    0xd70(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_self_pipe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    119c <measure_self_pipe+0x2c>
lea    0xe89(%rip),%rdi
call   1040 <errExit@plt>
movb   $0x6a,0x6(%rsp)
mov    0xc(%rsp),%edi
lea    0x6(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11c5 <measure_self_pipe+0x55>
lea    0xe65(%rip),%rdi
call   1040 <errExit@plt>
mov    0x8(%rsp),%edi
lea    0x4(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
je     11e9 <measure_self_pipe+0x79>
lea    0xe47(%rip),%rdi
call   1040 <errExit@plt>
movsbq 0x4(%rsp),%rsi
cmp    $0x6a,%rsi
jne    12d0 <measure_self_pipe+0x160>
xor    %eax,%eax
call   1090 <time_ns@plt>
mov    %rax,0x10(%rsp)
test   %r14d,%r14d
jle    1274 <measure_self_pipe+0x104>
lea    0x6(%rsp),%rbx
lea    0x4(%rsp),%rbp
lea    0xe10(%rip),%r12
lea    0xe03(%rip),%r13
mov    %r14d,%r15d
jmp    1236 <measure_self_pipe+0xc6>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%r15d
je     1274 <measure_self_pipe+0x104>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1254 <measure_self_pipe+0xe4>
mov    %r13,%rdi
call   1040 <errExit@plt>
mov    0x8(%rsp),%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
je     1230 <measure_self_pipe+0xc0>
mov    %r12,%rdi
call   1040 <errExit@plt>
jmp    1230 <measure_self_pipe+0xc0>
xor    %eax,%eax
call   1090 <time_ns@plt>
sub    0x10(%rsp),%rax
movq   %rax,%xmm1
punpckldq 0xd73(%rip),%xmm1
subpd  0xd7b(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %r14d,%xmm1
divsd  %xmm1,%xmm0
lea    0xd9c(%rip),%rdi
mov    %rax,%rsi
mov    %r14d,%edx
mov    $0x1,%al
call   1050 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd59(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [32 x i8] c"Boo, got %c back from the pipe\0A\00"
@.str.4 = [64 x i8] c"measure_self_pipe: %llu ns for %d iterations (%.2lf ns / iter)\0A\00"
define void @measure_self_pipe(i32 %0) {
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca [2 x i8]
%5 = alloca [2 x i8]
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
store i32 %0, i32* %2
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @errExit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %1
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
store i8 106, i8* %15
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%19 = call i32 @write(i32 %17, i8* %18, i32 1)
%20 = icmp ne i32 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @errExit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %14
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 1)
%28 = icmp ne i32 %27, 1
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @errExit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 106
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i64
%40 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %31
%43 = call i64 (...) @time_ns()
store i64 %43, i64* %6
store i32 0, i32* %7
br label %44
44: ; preds = %65, %42
%45 = load i32, i32* %7
%46 = load i32, i32* %2
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %68
48: ; preds = %44
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%50 = load i32, i32* %49
%51 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%52 = call i32 @write(i32 %50, i8* %51, i32 1)
%53 = icmp ne i32 %52, 1
br i1 %53, label %54, label %56
54: ; preds = %48
%55 = call i32 @errExit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %56
56: ; preds = %54, %48
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%58 = load i32, i32* %57
%59 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%60 = call i32 @read(i32 %58, i8* %59, i32 1)
%61 = icmp ne i32 %60, 1
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = call i32 @errExit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %62, %56
br label %65
65: ; preds = %64
%66 = load i32, i32* %7
%67 = add nsw i32 %66, 1
store i32 %67, i32* %7
br label %44
68: ; preds = %44
%69 = call i64 (...) @time_ns()
%70 = load i64, i64* %6
%71 = sub i64 %69, %70
store i64 %71, i64* %8
%72 = load i64, i64* %8
%73 = load i32, i32* %2
%74 = load i64, i64* %8
%75 = uitofp i64 %74 to double
%76 = load i32, i32* %2
%77 = sitofp i32 %76 to double
%78 = fdiv double %75, %77
%79 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0), i64 %72, i32 %73, double %78)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [32 x i8] c"Boo, got %c back from the pipe\0A\00"
@.str.4 = [64 x i8] c"measure_self_pipe: %llu ns for %d iterations (%.2lf ns / iter)\0A\00"
define void @measure_self_pipe(i32 %0) {
%2 = alloca [2 x i32]
%3 = alloca [2 x i8]
%4 = alloca [2 x i8]
%5 = bitcast [2 x i32]* %2 to i8*
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @errExit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
store i8 106, i8* %6
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @write(i32 %15, i8* %6, i32 1)
%17 = icmp eq i32 %16, 1
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = call i32 @errExit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = load i32, i32* %8
%22 = call i32 @read(i32 %21, i8* %7, i32 1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @errExit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = load i8, i8* %7
%28 = icmp eq i8 %27, 106
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = sext i8 %27 to i64
%31 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = call i64 (...) @time_ns()
%35 = icmp sgt i32 %0, 0
br i1 %35, label %43, label %36
36: ; preds = %56, %33
%37 = call i64 (...) @time_ns()
%38 = sub i64 %37, %34
%39 = uitofp i64 %38 to double
%40 = sitofp i32 %0 to double
%41 = fdiv double %39, %40
%42 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0), i64 %38, i32 %0, double %41)
ret void
43: ; preds = %33, %56
%44 = phi i32 [ %57, %56 ], [ 0, %33 ]
%45 = load i32, i32* %14
%46 = call i32 @write(i32 %45, i8* %6, i32 1)
%47 = icmp eq i32 %46, 1
br i1 %47, label %50, label %48
48: ; preds = %43
%49 = call i32 @errExit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
%51 = load i32, i32* %8
%52 = call i32 @read(i32 %51, i8* %7, i32 1)
%53 = icmp eq i32 %52, 1
br i1 %53, label %56, label %54
54: ; preds = %50
%55 = call i32 @errExit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %50, %54
%57 = add nuw nsw i32 %44, 1
%58 = icmp eq i32 %57, %0
br i1 %58, label %36, label %43
}