#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 /*<<< orphan*/ c;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *);
int read(int, char *, int);
void read_pipe(int pipe[2]) {
  char c;
  int ret;
  do {
    ret = read(pipe[0], &c, sizeof(c));
  } while (ret == -1 && errno == EINTR);
  if (ret == -1)
    err(1, "read");
  if (ret == 0)
    errx(1, "pipe closed");
}


x86 assembly (clang, -O0):
<read_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jne    117c <read_pipe+0x4c>
mov    0x2e77(%rip),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    113c <read_pipe+0xc>
cmpl   $0xffffffff,-0x10(%rbp)
jne    11a2 <read_pipe+0x72>
mov    $0x1,%edi
lea    0xe63(%rip),%rsi
call   1050 <err@plt>
cmpl   $0x0,-0x10(%rbp)
jne    11bd <read_pipe+0x8d>
mov    $0x1,%edi
lea    0xe4d(%rip),%rsi
call   1030 <errx@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pipe>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0x7(%rsp),%r14
mov    0x2e99(%rip),%r15
mov    0x2e8a(%rip),%r12
xchg   %ax,%ax
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    116d <read_pipe+0x3d>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1150 <read_pipe+0x20>
cmp    $0xffffffff,%eax
je     1182 <read_pipe+0x52>
test   %eax,%eax
je     119e <read_pipe+0x6e>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe77(%rip),%rsi
mov    $0x1,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <err@plt>
lea    0xe60(%rip),%rsi
mov    $0x1,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <errx@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [12 x i8] c"pipe closed\00"
define void @read_pipe(i32* %0) {
%2 = alloca i32*
%3 = alloca i8
%4 = alloca i32
store i32* %0, i32** %2
br label %5
5: ; preds = %17, %1
%6 = load i32*, i32** %2
%7 = getelementptr inbounds i32, i32* %6, i64 0
%8 = load i32, i32* %7
%9 = call i32 @read(i32 %8, i8* %3, i32 1)
store i32 %9, i32* %4
br label %10
10: ; preds = %5
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br label %17
17: ; preds = %13, %10
%18 = phi i1 [ false, %10 ], [ %16, %13 ]
br i1 %18, label %5, label %19
19: ; preds = %17
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @errx(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [12 x i8] c"pipe closed\00"
define void @read_pipe(i32* nocapture %0) {
%2 = alloca i8
br label %3
3: ; preds = %3, %1
%4 = load i32, i32* %0
%5 = call i32 @read(i32 %4, i8* %2, i32 1)
%6 = icmp eq i32 %5, -1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %3, label %11
11: ; preds = %3
br i1 %6, label %12, label %14
12: ; preds = %11
%13 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
14: ; preds = %11
%15 = icmp eq i32 %5, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %12, %16, %14
ret void
}