#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 */
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ item;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ consume_item(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int produce_item();
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ write(int, int *, int);
int main() {
  int item = 0;
  int pipefd[2];
  pid_t pid;
  if (pipe(pipefd) == -1) {
    printf("pipe: error creating pipe");
    exit(-1);
  }
  pid = fork();
  if (pid == 0) {
    close(pipefd[1]);
    while (1) {
      read(pipefd[0], &item, sizeof(item));
      printf("consumer: %d\n", item);
      consume_item(item);
    }
  } else {
    close(pipefd[0]);
    while (1) {
      item = produce_item();
      write(pipefd[1], &item, sizeof(item));
      printf("producer: %d\n", item);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d0 <main+0x40>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121d <main+0x8d>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    -0x8(%rbp),%esi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1030 <consume_item@plt>
jmp    11ee <main+0x5e>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1090 <produce_item@plt>
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%esi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1225 <main+0x95>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0x4(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     125a <main+0xca>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     120c <main+0x7c>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0x4(%rsp),%r14
lea    0xe54(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1090 <produce_item@plt>
mov    %eax,%ebp
mov    %eax,0x4(%rsp)
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11e0 <main+0x50>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0x4(%rsp),%r14
lea    0xdf9(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0x4(%rsp),%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1030 <consume_item@plt>
jmp    1230 <main+0xa0>
lea    0xd9f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"pipe: error creating pipe\00"
@.str.1 = [14 x i8] c"consumer: %d\0A\00"
@.str.2 = [14 x i8] c"producer: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %2
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = call i64 (...) @fork()
store i64 %12, i64* %4
%13 = load i64, i64* %4
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %11
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
br label %19
19: ; preds = %15, %19
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @read(i32 %21, i32* %2, i32 4)
%23 = load i32, i32* %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %2
%26 = call i32 @consume_item(i32 %25)
br label %19
27: ; preds = %11
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
br label %31
31: ; preds = %27, %31
%32 = call i32 (...) @produce_item()
store i32 %32, i32* %2
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @write(i32 %34, i32* %2, i32 4)
%36 = load i32, i32* %2
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %31
}

LLVM IR (-O2):
@.str = [26 x i8] c"pipe: error creating pipe\00"
@.str.1 = [14 x i8] c"consumer: %d\0A\00"
@.str.2 = [14 x i8] c"producer: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = bitcast [2 x i32]* %2 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %11
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
br label %18
18: ; preds = %18, %14
%19 = load i32, i32* %5
%20 = call i32 @read(i32 %19, i32* %1, i32 4)
%21 = load i32, i32* %1
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i32 @consume_item(i32 %21)
br label %18
24: ; preds = %11
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
br label %28
28: ; preds = %28, %24
%29 = call i32 (...) @produce_item()
store i32 %29, i32* %1
%30 = load i32, i32* %27
%31 = call i32 @write(i32 %30, i32* %1, i32 4)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %28
}