#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 w_read(int, char *, int);
int w_readn(int fd, char *buf, unsigned int count) {
  if (count == 0) {
    return -1;
  }
  int left = count - 1;
  int readn = 0;
  while (left > 0) {
    int n = w_read(fd, buf + readn, left);
    if (n <= 0) {
      break;
    }
    left -= n;
    readn += n;
  }
  buf[readn] = '\0';
  return readn;
}


x86 assembly (clang, -O0):
<w_readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1138 <w_readn+0x28>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a5 <w_readn+0x95>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1193 <w_readn+0x83>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <w_read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     117a <w_readn+0x6a>
jmp    1193 <w_readn+0x83>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1148 <w_readn+0x38>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<w_readn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
je     1158 <w_readn+0x48>
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%r14d
add    $0xffffffff,%ebp
xor    %ecx,%ecx
nopl   0x0(%rax)
mov    %ecx,%r12d
movslq %ecx,%rbx
test   %ebp,%ebp
jle    1151 <w_readn+0x41>
lea    (%r15,%rbx,1),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
call   1030 <w_read@plt>
sub    %eax,%ebp
lea    (%rbx,%rax,1),%ecx
test   %eax,%eax
jg     1130 <w_readn+0x20>
movb   $0x0,(%r15,%rbx,1)
jmp    115e <w_readn+0x4e>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @w_readn(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %44
14: ; preds = %3
%15 = load i32, i32* %7
%16 = sub i32 %15, 1
store i32 %16, i32* %8
store i32 0, i32* %9
br label %17
17: ; preds = %31, %14
%18 = load i32, i32* %8
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %17
%21 = load i32, i32* %5
%22 = load i8*, i8** %6
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i32, i32* %8
%27 = call i32 @w_read(i32 %21, i8* %25, i32 %26)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %20
br label %38
31: ; preds = %20
%32 = load i32, i32* %10
%33 = load i32, i32* %8
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %8
%35 = load i32, i32* %10
%36 = load i32, i32* %9
%37 = add nsw i32 %36, %35
store i32 %37, i32* %9
br label %17
38: ; preds = %30, %17
%39 = load i8*, i8** %6
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = load i32, i32* %9
store i32 %43, i32* %4
br label %44
44: ; preds = %38, %13
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
define i32 @w_readn(i32 %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %20, label %5
5: ; preds = %3
%6 = add i32 %2, -1
br label %7
7: ; preds = %12, %5
%8 = phi i32 [ %6, %5 ], [ %16, %12 ]
%9 = phi i32 [ 0, %5 ], [ %17, %12 ]
%10 = icmp sgt i32 %8, 0
%11 = sext i32 %9 to i64
br i1 %10, label %12, label %18
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %1, i64 %11
%14 = tail call i32 @w_read(i32 %0, i8* %13, i32 %8)
%15 = icmp slt i32 %14, 1
%16 = sub nsw i32 %8, %14
%17 = add nsw i32 %14, %9
br i1 %15, label %18, label %7
18: ; preds = %12, %7
%19 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 0, i8* %19
br label %20
20: ; preds = %3, %18
%21 = phi i32 [ %9, %18 ], [ -1, %3 ]
ret i32 %21
}