#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 */
char getKey();
int /*<<< orphan*/ isBufferEmpty();
signed int read(unsigned int fd, char *buf, int count) {
  signed int bytes = 0;
  if (fd == 0) {
    int i;
    for (i = 0; i < count && !isBufferEmpty(); i++) {
      buf[i] = getKey();
      bytes++;
    }
  }
  return bytes;
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11ad <read+0x8d>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x1d(%rbp)
jge    116d <read+0x4d>
mov    $0x0,%al
call   1040 <isBufferEmpty@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    117d <read+0x5d>
jmp    11a8 <read+0x88>
mov    $0x0,%al
call   1030 <getKey@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    114a <read+0x2a>
jmp    11ad <read+0x8d>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read>:
push   %r15
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %edi,%edi
jne    1162 <read+0x42>
test   %edx,%edx
jle    1162 <read+0x42>
mov    %rsi,%r14
mov    %edx,%r15d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <isBufferEmpty@plt>
test   %eax,%eax
jne    1162 <read+0x42>
xor    %eax,%eax
call   1030 <getKey@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1140 <read+0x20>
mov    %r15d,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @read(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 0, i32* %7
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %34
11: ; preds = %3
store i32 0, i32* %8
br label %12
12: ; preds = %30, %11
%13 = load i32, i32* %8
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = call i32 (...) @isBufferEmpty()
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br label %20
20: ; preds = %16, %12
%21 = phi i1 [ false, %12 ], [ %19, %16 ]
br i1 %21, label %22, label %33
22: ; preds = %20
%23 = call signext i8 (...) @getKey()
%24 = load i8*, i8** %5
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 %23, i8* %27
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %30
30: ; preds = %22
%31 = load i32, i32* %8
%32 = add nsw i32 %31, 1
store i32 %32, i32* %8
br label %12
33: ; preds = %20
br label %34
34: ; preds = %33, %3
%35 = load i32, i32* %7
ret i32 %35
}

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