#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*/ bzero(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int read_socket(int sock, int total) {
  char buffer[1024] = {0};
  int bytes_read = 0;
  while (1) {
    bytes_read = recv(sock, buffer, 1024, 0);
    total += bytes_read;
    if (bytes_read <= 0)
      break;
    printf("%s", buffer);
    bzero(buffer, 1024);
  }
  return (total);
}


x86 assembly (clang, -O0):
<read_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
movl   $0x0,-0x414(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x414(%rbp)
jg     11a8 <read_socket+0x68>
jmp    11d1 <read_socket+0x91>
lea    -0x410(%rbp),%rsi
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1060 <bzero@plt>
jmp    116e <read_socket+0x2e>
mov    -0x8(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%ebx
mov    %edi,%r15d
mov    %rsp,%rbp
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
add    %eax,%ebx
test   %eax,%eax
jle    11c0 <read_socket+0x80>
lea    0xe7d(%rip),%r14
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    $0x400,%esi
call   1060 <bzero@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
add    %eax,%ebx
test   %eax,%eax
jg     1190 <read_socket+0x50>
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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