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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int MSG_PEEK;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int);
ssize_t client_recv(int server_socket, char *buf, size_t len, int flags) {
  ssize_t read = 0;
  while (1) {
    ssize_t n;
    if ((n = recv(server_socket, buf + read, len - read, flags)) < 0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      perror("client recv");
      exit(1);
    }
    read += n;
    if (!n || flags & MSG_PEEK) {
      break;
    }
  }
  return read;
}


x86 assembly (clang, -O0):
<client_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
mov    -0x1c(%rbp),%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11cb <client_recv+0x9b>
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <client_recv+0x80>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%rax
jne    11b5 <client_recv+0x85>
jmp    114d <client_recv+0x1d>
lea    0xe44(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11f3 <client_recv+0xc3>
mov    -0x1c(%rbp),%eax
mov    0x2e00(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f8 <client_recv+0xc8>
jmp    11fd <client_recv+0xcd>
jmp    114d <client_recv+0x1d>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r13d
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
mov    %edi,%ebp
xor    %ecx,%ecx
mov    0x2e82(%rip),%r15
mov    0x2e73(%rip),%r12
nopl   (%rax)
mov    %ecx,0x4(%rsp)
movslq %ecx,%rax
mov    0x8(%rsp),%rcx
lea    (%rcx,%rax,1),%rbx
mov    0x10(%rsp),%rcx
mov    %ecx,%r14d
sub    %eax,%r14d
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
mov    %r13d,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    11b0 <client_recv+0x80>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1180 <client_recv+0x50>
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
je     1180 <client_recv+0x50>
jmp    11d7 <client_recv+0xa7>
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%ecx
add    %eax,%ecx
test   %eax,%eax
je     11c6 <client_recv+0x96>
mov    0x2e27(%rip),%rax
test   %r13d,(%rax)
je     1160 <client_recv+0x30>
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe22(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [12 x i8] c"client recv\00"
@MSG_PEEK = global i32 0
define i32 @client_recv(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %11
11: ; preds = %4, %33, %49
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = load i32, i32* %9
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i64, i64* %7
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = sub i64 %17, %19
%21 = trunc i64 %20 to i32
%22 = load i32, i32* %8
%23 = call i32 @recv(i32 %12, i8* %16, i32 %21, i32 %22)
store i32 %23, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %11
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%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
br label %11
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %11
%38 = load i32, i32* %10
%39 = load i32, i32* %9
%40 = add nsw i32 %39, %38
store i32 %40, i32* %9
%41 = load i32, i32* %10
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32, i32* %8
%45 = load i32, i32* @MSG_PEEK
%46 = and i32 %44, %45
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43, %37
br label %50
49: ; preds = %43
br label %11
50: ; preds = %48
%51 = load i32, i32* %9
ret i32 %51
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [12 x i8] c"client recv\00"
@MSG_PEEK = global i32 0
define i32 @client_recv(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = trunc i64 %2 to i32
br label %6
6: ; preds = %27, %4
%7 = phi i32 [ %25, %27 ], [ 0, %4 ]
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sub i32 %5, %7
br label %11
11: ; preds = %6, %14
%12 = tail call i32 @recv(i32 %0, i8* %9, i32 %10, i32 %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %15, %18
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %11, label %21
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %11
%25 = add nsw i32 %12, %7
%26 = icmp eq i32 %12, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = load i32, i32* @MSG_PEEK
%29 = and i32 %28, %3
%30 = icmp eq i32 %29, 0
br i1 %30, label %6, label %31
31: ; preds = %27, %24
ret i32 %25
}