#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__ EWOULDBLOCK;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int recv(int, void *, size_t, int /*<<< orphan*/);
ssize_t sock_recv(int sockfd, void *buf, size_t len) {
  ssize_t rbytes;
  rbytes = recv(sockfd, buf, len, 0);
  if (rbytes == 0) {
    perror("recv EOF");
    close(sockfd);
    return -1;
  } else if (rbytes == -1) {
    if (errno == EAGAIN || errno == EWOULDBLOCK) {
      perror("recv EAGAIN");
      return -1;
    } else {
      perror("recv OTHER");
      return -2;
    }
  }
  return rbytes;
}


x86 assembly (clang, -O0):
<sock_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1182 <sock_recv+0x52>
lea    0xe97(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <sock_recv+0xcb>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f0 <sock_recv+0xc0>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
je     11c0 <sock_recv+0x90>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <sock_recv+0xa8>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <sock_recv+0xcb>
lea    0xe36(%rip),%rdi
call   1050 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11fb <sock_recv+0xcb>
jmp    11f5 <sock_recv+0xc5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_recv>:
push   %rbx
mov    %edi,%ebx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1145 <sock_recv+0x15>
test   %eax,%eax
je     117a <sock_recv+0x4a>
pop    %rbx
ret
mov    0x2e94(%rip),%rax
mov    (%rax),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
je     118f <sock_recv+0x5f>
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%rax
je     118f <sock_recv+0x5f>
lea    0xea7(%rip),%rdi
call   1050 <perror@plt>
mov    $0xfffffffe,%eax
pop    %rbx
ret
lea    0xe7f(%rip),%rdi
call   1050 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    119b <sock_recv+0x6b>
lea    0xe73(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

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

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