#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__ EINTR;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ pcap_snprintf(char *, int, char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sock_geterror(char *, char *, int);
int /*<<< orphan*/ stderr;
int sock_recv(int sock, void *buffer, size_t size, int receiveall, char *errbuf,
              int errbuflen) {
  char *bufp = buffer;
  int remaining;
  ssize_t nread;
  if (size == 0) {
    {
      fprintf(stderr, "%s\n", "I have been requested to read zero bytes");
      assert(1);
    };
    return 0;
  }
  if (size > 2147483647) {
    pcap_snprintf(errbuf, errbuflen,
                  "Can't read more than %u bytes with sock_recv", 2147483647);
    return -1;
  }
  bufp = (char *)buffer;
  remaining = (int)size;
  for (;;) {
    nread = recv(sock, bufp, remaining, 0);
    if (nread == -1) {
      if (errno == EINTR)
        return -3;
      sock_geterror("recv(): ", errbuf, errbuflen);
      return -1;
    }
    if (nread == 0) {
      if (errbuf) {
        pcap_snprintf(errbuf, errbuflen,
                      "The other host terminated the connection.");
      }
      return -1;
    }
    if (!receiveall) {
      return (int)nread;
    }
    bufp += nread;
    remaining -= nread;
    if (remaining == 0)
      return (int)size;
  }
}


