#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);
ssize_t safe_recv(int *socket, void *buffer, size_t length, int flags) {
  ssize_t bytes = recv(*socket, buffer, length, flags);
  if (bytes == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
    perror("recv");
    close(*socket);
    *socket = -1;
  } else if (bytes == -1) {
    bytes = 0;
  } else if (bytes == 0) {
    close(*socket);
    *socket = -1;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<safe_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c4 <safe_recv+0x94>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <safe_recv+0x94>
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <safe_recv+0x94>
lea    0xe5b(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    1203 <safe_recv+0xd3>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11da <safe_recv+0xaa>
movl   $0x0,-0x20(%rbp)
jmp    11fe <safe_recv+0xce>
cmpl   $0x0,-0x20(%rbp)
jne    11f9 <safe_recv+0xc9>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    11fe <safe_recv+0xce>
jmp    1203 <safe_recv+0xd3>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_recv>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%edi
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
je     117d <safe_recv+0x4d>
cmp    $0xffffffff,%ebp
jne    118a <safe_recv+0x5a>
mov    0x2e91(%rip),%rax
mov    (%rax),%rax
mov    0x2e6f(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
je     118a <safe_recv+0x5a>
mov    0x2e71(%rip),%rcx
cmp    (%rcx),%rax
je     118a <safe_recv+0x5a>
lea    0xe8d(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebp
mov    (%rbx),%edi
call   1040 <close@plt>
movl   $0xffffffff,(%rbx)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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