#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 */
unsigned char kgetc();
int _read(int fd, const void *buf, int count) {
  int written = 0;
  (void)fd;
  unsigned char *charbuf = (unsigned char *)buf;
  for (; count > 0; --count) {
    written++;
    *charbuf = kgetc();
    if (*charbuf == 0x0Du) {
      *charbuf = '\n';
      break;
    }
    charbuf++;
  }
  return written;
}


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)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1189 <_read+0x79>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <kgetc@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0xd,%eax
jne    116f <_read+0x5f>
mov    -0x20(%rbp),%rax
movb   $0xa,(%rax)
jmp    1189 <_read+0x79>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1131 <_read+0x21>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read>:
push   %r15
push   %r14
push   %rbx
test   %edx,%edx
jle    114d <_read+0x3d>
mov    %rsi,%r14
mov    %edx,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <kgetc@plt>
mov    %al,(%r14,%rbx,1)
cmp    $0xd,%al
je     1151 <_read+0x41>
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    1130 <_read+0x20>
mov    %r15d,%ebx
jmp    1159 <_read+0x49>
xor    %ebx,%ebx
jmp    1159 <_read+0x49>
movb   $0xa,(%r14,%rbx,1)
add    $0x1,%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 i8*
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 = load i8*, i8** %5
store i8* %10, i8** %8
br label %11
11: ; preds = %28, %3
%12 = load i32, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %31
14: ; preds = %11
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
store i32 %16, i32* %7
%17 = call zeroext i8 (...) @kgetc()
%18 = load i8*, i8** %8
store i8 %17, i8* %18
%19 = load i8*, i8** %8
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = icmp eq i32 %21, 13
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = load i8*, i8** %8
store i8 10, i8* %24
br label %31
25: ; preds = %14
%26 = load i8*, i8** %8
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %8
br label %28
28: ; preds = %25
%29 = load i32, i32* %6
%30 = add nsw i32 %29, -1
store i32 %30, i32* %6
br label %11
31: ; preds = %23, %11
%32 = load i32, i32* %7
ret i32 %32
}

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