#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*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int recv_all(int sock) {
  int size_recv, total_size = 0;
  char chunk[4096];
  while (1) {
    memset(chunk, 0, 4096);
    if ((size_recv = recv(sock, chunk, 4096, 0)) < 0) {
      break;
    } else {
      total_size += size_recv;
      printf("%s", chunk);
    }
  }
  return total_size;
}


x86 assembly (clang, -O0):
<recv_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    117f <recv_all+0x4f>
jmp    11a0 <recv_all+0x70>
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x1010(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
jmp    1145 <recv_all+0x15>
mov    -0xc(%rbp),%eax
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %edi,%r15d
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     11b2 <recv_all+0x82>
xor    %ebp,%ebp
lea    0xe8d(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
add    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    1180 <recv_all+0x50>
mov    %ebp,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i32 @recv_all(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x i8]
store i32 %0, i32* %2
store i32 0, i32* %4
br label %6
6: ; preds = %1, %20
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 4096)
%9 = load i32, i32* %2
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%11 = call i32 @recv(i32 %9, i8* %10, i32 4096, i32 0)
store i32 %11, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %6
br label %21
14: ; preds = %6
%15 = load i32, i32* %3
%16 = load i32, i32* %4
%17 = add nsw i32 %16, %15
store i32 %17, i32* %4
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%19 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %14
br label %6
21: ; preds = %13
%22 = load i32, i32* %4
ret i32 %22
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i32 @recv_all(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 4096)
%5 = call i32 @recv(i32 %0, i8* %3, i32 4096, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %1, %7
%8 = phi i32 [ %13, %7 ], [ %5, %1 ]
%9 = phi i32 [ %10, %7 ], [ 0, %1 ]
%10 = add nsw i32 %8, %9
%11 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %3)
%12 = call i32 @memset(i8* %3, i32 0, i32 4096)
%13 = call i32 @recv(i32 %0, i8* %3, i32 4096, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %7
15: ; preds = %7, %1
%16 = phi i32 [ 0, %1 ], [ %10, %7 ]
ret i32 %16
}