#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 */

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int recv(int, void *, int, int /*<<< orphan*/);
int recvn(int sd, void *buf, int buf_len, int useless) {
  int n_left = buf_len;
  int n = 0;
  while (n_left > 0) {
    if ((n = recv(sd, buf + (buf_len - n_left), n_left, 0)) < 0) {
      if (errno == EINTR)
        n = 0;
      else
        return -1;
    } else if (n == 0) {
      return 0;
    }
    n_left -= n;
  }
  return buf_len;
}


x86 assembly (clang, -O0):
<recvn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11c6 <recvn+0xb6>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    119b <recvn+0x8b>
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <recvn+0x7a>
movl   $0x0,-0x20(%rbp)
jmp    1196 <recvn+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <recvn+0xbc>
jmp    11b6 <recvn+0xa6>
cmpl   $0x0,-0x20(%rbp)
jne    11b1 <recvn+0xa1>
movl   $0x0,-0x4(%rbp)
jmp    11cc <recvn+0xbc>
jmp    11b6 <recvn+0xa6>
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1132 <recvn+0x22>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
test   %edx,%edx
jle    1175 <recvn+0x65>
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2eb2(%rip),%r12
mov    0x2ea3(%rip),%r13
mov    %ebp,%ebx
jmp    1150 <recvn+0x40>
nopl   0x0(%rax)
mov    (%r12),%rcx
xor    %eax,%eax
cmp    0x0(%r13),%rcx
jne    1170 <recvn+0x60>
sub    %eax,%ebx
jle    1175 <recvn+0x65>
mov    %ebp,%eax
sub    %ebx,%eax
movslq %eax,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1140 <recvn+0x30>
jne    114c <recvn+0x3c>
xor    %ebp,%ebp
jmp    1175 <recvn+0x65>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @recvn(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %8
store i32 %12, i32* %10
store i32 0, i32* %11
br label %13
13: ; preds = %39, %4
%14 = load i32, i32* %10
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %43
16: ; preds = %13
%17 = load i32, i32* %6
%18 = load i8*, i8** %7
%19 = load i32, i32* %8
%20 = load i32, i32* %10
%21 = sub nsw i32 %19, %20
%22 = sext i32 %21 to i64
%23 = getelementptr i8, i8* %18, i64 %22
%24 = load i32, i32* %10
%25 = call i32 @recv(i32 %17, i8* %23, i32 %24, i32 0)
store i32 %25, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %16
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 0, i32* %11
br label %33
32: ; preds = %27
store i32 -1, i32* %5
br label %45
33: ; preds = %31
br label %39
34: ; preds = %16
%35 = load i32, i32* %11
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 0, i32* %5
br label %45
38: ; preds = %34
br label %39
39: ; preds = %38, %33
%40 = load i32, i32* %11
%41 = load i32, i32* %10
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %10
br label %13
43: ; preds = %13
%44 = load i32, i32* %8
store i32 %44, i32* %5
br label %45
45: ; preds = %43, %37, %32
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @recvn(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %23
6: ; preds = %4, %19
%7 = phi i32 [ %21, %19 ], [ %2, %4 ]
%8 = sub nsw i32 %2, %7
%9 = sext i32 %8 to i64
%10 = getelementptr i8, i8* %1, i64 %9
%11 = tail call i32 @recv(i32 %0, i8* %10, i32 %7, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %19, label %23
17: ; preds = %6
%18 = icmp eq i32 %11, 0
br i1 %18, label %23, label %19
19: ; preds = %13, %17
%20 = phi i32 [ %11, %17 ], [ 0, %13 ]
%21 = sub nsw i32 %7, %20
%22 = icmp sgt i32 %21, 0
br i1 %22, label %6, label %23
23: ; preds = %13, %17, %19, %4
%24 = phi i32 [ %2, %4 ], [ %2, %19 ], [ 0, %17 ], [ -1, %13 ]
ret i32 %24
}