#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*/ inbyte();
int read(int fd, char *buf, int len) {
  int _len = len;
  if (fd != 0)
    return (-1);
  while (len-- > 0) {
    *buf++ = inbyte();
    _len--;
  }
  return len;
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     113e <read+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1185 <read+0x75>
jmp    1143 <read+0x33>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    117f <read+0x6f>
mov    $0x0,%al
call   1030 <inbyte@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1143 <read+0x33>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %edi,%edi
jne    114b <read+0x3b>
test   %edx,%edx
jle    1145 <read+0x35>
mov    %rsi,%r15
mov    %edx,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <inbyte@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    1130 <read+0x20>
jmp    114b <read+0x3b>
add    $0xffffffff,%edx
mov    %edx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
store i32 %9, i32* %8
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %27
13: ; preds = %3
br label %14
14: ; preds = %18, %13
%15 = load i32, i32* %7
%16 = add nsw i32 %15, -1
store i32 %16, i32* %7
%17 = icmp sgt i32 %15, 0
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = call i32 (...) @inbyte()
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %6
store i8 %20, i8* %21
%23 = load i32, i32* %8
%24 = add nsw i32 %23, -1
store i32 %24, i32* %8
br label %14
25: ; preds = %14
%26 = load i32, i32* %7
store i32 %26, i32* %4
br label %27
27: ; preds = %25, %12
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
define i32 @read(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = icmp eq i32 %0, 0
br i1 %4, label %5, label %16
5: ; preds = %3
%6 = add nsw i32 %2, -1
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %16
8: ; preds = %5, %8
%9 = phi i32 [ %14, %8 ], [ %6, %5 ]
%10 = phi i8* [ %13, %8 ], [ %1, %5 ]
%11 = tail call i32 (...) @inbyte()
%12 = trunc i32 %11 to i8
%13 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %12, i8* %10
%14 = add nsw i32 %9, -1
%15 = icmp eq i32 %9, 0
br i1 %15, label %16, label %8
16: ; preds = %8, %5, %3
%17 = phi i32 [ -1, %3 ], [ %6, %5 ], [ -1, %8 ]
ret i32 %17
}