#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, unsigned long, long);
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 @%lu %lu from other end %li", done, size, ret);
}


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    11b8 <receive_data+0x98>
jmp    113b <receive_data+0x1b>
cmpq   $0x0,-0x20(%rbp)
jl     119b <receive_data+0x7b>
cmpq   $0x0,-0x18(%rbp)
je     11b8 <receive_data+0x98>
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0x1,%edi
lea    0xe4d(%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,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <read@plt>
test   %rax,%rax
jle    1160 <receive_data+0x40>
add    %rax,%rbx
cmp    %r14,%rbx
jne    1140 <receive_data+0x20>
jmp    118c <receive_data+0x6c>
mov    %rax,%rcx
or     %r14,%rcx
je     118c <receive_data+0x6c>
lea    0xe91(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %rax,%r8
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 = [36 x i8] c"reading @%lu %lu from other end %li\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 %39
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 %39
34: ; preds = %31, %28
%35 = load i64, i64* %8
%36 = load i64, i64* %6
%37 = load i64, i64* %7
%38 = call i32 @err(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %35, i64 %36, i64 %37)
br label %39
39: ; preds = %26, %34, %31
ret void
}

LLVM IR (-O2):
@.str = [36 x i8] c"reading @%lu %lu from other end %li\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 ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %5, i64 %2, i64 %8)
br label %18
18: ; preds = %10, %16, %13
ret void
}