#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*/ LOG_ERR;
scalar_t__ Recv(int, void *, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
ssize_t readn(int fd, void *buf, ssize_t len) {
  ssize_t n = 0;
  ssize_t total = 0;
  do {
    if ((n = Recv(fd, buf + total, len - total, 0)) == -1) {
      syslog(LOG_ERR, "readn: %s", strerror(errno));
    }
  } while (n > 0 && (total += n) < len);
  return n <= 0 ? n : total;
}


x86 assembly (clang, -O0):
<readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%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
xor    %ecx,%ecx
call   1050 <Recv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    11a6 <readn+0x76>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
lea    0xe5f(%rip),%rsi
call   1030 <syslog@plt>
jmp    11ab <readn+0x7b>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x2d(%rbp)
jle    11d1 <readn+0xa1>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmp    -0x18(%rbp),%rax
setl   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1153 <readn+0x23>
cmpq   $0x0,-0x20(%rbp)
jg     11f4 <readn+0xc4>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11fc <readn+0xcc>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn>:
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
xor    %ecx,%ecx
call   1050 <Recv@plt>
cmp    $0xffffffffffffffff,%rax
je     1178 <readn+0x48>
test   %rax,%rax
jle    11a8 <readn+0x78>
add    %rax,%rbx
cmp    %r14,%rbx
jl     1150 <readn+0x20>
jmp    11ab <readn+0x7b>
mov    0x2e51(%rip),%rax
mov    (%rax),%ebx
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
lea    0xe6a(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <syslog@plt>
mov    $0xffffffffffffffff,%rbx
jmp    11ab <readn+0x7b>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [10 x i8] c"readn: %s\00"
@errno = global i32 0
define i64 @readn(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* %7
store i64 0, i64* %8
br label %9
9: ; preds = %34, %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 nsw i64 %14, %15
%17 = call i64 @Recv(i32 %10, i8* %13, i64 %16, i32 0)
store i64 %17, i64* %7
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %9
%20 = load i32, i32* @LOG_ERR
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @syslog(i32 %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %19, %9
br label %25
25: ; preds = %24
%26 = load i64, i64* %7
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i64, i64* %7
%30 = load i64, i64* %8
%31 = add nsw i64 %30, %29
store i64 %31, i64* %8
%32 = load i64, i64* %6
%33 = icmp slt i64 %31, %32
br label %34
34: ; preds = %28, %25
%35 = phi i1 [ false, %25 ], [ %33, %28 ]
br i1 %35, label %9, label %36
36: ; preds = %34
%37 = load i64, i64* %7
%38 = icmp sle i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i64, i64* %7
br label %43
41: ; preds = %36
%42 = load i64, i64* %8
br label %43
43: ; preds = %41, %39
%44 = phi i64 [ %40, %39 ], [ %42, %41 ]
ret i64 %44
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [10 x i8] c"readn: %s\00"
@errno = global i32 0
define i64 @readn(i32 %0, i8* %1, i64 %2) {
br label %4
4: ; preds = %17, %3
%5 = phi i64 [ 0, %3 ], [ %18, %17 ]
%6 = getelementptr i8, i8* %1, i64 %5
%7 = sub nsw i64 %2, %5
%8 = tail call i64 @Recv(i32 %0, i8* %6, i64 %7, i32 0)
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %4
%11 = load i32, i32* @LOG_ERR
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 @syslog(i32 %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %20
15: ; preds = %4
%16 = icmp sgt i64 %8, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = add nsw i64 %8, %5
%19 = icmp slt i64 %18, %2
br i1 %19, label %4, label %20
20: ; preds = %15, %17, %10
%21 = phi i64 [ -1, %10 ], [ %8, %15 ], [ %18, %17 ]
ret i64 %21
}