#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;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, size_t);
ssize_t readn_nonblock(int fd, void *vptr, size_t n) {
  size_t nleft;
  ssize_t nread;
  char *ptr;
  ptr = vptr;
  nleft = n;
  while (nleft > 0) {
    if ((nread = read(fd, ptr, nleft)) <= 0) {
      if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) {
        nread = 0;
      } else {
        if (nread < 0) {
          perror("read");
        } else {
          printf("the peer socket is closed\n");
        }
        break;
      }
    }
    nleft -= nread;
    ptr += nread;
  }
  return n - nleft;
}


x86 assembly (clang, -O0):
<readn_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1229 <readn_nonblock+0xf9>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jg     1209 <readn_nonblock+0xd9>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
je     11ca <readn_nonblock+0x9a>
mov    0x2e43(%rip),%rax
mov    (%rax),%rax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
je     11ca <readn_nonblock+0x9a>
mov    0x2e29(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    11d7 <readn_nonblock+0xa7>
movq   $0x0,-0x28(%rbp)
jmp    1204 <readn_nonblock+0xd4>
cmpq   $0x0,-0x28(%rbp)
jge    11f3 <readn_nonblock+0xc3>
lea    0xe17(%rip),%rdi
call   1050 <perror@plt>
jmp    11ff <readn_nonblock+0xcf>
lea    0xe0b(%rip),%rdi
call   1030 <printf@plt>
jmp    1229 <readn_nonblock+0xf9>
jmp    1209 <readn_nonblock+0xd9>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1153 <readn_nonblock+0x23>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn_nonblock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     11b7 <readn_nonblock+0x87>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e91(%rip),%r12
mov    0x2e7a(%rip),%r13
mov    %r14,%r15
jmp    1168 <readn_nonblock+0x38>
xor    %ecx,%ecx
nopl   (%rax)
add    %rcx,%rbx
sub    %rcx,%r15
je     11b7 <readn_nonblock+0x87>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <read@plt>
mov    %rax,%rcx
test   %rax,%rax
jg     1160 <readn_nonblock+0x30>
mov    (%r12),%rdx
cmp    0x0(%r13),%rdx
je     115b <readn_nonblock+0x2b>
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%rdx
je     115b <readn_nonblock+0x2b>
mov    $0x0,%ecx
mov    0x2e21(%rip),%rsi
cmp    (%rsi),%rdx
je     1160 <readn_nonblock+0x30>
test   %rax,%rax
js     11cf <readn_nonblock+0x9f>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
jmp    11ba <readn_nonblock+0x8a>
xor    %r15d,%r15d
sub    %r15,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2a(%rip),%rdi
call   1050 <perror@plt>
jmp    11ba <readn_nonblock+0x8a>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [27 x i8] c"the peer socket is closed\0A\00"
define i64 @readn_nonblock(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %5
store i8* %10, i8** %9
%11 = load i64, i64* %6
store i64 %11, i64* %7
br label %12
12: ; preds = %43, %3
%13 = load i64, i64* %7
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %50
15: ; preds = %12
%16 = load i32, i32* %4
%17 = load i8*, i8** %9
%18 = load i64, i64* %7
%19 = call i64 @read(i32 %16, i8* %17, i64 %18)
store i64 %19, i64* %8
%20 = icmp sle i64 %19, 0
br i1 %20, label %21, label %43
21: ; preds = %15
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @EWOULDBLOCK
%28 = icmp eq i64 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EAGAIN
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29, %25, %21
store i64 0, i64* %8
br label %42
34: ; preds = %29
%35 = load i64, i64* %8
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %41
39: ; preds = %34
%40 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %37
br label %50
42: ; preds = %33
br label %43
43: ; preds = %42, %15
%44 = load i64, i64* %8
%45 = load i64, i64* %7
%46 = sub i64 %45, %44
store i64 %46, i64* %7
%47 = load i64, i64* %8
%48 = load i8*, i8** %9
%49 = getelementptr inbounds i8, i8* %48, i64 %47
store i8* %49, i8** %9
br label %12
50: ; preds = %41, %12
%51 = load i64, i64* %6
%52 = load i64, i64* %7
%53 = sub i64 %51, %52
ret i64 %53
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str = [5 x i8] c"read\00"
@str = [26 x i8] c"the peer socket is closed\00"
define i64 @readn_nonblock(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %31, label %5
5: ; preds = %3, %26
%6 = phi i8* [ %29, %26 ], [ %1, %3 ]
%7 = phi i64 [ %28, %26 ], [ %2, %3 ]
%8 = tail call i64 @read(i32 %0, i8* %6, i64 %7)
%9 = icmp slt i64 %8, 1
br i1 %9, label %10, label %26
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
%14 = load i64, i64* @EWOULDBLOCK
%15 = icmp eq i64 %11, %14
%16 = select i1 %13, i1 true, i1 %15
%17 = load i64, i64* @EAGAIN
%18 = icmp eq i64 %11, %17
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %26, label %20
20: ; preds = %10
%21 = icmp slt i64 %8, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %31
24: ; preds = %20
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %31
26: ; preds = %10, %5
%27 = phi i64 [ %8, %5 ], [ 0, %10 ]
%28 = sub i64 %7, %27
%29 = getelementptr inbounds i8, i8* %6, i64 %27
%30 = icmp eq i64 %28, 0
br i1 %30, label %31, label %5
31: ; preds = %26, %3, %22, %24
%32 = phi i64 [ %7, %22 ], [ %7, %24 ], [ 0, %3 ], [ 0, %26 ]
%33 = sub i64 %2, %32
ret i64 %33
}