#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int recv(int, void *, int, int /*<<< orphan*/);
int read_n_bytes(int sd, void *buffer, int n) {
  int dev;
  int done;
  dev = 1;
  done = 0;
  while ((done < n) && (dev != 0)) {
    dev = recv(sd, buffer + done, (n - done), 0);
    if (dev < 0) {
      perror("read_n_bytes recv");
      exit(EXIT_FAILURE);
    }
    done += dev;
  }
  return done;
}


x86 assembly (clang, -O0):
<read_n_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x1d(%rbp)
jge    116b <read_n_bytes+0x3b>
cmpl   $0x0,-0x18(%rbp)
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    117b <read_n_bytes+0x4b>
jmp    11cb <read_n_bytes+0x9b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bd <read_n_bytes+0x8d>
lea    0xe56(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1150 <read_n_bytes+0x20>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_n_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1175 <read_n_bytes+0x45>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1184 <read_n_bytes+0x54>
add    %eax,%ebx
cmp    %ebp,%ebx
jge    1177 <read_n_bytes+0x47>
test   %eax,%eax
jne    1150 <read_n_bytes+0x20>
jmp    1177 <read_n_bytes+0x47>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe75(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"read_n_bytes recv\00"
@EXIT_FAILURE = global i32 0
define i32 @read_n_bytes(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 1, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %34, %3
%10 = load i32, i32* %8
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* %7
%15 = icmp ne i32 %14, 0
br label %16
16: ; preds = %13, %9
%17 = phi i1 [ false, %9 ], [ %15, %13 ]
br i1 %17, label %18, label %38
18: ; preds = %16
%19 = load i32, i32* %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr i8, i8* %20, i64 %22
%24 = load i32, i32* %6
%25 = load i32, i32* %8
%26 = sub nsw i32 %24, %25
%27 = call i32 @recv(i32 %19, i8* %23, i32 %26, i32 0)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %18
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = add nsw i32 %36, %35
store i32 %37, i32* %8
br label %9
38: ; preds = %16
%39 = load i32, i32* %8
ret i32 %39
}

LLVM IR (-O2):
@.str = [18 x i8] c"read_n_bytes recv\00"
@EXIT_FAILURE = global i32 0
define i32 @read_n_bytes(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %9, label %21
5: ; preds = %9
%6 = icmp slt i32 %16, %2
%7 = icmp ne i32 %14, 0
%8 = and i1 %6, %7
br i1 %8, label %9, label %21
9: ; preds = %3, %5
%10 = phi i32 [ %16, %5 ], [ 0, %3 ]
%11 = sext i32 %10 to i64
%12 = getelementptr i8, i8* %1, i64 %11
%13 = sub nsw i32 %2, %10
%14 = tail call i32 @recv(i32 %0, i8* %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
%16 = add nsw i32 %14, %10
br i1 %15, label %17, label %5
17: ; preds = %9
%18 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %5, %3
%22 = phi i32 [ 0, %3 ], [ %16, %5 ]
ret i32 %22
}