void receiver(unsigned int num_packets, int in_fd, int ready_out, int wakefd,
              int id) {
  unsigned int i;
  ready(ready_out, wakefd, id, 2);
  int timeout = 0;
  for (i = 0; i < num_packets; i++) {
    char data[100];
    int ret, done = 0;
  again:
    ret = read(in_fd, data + done, 100 - done);
    if (0)
      printf(1, "recv[%d]: ret = %d. (%d/%d)\n", id, ret, i, num_packets);
    if (ret < 0)
      barf("SERVER: read");
    done += ret;
    if (done < 100) {
      goto again;
    }
    if (0)
      printf(1, "recv[%d]'s task has done. (%d/%d)\n", id, i, num_packets);
    timeout = 0;
  }
}


x86 assembly (clang, -O0):
<receiver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8d,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    $0x2,%ecx
mov    $0x0,%al
call   1040 <ready@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1201 <receiver+0xd1>
movl   $0x0,-0x88(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
movslq -0x88(%rbp),%rax
add    %rax,%rsi
mov    $0x64,%edx
sub    -0x88(%rbp),%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    11c8 <receiver+0x98>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <barf@plt>
mov    -0x84(%rbp),%eax
add    -0x88(%rbp),%eax
mov    %eax,-0x88(%rbp)
cmpl   $0x64,-0x88(%rbp)
jge    11ec <receiver+0xbc>
jmp    1184 <receiver+0x54>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    116e <receiver+0x3e>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receiver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %esi,%r13d
mov    %edi,%r14d
mov    %edx,%edi
mov    %ecx,%esi
mov    %r8d,%edx
mov    $0x2,%ecx
xor    %eax,%eax
call   1040 <ready@plt>
test   %r14d,%r14d
je     11b1 <receiver+0x81>
lea    0xe9d(%rip),%r15
xor    %r12d,%r12d
jmp    1179 <receiver+0x49>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12d
cmp    %r14d,%r12d
je     11b1 <receiver+0x81>
xor    %ebx,%ebx
jmp    1187 <receiver+0x57>
nopl   (%rax)
add    %ebp,%ebx
cmp    $0x64,%ebx
jge    1170 <receiver+0x40>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    $0x64,%edx
sub    %eax,%edx
mov    %r13d,%edi
xor    %eax,%eax
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1180 <receiver+0x50>
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <barf@plt>
jmp    1180 <receiver+0x50>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"SERVER: read\00"
define void @receiver(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [100 x i8]
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = load i32, i32* %8
%17 = load i32, i32* %9
%18 = load i32, i32* %10
%19 = call i32 (i32, i32, i32, i32, ...) bitcast (i32 (...)* @ready to i32 (i32, i32, i32, i32, ...)*)(i32 %16, i32 %17, i32 %18, i32 2)
store i32 0, i32* %12
store i32 0, i32* %11
br label %20
20: ; preds = %46, %5
%21 = load i32, i32* %11
%22 = load i32, i32* %6
%23 = icmp ult i32 %21, %22
br i1 %23, label %24, label %49
24: ; preds = %20
store i32 0, i32* %15
br label %25
25: ; preds = %44, %24
%26 = load i32, i32* %7
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%28 = load i32, i32* %15
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %15
%32 = sub nsw i32 100, %31
%33 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %26, i8* %30, i32 %32)
store i32 %33, i32* %14
%34 = load i32, i32* %14
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %25
%37 = call i32 (i8*, ...) bitcast (i32 (...)* @barf to i32 (i8*, ...)*)(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %36, %25
%39 = load i32, i32* %14
%40 = load i32, i32* %15
%41 = add nsw i32 %40, %39
store i32 %41, i32* %15
%42 = load i32, i32* %15
%43 = icmp slt i32 %42, 100
br i1 %43, label %44, label %45
44: ; preds = %38
br label %25
45: ; preds = %38
store i32 0, i32* %12
br label %46
46: ; preds = %45
%47 = load i32, i32* %11
%48 = add i32 %47, 1
store i32 %48, i32* %11
br label %20
49: ; preds = %20
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"SERVER: read\00"
define void @receiver(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca [100 x i8]
%7 = tail call i32 (i32, i32, i32, i32, ...) bitcast (i32 (...)* @ready to i32 (i32, i32, i32, i32, ...)*)(i32 %2, i32 %3, i32 %4, i32 2)
%8 = icmp eq i32 %0, 0
br i1 %8, label %28, label %9
9: ; preds = %5
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
br label %11
11: ; preds = %9, %25
%12 = phi i32 [ 0, %9 ], [ %26, %25 ]
br label %13
13: ; preds = %22, %11
%14 = phi i32 [ 0, %11 ], [ %23, %22 ]
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 %15
%17 = sub nsw i32 100, %14
%18 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %1, i8* %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 (i8*, ...) bitcast (i32 (...)* @barf to i32 (i8*, ...)*)(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %13
%23 = add nsw i32 %18, %14
%24 = icmp slt i32 %23, 100
br i1 %24, label %13, label %25
25: ; preds = %22
%26 = add nuw i32 %12, 1
%27 = icmp eq i32 %26, %0
br i1 %27, label %28, label %11
28: ; preds = %25, %5
ret void
}