#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*/ err(int, char *);
long read(int, void *, unsigned long);
void receive_data(int fd, void *mem, unsigned long size) {
  long ret, done = 0;
  while ((ret = read(fd, mem + done, size - done)) > 0) {
    done += ret;
    if (done == size)
      return;
  }
  if (ret < 0 || size != 0)
    err(1, "reading from other end");
}


x86 assembly (clang, -O0):
<receive_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jle    1185 <receive_data+0x65>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1180 <receive_data+0x60>
jmp    11ac <receive_data+0x8c>
jmp    113b <receive_data+0x1b>
cmpq   $0x0,-0x20(%rbp)
jl     119b <receive_data+0x7b>
cmpq   $0x0,-0x18(%rbp)
je     11ac <receive_data+0x8c>
mov    $0x1,%edi
lea    0xe59(%rip),%rsi
call   1040 <err@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <read@plt>
test   %rax,%rax
jle    1161 <receive_data+0x41>
add    %rax,%rbp
cmp    %rbx,%rbp
jne    1140 <receive_data+0x20>
jmp    1181 <receive_data+0x61>
or     %rbx,%rax
je     1181 <receive_data+0x61>
lea    0xe93(%rip),%rsi
mov    $0x1,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <err@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"reading from other end\00"
define void @receive_data(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %8
br label %9
9: ; preds = %27, %3
%10 = load i32, i32* %4
%11 = load i8*, i8** %5
%12 = load i64, i64* %8
%13 = getelementptr i8, i8* %11, i64 %12
%14 = load i64, i64* %6
%15 = load i64, i64* %8
%16 = sub i64 %14, %15
%17 = call i64 @read(i32 %10, i8* %13, i64 %16)
store i64 %17, i64* %7
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %9
%20 = load i64, i64* %7
%21 = load i64, i64* %8
%22 = add nsw i64 %21, %20
store i64 %22, i64* %8
%23 = load i64, i64* %8
%24 = load i64, i64* %6
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %19
br label %36
27: ; preds = %19
br label %9
28: ; preds = %9
%29 = load i64, i64* %7
%30 = icmp slt i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i64, i64* %6
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31, %28
%35 = call i32 @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %26, %34, %31
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"reading from other end\00"
define void @receive_data(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %10, %3
%5 = phi i64 [ 0, %3 ], [ %11, %10 ]
%6 = getelementptr i8, i8* %1, i64 %5
%7 = sub i64 %2, %5
%8 = tail call i64 @read(i32 %0, i8* %6, i64 %7)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = add nsw i64 %8, %5
%12 = icmp eq i64 %11, %2
br i1 %12, label %18, label %4
13: ; preds = %4
%14 = or i64 %8, %2
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %10, %16, %13
ret void
}