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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, size_t);
ssize_t readfd(int *fdp, char *buf, size_t size) {
  ssize_t len, ret = 0;
  len = read(*fdp, buf, size);
  if (len < 0) {
    if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
      perror("read()");
      goto out;
    }
    goto skip;
  }
  if (len == 0) {
    close(*fdp);
    *fdp = -1;
  }
out:
  ret = len;
skip:
  return ret;
}


x86 assembly (clang, -O0):
<readfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11d2 <readfd+0xa2>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
je     11cd <readfd+0x9d>
mov    0x2e51(%rip),%rax
mov    (%rax),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
je     11cd <readfd+0x9d>
mov    0x2e37(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
je     11cd <readfd+0x9d>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
jmp    11f7 <readfd+0xc7>
jmp    11ff <readfd+0xcf>
cmpq   $0x0,-0x20(%rbp)
jne    11f2 <readfd+0xc2>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    -0x8(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    11f7 <readfd+0xc7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfd>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%edi
call   1040 <read@plt>
mov    %rax,%r14
test   %rax,%rax
js     115c <readfd+0x2c>
mov    %r14,%rax
jne    118c <readfd+0x5c>
mov    (%rbx),%edi
call   1030 <close@plt>
movl   $0xffffffff,(%rbx)
xor    %eax,%eax
jmp    118c <readfd+0x5c>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rcx
mov    0x2e63(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     118c <readfd+0x5c>
mov    0x2e45(%rip),%rdx
cmp    (%rdx),%rcx
je     118c <readfd+0x5c>
mov    0x2e51(%rip),%rdx
cmp    (%rdx),%rcx
jne    1194 <readfd+0x64>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe65(%rip),%rdi
call   1050 <perror@plt>
mov    %r14,%rax
jmp    118c <readfd+0x5c>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [7 x i8] c"read()\00"
define i64 @readfd(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %8
%9 = load i32*, i32** %4
%10 = load i32, i32* %9
%11 = load i8*, i8** %5
%12 = load i64, i64* %6
%13 = call i64 @read(i32 %10, i8* %11, i64 %12)
store i64 %13, i64* %7
%14 = load i64, i64* %7
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %3
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EWOULDBLOCK
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %40
30: ; preds = %24, %20, %16
br label %42
31: ; preds = %3
%32 = load i64, i64* %7
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i32*, i32** %4
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = load i32*, i32** %4
store i32 -1, i32* %38
br label %39
39: ; preds = %34, %31
br label %40
40: ; preds = %39, %28
%41 = load i64, i64* %7
store i64 %41, i64* %8
br label %42
42: ; preds = %40, %30
%43 = load i64, i64* %8
ret i64 %43
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [7 x i8] c"read()\00"
define i64 @readfd(i32* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* %0
%5 = tail call i64 @read(i32 %4, i8* %1, i64 %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %19
7: ; preds = %3
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %8, %11
%13 = select i1 %10, i1 true, i1 %12
%14 = load i64, i64* @EWOULDBLOCK
%15 = icmp eq i64 %8, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %24, label %17
17: ; preds = %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %24
19: ; preds = %3
%20 = icmp eq i64 %5, 0
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = load i32, i32* %0
%23 = tail call i32 @close(i32 %22)
store i32 -1, i32* %0
br label %24
24: ; preds = %17, %21, %19, %7
%25 = phi i64 [ 0, %7 ], [ %5, %19 ], [ 0, %21 ], [ %5, %17 ]
ret i64 %25
}