#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 */
struct sockaddr {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *);
int recvfrom(int, void *, size_t, int, struct sockaddr *, int /*<<< orphan*/ *);
ssize_t recvfromn(int sockfd, void *buf, size_t len, int flags,
                  struct sockaddr *src_addr, socklen_t *addrlen) {
  size_t nleft;
  ssize_t nread;
  void *ptr;
  ptr = buf;
  nleft = len;
  while (nleft > 0) {
    if ((nread = recvfrom(sockfd, ptr, nleft, flags, src_addr, addrlen)) < 0) {
      if (errno == EINTR)
        nread = 0;
      else if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
        if (0)
          printf("nonblock recvfromn\n");
        break;
      } else
        return (-1);
    } else if (nread == 0)
      break;
    nleft -= nread;
    ptr += nread;
  }
  return (len - nleft);
}


x86 assembly (clang, -O0):
<recvfromn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jbe    1219 <recvfromn+0x109>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
call   1030 <recvfrom@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    11e0 <recvfromn+0xd0>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
jne    1196 <recvfromn+0x86>
movl   $0x0,-0x3c(%rbp)
jmp    11db <recvfromn+0xcb>
mov    0x2e43(%rip),%rax
mov    (%rax),%rax
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%rax
je     11ca <recvfromn+0xba>
mov    0x2e29(%rip),%rax
mov    (%rax),%rax
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%rax
jne    11cf <recvfromn+0xbf>
jmp    1219 <recvfromn+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    1224 <recvfromn+0x114>
jmp    11f4 <recvfromn+0xe4>
cmpl   $0x0,-0x3c(%rbp)
jne    11ef <recvfromn+0xdf>
jmp    1219 <recvfromn+0x109>
jmp    11f4 <recvfromn+0xe4>
movslq -0x3c(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
jmp    113e <recvfromn+0x2e>
mov    -0x18(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvfromn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1181 <recvfromn+0x71>
mov    %r9,%r15
mov    %r8,%r12
mov    %ecx,%r13d
mov    %rsi,%rbp
mov    %edi,%r14d
mov    %rdx,(%rsp)
mov    %rdx,%rbx
jmp    1162 <recvfromn+0x52>
nopl   0x0(%rax,%rax,1)
mov    0x2e99(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2e7d(%rip),%rdx
cmp    (%rdx),%rcx
jne    119e <recvfromn+0x8e>
mov    %eax,%eax
add    %rax,%rbp
sub    %rax,%rbx
je     1185 <recvfromn+0x75>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r13d,%ecx
mov    %r12,%r8
mov    %r15,%r9
call   1030 <recvfrom@plt>
test   %eax,%eax
js     1140 <recvfromn+0x30>
jne    1158 <recvfromn+0x48>
jmp    1187 <recvfromn+0x77>
xor    %ebx,%ebx
jmp    118b <recvfromn+0x7b>
xor    %ebx,%ebx
mov    (%rsp),%rdx
sub    %ebx,%edx
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e1b(%rip),%rax
cmp    (%rax),%rcx
je     1187 <recvfromn+0x77>
mov    0x2e27(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
mov    (%rsp),%rdx
jne    118f <recvfromn+0x7f>
jmp    118b <recvfromn+0x7b>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @recvfromn(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca %struct.sockaddr*
%13 = alloca i32*
%14 = alloca i64
%15 = alloca i32
%16 = alloca i8*
store i32 %0, i32* %8
store i8* %1, i8** %9
store i64 %2, i64* %10
store i32 %3, i32* %11
store %struct.sockaddr* %4, %struct.sockaddr** %12
store i32* %5, i32** %13
%17 = load i8*, i8** %9
store i8* %17, i8** %16
%18 = load i64, i64* %10
store i64 %18, i64* %14
br label %19
19: ; preds = %52, %6
%20 = load i64, i64* %14
%21 = icmp ugt i64 %20, 0
br i1 %21, label %22, label %61
22: ; preds = %19
%23 = load i32, i32* %8
%24 = load i8*, i8** %16
%25 = load i64, i64* %14
%26 = load i32, i32* %11
%27 = load %struct.sockaddr*, %struct.sockaddr** %12
%28 = load i32*, i32** %13
%29 = call i32 @recvfrom(i32 %23, i8* %24, i64 %25, i32 %26, %struct.sockaddr* %27, i32* %28)
store i32 %29, i32* %15
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %47
31: ; preds = %22
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 0, i32* %15
br label %46
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp eq i64 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i64, i64* @errno
%42 = load i64, i64* @EWOULDBLOCK
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40, %36
br label %61
45: ; preds = %40
store i32 -1, i32* %7
br label %66
46: ; preds = %35
br label %52
47: ; preds = %22
%48 = load i32, i32* %15
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %47
br label %61
51: ; preds = %47
br label %52
52: ; preds = %51, %46
%53 = load i32, i32* %15
%54 = sext i32 %53 to i64
%55 = load i64, i64* %14
%56 = sub i64 %55, %54
store i64 %56, i64* %14
%57 = load i32, i32* %15
%58 = load i8*, i8** %16
%59 = sext i32 %57 to i64
%60 = getelementptr i8, i8* %58, i64 %59
store i8* %60, i8** %16
br label %19
61: ; preds = %50, %44, %19
%62 = load i64, i64* %10
%63 = load i64, i64* %14
%64 = sub i64 %62, %63
%65 = trunc i64 %64 to i32
store i32 %65, i32* %7
br label %66
66: ; preds = %61, %45
%67 = load i32, i32* %7
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @recvfromn(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32* %5) {
%7 = icmp eq i64 %2, 0
br i1 %7, label %31, label %8
8: ; preds = %6, %25
%9 = phi i8* [ %29, %25 ], [ %1, %6 ]
%10 = phi i64 [ %28, %25 ], [ %2, %6 ]
%11 = tail call i32 @recvfrom(i32 %0, i8* %9, i64 %10, i32 %3, %struct.sockaddr* %4, i32* %5)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %8
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %25, label %17
17: ; preds = %13
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %14, %18
%20 = load i64, i64* @EWOULDBLOCK
%21 = icmp eq i64 %14, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %31, label %35
23: ; preds = %8
%24 = icmp eq i32 %11, 0
br i1 %24, label %31, label %25
25: ; preds = %13, %23
%26 = phi i32 [ %11, %23 ], [ 0, %13 ]
%27 = zext i32 %26 to i64
%28 = sub i64 %10, %27
%29 = getelementptr i8, i8* %9, i64 %27
%30 = icmp eq i64 %28, 0
br i1 %30, label %31, label %8
31: ; preds = %25, %23, %6, %17
%32 = phi i64 [ %10, %17 ], [ 0, %6 ], [ 0, %25 ], [ %10, %23 ]
%33 = sub i64 %2, %32
%34 = trunc i64 %33 to i32
br label %35
35: ; preds = %17, %31
%36 = phi i32 [ %34, %31 ], [ -1, %17 ]
ret i32 %36
}