#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 int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int main(void) {
  int fd[2];
  if (pipe(fd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
  }
  if (write(fd[1], "hello", 5) == -1) {
    perror("write");
    exit(EXIT_FAILURE);
  }
  close(fd[1]);
  while (1) {
    char buf;
    ssize_t readRetVal = read(fd[0], &buf, 1);
    if (readRetVal == 0) {
      printf("Read end of file from pipe\n");
      exit(EXIT_SUCCESS);
    } else if (readRetVal == -1) {
      perror("write");
      exit(EXIT_FAILURE);
    } else {
      printf("Read character %c\n", buf);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11ab <main+0x3b>
lea    0xe68(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
lea    0xe50(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11e2 <main+0x72>
lea    0xe3c(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1224 <main+0xb4>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1248 <main+0xd8>
lea    0xdd6(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movsbl -0xd(%rbp),%esi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125f <main+0xef>
jmp    11ea <main+0x7a>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    119f <main+0x1f>
lea    0xe98(%rip),%rdi
jmp    11c0 <main+0x40>
mov    0x14(%rsp),%edi
lea    0xe56(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    11d3 <main+0x53>
lea    0xe46(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
lea    0xf(%rsp),%r14
lea    0xe24(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     11b9 <main+0x39>
test   %eax,%eax
je     121b <main+0x9b>
movsbl 0xf(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f0 <main+0x70>
lea    0xdfd(%rip),%rdi
call   1030 <puts@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"hello\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [28 x i8] c"Read end of file from pipe\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [19 x i8] c"Read character %c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i8
%4 = alloca i32
store i32 0, i32* %1
%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 %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @write(i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
br label %25
25: ; preds = %21, %47
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @read(i32 %27, i8* %3, i32 1)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %25
%36 = load i32, i32* %4
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %35
%43 = load i8, i8* %3
%44 = sext i8 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %42
br label %47
47: ; preds = %46
br label %25
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"hello\00"
@.str.2 = [6 x i8] c"write\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [19 x i8] c"Read character %c\0A\00"
@str = [27 x i8] c"Read end of file from pipe\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i8
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%13 = load i32, i32* %12
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %11
%21 = load i32, i32* %12
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %34, %20
%24 = load i32, i32* %4
%25 = call i32 @read(i32 %24, i8* %2, i32 1)
switch i32 %25, label %34 [
i32 0, label %26
i32 -1, label %30
]
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%28 = load i32, i32* @EXIT_SUCCESS
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %23
%35 = load i8, i8* %2
%36 = sext i8 %35 to i32
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %36)
br label %23
}