#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 /*<<< orphan*/ u_char;
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ packet;

/* Variables and functions */
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ err(int, char *, char const *);
int errno;
int /*<<< orphan*/ errx(int, char *, char const *);
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ packet_check(int /*<<< orphan*/ *, size_t, scalar_t__);
scalar_t__ recv(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
void my_recv(int sock, const char *who, pid_t pid) {
  u_char packet[16 * (1024 * 1024)];
  ssize_t len;
  size_t totlen;
  int error;
  totlen = 0;
  bzero(packet, sizeof(packet));
  while (totlen < 16 * (1024 * 1024)) {
    len = recv(sock, packet + totlen, sizeof(packet) - totlen, 0);
    if (len < 0) {
      errno = error;
      (void)kill(pid, SIGTERM);
      errno = error;
      err(-1, "%s: recv()", who);
    }
    if (len == 0) {
      (void)kill(pid, SIGTERM);
      errx(-1, "%s: recv(): EOF", who);
    }
    if (packet_check(packet, totlen, len) < 0) {
      (void)kill(pid, SIGTERM);
      errx(-1, "%s: recv(): got bad data", who);
    }
    totlen += len;
  }
}


x86 assembly (clang, -O0):
<my_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4000040,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x4000030(%rbp)
lea    -0x4000020(%rbp),%rdi
mov    $0x4000000,%esi
call   1080 <bzero@plt>
cmpq   $0x1000000,-0x4000030(%rbp)
jae    12c0 <my_recv+0x160>
mov    -0x4(%rbp),%edi
lea    -0x4000020(%rbp),%rsi
mov    -0x4000030(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x4000000,%eax
sub    -0x4000030(%rbp),%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x4000028(%rbp)
cmpq   $0x0,-0x4000028(%rbp)
jge    1228 <my_recv+0xc8>
mov    -0x4000034(%rbp),%ecx
mov    0x2de7(%rip),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    -0x4000034(%rbp),%ecx
mov    0x2dc7(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdx
mov    $0xffffffff,%edi
lea    0xddd(%rip),%rsi
call   1060 <err@plt>
cmpq   $0x0,-0x4000028(%rbp)
jne    125c <my_recv+0xfc>
mov    -0x14(%rbp),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    -0x10(%rbp),%rdx
mov    $0xffffffff,%edi
lea    0xdb4(%rip),%rsi
call   1040 <errx@plt>
lea    -0x4000020(%rbp),%rdi
mov    -0x4000030(%rbp),%rsi
mov    -0x4000028(%rbp),%rdx
call   1070 <packet_check@plt>
cmp    $0x0,%rax
jge    12a6 <my_recv+0x146>
mov    -0x14(%rbp),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    -0x10(%rbp),%rdx
mov    $0xffffffff,%edi
lea    0xd7a(%rip),%rsi
call   1040 <errx@plt>
mov    -0x4000028(%rbp),%rax
add    -0x4000030(%rbp),%rax
mov    %rax,-0x4000030(%rbp)
jmp    1191 <my_recv+0x31>
add    $0x4000040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4000008,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%ebp
mov    %rsp,%rdi
mov    $0x4000000,%esi
call   1080 <bzero@plt>
xor    %ebx,%ebx
mov    0x2e59(%rip),%r15
jmp    11de <my_recv+0x7e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %r14d,%edi
call   1050 <kill@plt>
mov    $0xffffffff,%edi
lea    0xe49(%rip),%rsi
mov    %r12,%rdx
call   1060 <err@plt>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1070 <packet_check@plt>
test   %rax,%rax
js     1220 <my_recv+0xc0>
add    %r13,%rbx
cmp    $0x1000000,%rbx
jae    1241 <my_recv+0xe1>
lea    (%rsp,%rbx,4),%rsi
mov    $0x4000000,%edx
sub    %ebx,%edx
mov    %ebp,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%r13
test   %rax,%rax
js     11a0 <my_recv+0x40>
jne    11bf <my_recv+0x5f>
mov    (%r15),%esi
mov    %r14d,%edi
call   1050 <kill@plt>
mov    $0xffffffff,%edi
lea    0xdf8(%rip),%rsi
mov    %r12,%rdx
call   1040 <errx@plt>
jmp    11bf <my_recv+0x5f>
nopl   (%rax)
mov    (%r15),%esi
mov    %r14d,%edi
call   1050 <kill@plt>
mov    $0xffffffff,%edi
lea    0xde4(%rip),%rsi
mov    %r12,%rdx
call   1040 <errx@plt>
jmp    11d2 <my_recv+0x72>
add    $0x4000008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@SIGTERM = global i32 0
@.str = [11 x i8] c"%s: recv()\00"
@.str.1 = [16 x i8] c"%s: recv(): EOF\00"
@.str.2 = [25 x i8] c"%s: recv(): got bad data\00"
define void @my_recv(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [16777216 x i32]
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i64 0, i64* %9
%11 = getelementptr inbounds [16777216 x i32], [16777216 x i32]* %7, i64 0, i64 0
%12 = call i32 @bzero(i32* %11, i32 67108864)
br label %13
13: ; preds = %56, %3
%14 = load i64, i64* %9
%15 = icmp ult i64 %14, 16777216
br i1 %15, label %16, label %60
16: ; preds = %13
%17 = load i32, i32* %4
%18 = getelementptr inbounds [16777216 x i32], [16777216 x i32]* %7, i64 0, i64 0
%19 = load i64, i64* %9
%20 = getelementptr inbounds i32, i32* %18, i64 %19
%21 = load i64, i64* %9
%22 = sub i64 67108864, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @recv(i32 %17, i32* %20, i32 %23, i32 0)
store i64 %24, i64* %8
%25 = load i64, i64* %8
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %16
%28 = load i32, i32* %10
store i32 %28, i32* @errno
%29 = load i32, i32* %6
%30 = load i32, i32* @SIGTERM
%31 = call i32 @kill(i32 %29, i32 %30)
%32 = load i32, i32* %10
store i32 %32, i32* @errno
%33 = load i8*, i8** %5
%34 = call i32 @err(i32 -1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %27, %16
%36 = load i64, i64* %8
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %35
%39 = load i32, i32* %6
%40 = load i32, i32* @SIGTERM
%41 = call i32 @kill(i32 %39, i32 %40)
%42 = load i8*, i8** %5
%43 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %38, %35
%45 = getelementptr inbounds [16777216 x i32], [16777216 x i32]* %7, i64 0, i64 0
%46 = load i64, i64* %9
%47 = load i64, i64* %8
%48 = call i64 @packet_check(i32* %45, i64 %46, i64 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %44
%51 = load i32, i32* %6
%52 = load i32, i32* @SIGTERM
%53 = call i32 @kill(i32 %51, i32 %52)
%54 = load i8*, i8** %5
%55 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %50, %44
%57 = load i64, i64* %8
%58 = load i64, i64* %9
%59 = add i64 %58, %57
store i64 %59, i64* %9
br label %13
60: ; preds = %13
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@SIGTERM = global i32 0
@.str = [11 x i8] c"%s: recv()\00"
@.str.1 = [16 x i8] c"%s: recv(): EOF\00"
@.str.2 = [25 x i8] c"%s: recv(): got bad data\00"
define void @my_recv(i32 %0, i8* %1, i32 %2) {
%4 = alloca [16777216 x i32]
%5 = bitcast [16777216 x i32]* %4 to i8*
%6 = getelementptr inbounds [16777216 x i32], [16777216 x i32]* %4, i64 0, i64 0
%7 = call i32 @bzero(i32* %6, i32 67108864)
br label %8
8: ; preds = %3, %32
%9 = phi i64 [ 0, %3 ], [ %33, %32 ]
%10 = getelementptr inbounds [16777216 x i32], [16777216 x i32]* %4, i64 0, i64 %9
%11 = trunc i64 %9 to i32
%12 = sub nuw nsw i32 67108864, %11
%13 = call i64 @recv(i32 %0, i32* %10, i32 %12, i32 0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %8
%16 = load i32, i32* @SIGTERM
%17 = call i32 @kill(i32 %2, i32 %16)
%18 = call i32 @err(i32 -1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %25
19: ; preds = %8
%20 = icmp eq i64 %13, 0
br i1 %20, label %21, label %25
21: ; preds = %19
%22 = load i32, i32* @SIGTERM
%23 = call i32 @kill(i32 %2, i32 %22)
%24 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %25
25: ; preds = %15, %21, %19
%26 = call i64 @packet_check(i32* %6, i64 %9, i64 %13)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* @SIGTERM
%30 = call i32 @kill(i32 %2, i32 %29)
%31 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %32
32: ; preds = %28, %25
%33 = add i64 %13, %9
%34 = icmp ult i64 %33, 16777216
br i1 %34, label %8, label %35
35: ; preds = %32
ret void
}