#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 */

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *);
int /*<<< orphan*/ sock_geterror(char *, char *, int);
int /*<<< orphan*/ stderr;
int sock_recv(int sock, char *buffer, int size, int receiveall, char *errbuf,
              int errbuflen) {
  int nread;
  int totread = 0;
  if (size == 0) {
    {
      fprintf(stderr, "%s\n", "I have been requested to read zero bytes");
      assert(1);
    };
    return 0;
  }
again:
  nread = recv(sock, &(buffer[totread]), size - totread, 0);
  if (nread == -1) {
    sock_geterror("recv(): ", errbuf, errbuflen);
    return -1;
  }
  if (nread == 0) {
    if (errbuf) {
      snprintf(errbuf, errbuflen, "The other host terminated the connection.");
      errbuf[errbuflen - 1] = 0;
    }
    return -1;
  }
  if (!receiveall)
    return nread;
  totread += nread;
  if (totread != size)
    goto again;
  return totread;
}


x86 assembly (clang, -O0):
<sock_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11b0 <sock_recv+0x60>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
lea    0xe6f(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
movl   $0x0,-0x4(%rbp)
jmp    1276 <sock_recv+0x126>
jmp    11b5 <sock_recv+0x65>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x2c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11fc <sock_recv+0xac>
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xe42(%rip),%rdi
call   1070 <sock_geterror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <sock_recv+0x126>
cmpl   $0x0,-0x28(%rbp)
jne    1241 <sock_recv+0xf1>
cmpq   $0x0,-0x20(%rbp)
je     1235 <sock_recv+0xe5>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    0xe17(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <sock_recv+0x126>
cmpl   $0x0,-0x18(%rbp)
jne    1256 <sock_recv+0x106>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1276 <sock_recv+0x126>
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1270 <sock_recv+0x120>
jmp    11b5 <sock_recv+0x65>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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   %edx,%edx
je     11ac <sock_recv+0x5c>
mov    %r8,%r15
mov    %ecx,%r12d
mov    %edx,%ebx
mov    %rsi,%r13
mov    %edi,%ebp
mov    %r9d,0x4(%rsp)
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %r14d,%rax
lea    (%rax,%r13,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %ebp,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     11d6 <sock_recv+0x86>
cmp    $0xffffffff,%eax
je     1200 <sock_recv+0xb0>
test   %r12d,%r12d
je     121a <sock_recv+0xca>
add    %eax,%r14d
cmp    %ebx,%r14d
jne    1180 <sock_recv+0x30>
jmp    121c <sock_recv+0xcc>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
lea    0xe41(%rip),%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1050 <assert@plt>
xor    %ebx,%ebx
jmp    121c <sock_recv+0xcc>
mov    $0xffffffff,%ebx
test   %r15,%r15
je     121c <sock_recv+0xcc>
lea    0xe4f(%rip),%rdx
mov    %r15,%rdi
mov    0x4(%rsp),%ebp
mov    %ebp,%esi
call   1040 <snprintf@plt>
movslq %ebp,%rax
movb   $0x0,-0x1(%rax,%r15,1)
jmp    121c <sock_recv+0xcc>
lea    0xe26(%rip),%rdi
mov    %r15,%rsi
mov    0x4(%rsp),%edx
call   1070 <sock_geterror@plt>
mov    $0xffffffff,%ebx
jmp    121c <sock_recv+0xcc>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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 = [9 x i8] c"recv(): \00"
@.str.3 = [42 x i8] c"The other host terminated the connection.\00"
define i32 @sock_recv(i32 %0, i8* %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
store i32 0, i32* %15
%16 = load i32, i32* %10
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %6
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, 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))
%21 = call i32 @assert(i32 1)
store i32 0, i32* %7
br label %70
22: ; preds = %6
br label %23
23: ; preds = %67, %22
%24 = load i32, i32* %8
%25 = load i8*, i8** %9
%26 = load i32, i32* %15
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32, i32* %10
%30 = load i32, i32* %15
%31 = sub nsw i32 %29, %30
%32 = call i32 @recv(i32 %24, i8* %28, i32 %31, i32 0)
store i32 %32, i32* %14
%33 = load i32, i32* %14
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %23
%36 = load i8*, i8** %12
%37 = load i32, i32* %13
%38 = call i32 @sock_geterror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %36, i32 %37)
store i32 -1, i32* %7
br label %70
39: ; preds = %23
%40 = load i32, i32* %14
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %55
42: ; preds = %39
%43 = load i8*, i8** %12
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %54
45: ; preds = %42
%46 = load i8*, i8** %12
%47 = load i32, i32* %13
%48 = call i32 @snprintf(i8* %46, i32 %47, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%49 = load i8*, i8** %12
%50 = load i32, i32* %13
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
store i8 0, i8* %53
br label %54
54: ; preds = %45, %42
store i32 -1, i32* %7
br label %70
55: ; preds = %39
%56 = load i32, i32* %11
%57 = icmp ne i32 %56, 0
br i1 %57, label %60, label %58
58: ; preds = %55
%59 = load i32, i32* %14
store i32 %59, i32* %7
br label %70
60: ; preds = %55
%61 = load i32, i32* %14
%62 = load i32, i32* %15
%63 = add nsw i32 %62, %61
store i32 %63, i32* %15
%64 = load i32, i32* %15
%65 = load i32, i32* %10
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %68
67: ; preds = %60
br label %23
68: ; preds = %60
%69 = load i32, i32* %15
store i32 %69, i32* %7
br label %70
70: ; preds = %68, %58, %54, %35, %18
%71 = load i32, i32* %7
ret i32 %71
}

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 = [9 x i8] c"recv(): \00"
@.str.3 = [42 x i8] c"The other host terminated the connection.\00"
define i32 @sock_recv(i32 %0, i8* %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = icmp eq i32 %2, 0
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = icmp eq i32 %3, 0
br label %14
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, 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))
%13 = tail call i32 @assert(i32 1)
br label %33
14: ; preds = %8, %30
%15 = phi i32 [ %31, %30 ], [ 0, %8 ]
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = sub nsw i32 %2, %15
%19 = tail call i32 @recv(i32 %0, i8* %17, i32 %18, i32 0)
switch i32 %19, label %29 [
i32 -1, label %20
i32 0, label %22
]
20: ; preds = %14
%21 = tail call i32 @sock_geterror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 %5)
br label %33
22: ; preds = %14
%23 = icmp eq i8* %4, null
br i1 %23, label %33, label %24
24: ; preds = %22
%25 = tail call i32 @snprintf(i8* %4, i32 %5, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%26 = add nsw i32 %5, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %4, i64 %27
store i8 0, i8* %28
br label %33
29: ; preds = %14
br i1 %9, label %33, label %30
30: ; preds = %29
%31 = add nsw i32 %19, %15
%32 = icmp eq i32 %31, %2
br i1 %32, label %33, label %14
33: ; preds = %30, %29, %22, %24, %20, %10
%34 = phi i32 [ 0, %10 ], [ -1, %20 ], [ -1, %24 ], [ -1, %22 ], [ %2, %30 ], [ %19, %29 ]
ret i32 %34
}