x86 assembly (clang, -O0):
<sock_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <sock_recv+0x63>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
lea    0xe6c(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1040 <assert@plt>
movl   $0x0,-0x4(%rbp)
jmp    12db <sock_recv+0x18b>
cmpq   $0x7fffffff,-0x18(%rbp)
7f
jbe    11e7 <sock_recv+0x97>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
lea    0xe5e(%rip),%rdx
mov    $0x7fffffff,%ecx
mov    $0x0,%al
call   1060 <pcap_snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <sock_recv+0x18b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x3c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1259 <sock_recv+0x109>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
jne    123a <sock_recv+0xea>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12db <sock_recv+0x18b>
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
lea    0xe12(%rip),%rdi
call   1070 <sock_geterror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <sock_recv+0x18b>
cmpl   $0x0,-0x40(%rbp)
jne    128f <sock_recv+0x13f>
cmpq   $0x0,-0x28(%rbp)
je     1283 <sock_recv+0x133>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
lea    0xde7(%rip),%rdx
mov    $0x0,%al
call   1060 <pcap_snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <sock_recv+0x18b>
cmpl   $0x0,-0x1c(%rbp)
jne    12a4 <sock_recv+0x154>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12db <sock_recv+0x18b>
mov    -0x40(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%ecx
mov    -0x3c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    12d6 <sock_recv+0x186>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    12db <sock_recv+0x18b>
jmp    11f6 <sock_recv+0xa6>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1190 <sock_recv+0x40>
mov    %r8,%r15
mov    %rdx,%r12
test   $0xffffffff80000000,%rdx
je     11cb <sock_recv+0x7b>
lea    0xeb7(%rip),%rdx
mov    %r15,%rdi
mov    %r9d,%esi
mov    $0x7fffffff,%ecx
xor    %eax,%eax
call   1060 <pcap_snprintf@plt>
mov    $0xffffffff,%r12d
jmp    11b9 <sock_recv+0x69>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
lea    0xe5d(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1040 <assert@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ecx,%r13d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %r9d,0x4(%rsp)
mov    %r12d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     120a <sock_recv+0xba>
cmp    $0xffffffff,%eax
je     122c <sock_recv+0xdc>
test   %r13d,%r13d
je     126a <sock_recv+0x11a>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%r14d
jne    11e0 <sock_recv+0x90>
jmp    11b9 <sock_recv+0x69>
mov    $0xffffffff,%r12d
test   %r15,%r15
je     11b9 <sock_recv+0x69>
lea    0xe47(%rip),%rdx
mov    %r15,%rdi
mov    0x4(%rsp),%esi
xor    %eax,%eax
call   1060 <pcap_snprintf@plt>
jmp    11b9 <sock_recv+0x69>
mov    0x2da5(%rip),%rax
mov    (%rax),%rax
mov    0x2d93(%rip),%rcx
mov    $0xfffffffd,%r12d
cmp    (%rcx),%rax
je     11b9 <sock_recv+0x69>
lea    0xe07(%rip),%rdi
mov    %r15,%rsi
mov    0x4(%rsp),%edx
call   1070 <sock_geterror@plt>
mov    $0xffffffff,%r12d
jmp    11b9 <sock_recv+0x69>
mov    %eax,%r12d
jmp    11b9 <sock_recv+0x69>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [41 x i8] c"I have been requested to read zero bytes\00"
@.str.2 = [45 x i8] c"Can't read more than %u bytes with sock_recv\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [9 x i8] c"recv(): \00"
@.str.4 = [42 x i8] c"The other host terminated the connection.\00"
define i32 @sock_recv(i32 %0, i8* %1, i64 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %8
store i8* %1, i8** %9
store i64 %2, i64* %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
%17 = load i8*, i8** %9
store i8* %17, i8** %14
%18 = load i64, i64* %10
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %6
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @assert(i32 1)
store i32 0, i32* %7
br label %81
24: ; preds = %6
%25 = load i64, i64* %10
%26 = icmp ugt i64 %25, 2147483647
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i8*, i8** %12
%29 = load i32, i32* %13
%30 = call i32 (i8*, i32, i8*, ...) @pcap_snprintf(i8* %28, i32 %29, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 2147483647)
store i32 -1, i32* %7
br label %81
31: ; preds = %24
%32 = load i8*, i8** %9
store i8* %32, i8** %14
%33 = load i64, i64* %10
%34 = trunc i64 %33 to i32
store i32 %34, i32* %15
br label %35
35: ; preds = %80, %31
%36 = load i32, i32* %8
%37 = load i8*, i8** %14
%38 = load i32, i32* %15
%39 = call i32 @recv(i32 %36, i8* %37, i32 %38, i32 0)
store i32 %39, i32* %16
%40 = load i32, i32* %16
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %51
42: ; preds = %35
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -3, i32* %7
br label %81
47: ; preds = %42
%48 = load i8*, i8** %12
%49 = load i32, i32* %13
%50 = call i32 @sock_geterror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %48, i32 %49)
store i32 -1, i32* %7
br label %81
51: ; preds = %35
%52 = load i32, i32* %16
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %62
54: ; preds = %51
%55 = load i8*, i8** %12
%56 = icmp ne i8* %55, null
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = load i8*, i8** %12
%59 = load i32, i32* %13
%60 = call i32 (i8*, i32, i8*, ...) @pcap_snprintf(i8* %58, i32 %59, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %57, %54
store i32 -1, i32* %7
br label %81
62: ; preds = %51
%63 = load i32, i32* %11
%64 = icmp ne i32 %63, 0
br i1 %64, label %67, label %65
65: ; preds = %62
%66 = load i32, i32* %16
store i32 %66, i32* %7
br label %81
67: ; preds = %62
%68 = load i32, i32* %16
%69 = load i8*, i8** %14
%70 = sext i32 %68 to i64
%71 = getelementptr inbounds i8, i8* %69, i64 %70
store i8* %71, i8** %14
%72 = load i32, i32* %16
%73 = load i32, i32* %15
%74 = sub nsw i32 %73, %72
store i32 %74, i32* %15
%75 = load i32, i32* %15
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %67
%78 = load i64, i64* %10
%79 = trunc i64 %78 to i32
store i32 %79, i32* %7
br label %81
80: ; preds = %67
br label %35
81: ; preds = %77, %65, %61, %47, %46, %27, %20
%82 = load i32, i32* %7
ret i32 %82
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [41 x i8] c"I have been requested to read zero bytes\00"
@.str.2 = [45 x i8] c"Can't read more than %u bytes with sock_recv\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [9 x i8] c"recv(): \00"
@.str.4 = [42 x i8] c"The other host terminated the connection.\00"
define i32 @sock_recv(i32 %0, i8* %1, i64 %2, i32 %3, i8* %4, i32 %5) {
%7 = icmp eq i64 %2, 0
br i1 %7, label %8, label %12
8: ; preds = %6
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @assert(i32 1)
br label %39
12: ; preds = %6
%13 = icmp ugt i64 %2, 2147483647
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = tail call i32 (i8*, i32, i8*, ...) @pcap_snprintf(i8* %4, i32 %5, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 2147483647)
br label %39
16: ; preds = %12
%17 = trunc i64 %2 to i32
%18 = icmp eq i32 %3, 0
br label %19
19: ; preds = %34, %16
%20 = phi i8* [ %1, %16 ], [ %36, %34 ]
%21 = phi i32 [ %17, %16 ], [ %37, %34 ]
%22 = tail call i32 @recv(i32 %0, i8* %20, i32 %21, i32 0)
switch i32 %22, label %33 [
i32 -1, label %23
i32 0, label %29
]
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %39, label %27
27: ; preds = %23
%28 = tail call i32 @sock_geterror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %4, i32 %5)
br label %39
29: ; preds = %19
%30 = icmp eq i8* %4, null
br i1 %30, label %39, label %31
31: ; preds = %29
%32 = tail call i32 (i8*, i32, i8*, ...) @pcap_snprintf(i8* %4, i32 %5, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %39
33: ; preds = %19
br i1 %18, label %39, label %34
34: ; preds = %33
%35 = sext i32 %22 to i64
%36 = getelementptr inbounds i8, i8* %20, i64 %35
%37 = sub nsw i32 %21, %22
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %19
39: ; preds = %34, %33, %29, %31, %23, %27, %14, %8
%40 = phi i32 [ 0, %8 ], [ -1, %14 ], [ -1, %27 ], [ -3, %23 ], [ -1, %31 ], [ -1, %29 ], [ %17, %34 ], [ %22, %33 ]
ret i32 %40
}