#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ memory_clear(char *, size_t);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
size_t read_data(int socket, char *data, size_t size) {
  memory_clear(data, size);
  size_t i = 0;
  char c = '\0';
  while (i < size) {
    ssize_t n = recv(socket, &c, 1, 0);
    if (n > 0) {
      data[i] = c;
      ++i;
    }
  }
  return i;
}


x86 assembly (clang, -O0):
<read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <memory_clear@plt>
movq   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    119b <read_data+0x7b>
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jle    1196 <read_data+0x76>
mov    -0x21(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    114c <read_data+0x2c>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1040 <memory_clear@plt>
movb   $0x0,0xf(%rsp)
xor    %ebp,%ebp
test   %rbx,%rbx
je     118b <read_data+0x6b>
lea    0xf(%rsp),%r12
jmp    1165 <read_data+0x45>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %rbx,%rbp
jae    118b <read_data+0x6b>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jle    1160 <read_data+0x40>
movzbl 0xf(%rsp),%eax
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
jmp    1160 <read_data+0x40>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @read_data(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = call i32 @memory_clear(i8* %10, i64 %11)
store i64 0, i64* %7
store i8 0, i8* %8
br label %13
13: ; preds = %29, %3
%14 = load i64, i64* %7
%15 = load i64, i64* %6
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %30
17: ; preds = %13
%18 = load i32, i32* %4
%19 = call i64 @recv(i32 %18, i8* %8, i32 1, i32 0)
store i64 %19, i64* %9
%20 = load i64, i64* %9
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load i8, i8* %8
%24 = load i8*, i8** %5
%25 = load i64, i64* %7
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 %23, i8* %26
%27 = load i64, i64* %7
%28 = add i64 %27, 1
store i64 %28, i64* %7
br label %29
29: ; preds = %22, %17
br label %13
30: ; preds = %13
%31 = load i64, i64* %7
ret i64 %31
}

LLVM IR (-O2):
define i64 @read_data(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8
%5 = tail call i32 @memory_clear(i8* %1, i64 %2)
store i8 0, i8* %4
%6 = icmp eq i64 %2, 0
br i1 %6, label %18, label %7
7: ; preds = %3, %15
%8 = phi i64 [ %16, %15 ], [ 0, %3 ]
%9 = call i64 @recv(i32 %0, i8* %4, i32 1, i32 0)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i8, i8* %4
%13 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 %12, i8* %13
%14 = add nuw i64 %8, 1
br label %15
15: ; preds = %11, %7
%16 = phi i64 [ %14, %11 ], [ %8, %7 ]
%17 = icmp ult i64 %16, %2
br i1 %17, label %7, label %18
18: ; preds = %15, %3
%19 = phi i64 [ 0, %3 ], [ %16, %15 ]
ret i64 %19
}