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

/* Variables and functions */
#define EAGAIN 129
#define ECONNRESET 128
int errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ recv(int, void *, size_t, int /*<<< orphan*/);
ssize_t read_wrapper(int fd, void *buf, size_t count) {
  ssize_t bytes = recv(fd, buf, count, 0);
  if (bytes < 0) {
    switch (errno) {
    case EAGAIN:
      return 0;
    case ECONNRESET:
      return -errno;
    default:
      perror("recv");
      return -errno;
    }
  } else if (bytes == 0) {
    return -ECONNRESET;
  } else {
    return bytes;
  }
}


x86 assembly (clang, -O0):
<read_wrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11c8 <read_wrapper+0xa8>
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     1190 <read_wrapper+0x70>
jmp    1170 <read_wrapper+0x50>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
jne    11a6 <read_wrapper+0x86>
jmp    1183 <read_wrapper+0x63>
movq   $0x0,-0x8(%rbp)
jmp    11e8 <read_wrapper+0xc8>
xor    %eax,%eax
mov    0x2e47(%rip),%rcx
sub    (%rcx),%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    11e8 <read_wrapper+0xc8>
lea    0xe53(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
mov    0x2e25(%rip),%rcx
sub    (%rcx),%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    11e8 <read_wrapper+0xc8>
cmpq   $0x0,-0x28(%rbp)
jne    11e0 <read_wrapper+0xc0>
movq   $0xffffffffffffff80,-0x8(%rbp)
ff
jmp    11e8 <read_wrapper+0xc8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_wrapper>:
push   %rbx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     113a <read_wrapper+0x1a>
mov    $0xffffffffffffff80,%rcx
cmove  %rcx,%rax
pop    %rbx
ret
mov    0x2e9f(%rip),%rbx
mov    (%rbx),%eax
cmp    $0x81,%eax
je     115a <read_wrapper+0x3a>
cmp    $0x80,%eax
jne    115e <read_wrapper+0x3e>
mov    $0xffffffffffffff80,%rax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret
lea    0xe9b(%rip),%rdi
call   1040 <perror@plt>
movslq (%rbx),%rax
neg    %rax
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [5 x i8] c"recv\00"
define i64 @read_wrapper(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
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 i64 @recv(i32 %9, i8* %10, i64 %11, i32 0)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %3
%16 = load i32, i32* @errno
switch i32 %16, label %22 [
i32 129, label %17
i32 128, label %18
]
17: ; preds = %15
store i64 0, i64* %4
br label %33
18: ; preds = %15
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
%21 = sext i32 %20 to i64
store i64 %21, i64* %4
br label %33
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
%26 = sext i32 %25 to i64
store i64 %26, i64* %4
br label %33
27: ; preds = %3
%28 = load i64, i64* %8
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
store i64 -128, i64* %4
br label %33
31: ; preds = %27
%32 = load i64, i64* %8
store i64 %32, i64* %4
br label %33
33: ; preds = %31, %30, %22, %18, %17
%34 = load i64, i64* %4
ret i64 %34
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [5 x i8] c"recv\00"
define i64 @read_wrapper(i32 %0, i8* %1, i64 %2) {
%4 = tail call i64 @recv(i32 %0, i8* %1, i64 %2, i32 0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = load i32, i32* @errno
switch i32 %7, label %9 [
i32 129, label %17
i32 128, label %8
]
8: ; preds = %6
br label %17
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
%13 = sext i32 %12 to i64
br label %17
14: ; preds = %3
%15 = icmp eq i64 %4, 0
%16 = select i1 %15, i64 -128, i64 %4
br label %17
17: ; preds = %14, %6, %9, %8
%18 = phi i64 [ %13, %9 ], [ -128, %8 ], [ 0, %6 ], [ %16, %14 ]
ret i64 %18
}