#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 size_t ssize_t;

/* Variables and functions */
size_t read(int, char *, int);
ssize_t readbuffer(int fd, void *buf, size_t count) {
  ssize_t n;
  ssize_t rc;
  char letter;
  char *buffer;
  buffer = buf;
  for (n = 1; n < count; n++) {
    if ((rc = read(fd, &letter, 1)) == 1) {
      *buffer++ = letter;
      if (letter == '\n')
        break;
    }
  }
  *buffer = 0;
  return n;
}


x86 assembly (clang, -O0):
<readbuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11a1 <readbuffer+0x91>
mov    -0x4(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x1,%rax
jne    118b <readbuffer+0x7b>
mov    -0x29(%rbp),%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    1186 <readbuffer+0x76>
jmp    11a1 <readbuffer+0x91>
jmp    118b <readbuffer+0x7b>
jmp    1190 <readbuffer+0x80>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1133 <readbuffer+0x23>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readbuffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    $0x1,%ebx
cmp    $0x2,%rdx
jb     1176 <readbuffer+0x66>
mov    %rdx,%r15
mov    %edi,%ebp
mov    $0x1,%ebx
lea    0xf(%rsp),%r12
jmp    1149 <readbuffer+0x39>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%r15
je     1173 <readbuffer+0x63>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%rax
jne    1140 <readbuffer+0x30>
movzbl 0xf(%rsp),%eax
mov    %al,(%r14)
add    $0x1,%r14
cmpb   $0xa,0xf(%rsp)
jne    1140 <readbuffer+0x30>
jmp    1176 <readbuffer+0x66>
mov    %r15,%rbx
movb   $0x0,(%r14)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @readbuffer(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8
%10 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i8*, i8** %5
store i8* %11, i8** %10
store i64 1, i64* %7
br label %12
12: ; preds = %30, %3
%13 = load i64, i64* %7
%14 = load i64, i64* %6
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %33
16: ; preds = %12
%17 = load i32, i32* %4
%18 = call i64 @read(i32 %17, i8* %9, i32 1)
store i64 %18, i64* %8
%19 = icmp eq i64 %18, 1
br i1 %19, label %20, label %29
20: ; preds = %16
%21 = load i8, i8* %9
%22 = load i8*, i8** %10
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %10
store i8 %21, i8* %22
%24 = load i8, i8* %9
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %28
27: ; preds = %20
br label %33
28: ; preds = %20
br label %29
29: ; preds = %28, %16
br label %30
30: ; preds = %29
%31 = load i64, i64* %7
%32 = add i64 %31, 1
store i64 %32, i64* %7
br label %12
33: ; preds = %27, %12
%34 = load i8*, i8** %10
store i8 0, i8* %34
%35 = load i64, i64* %7
ret i64 %35
}

LLVM IR (-O2):
define i64 @readbuffer(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i8
%5 = icmp ugt i64 %2, 1
br i1 %5, label %6, label %20
6: ; preds = %3, %16
%7 = phi i8* [ %17, %16 ], [ %1, %3 ]
%8 = phi i64 [ %18, %16 ], [ 1, %3 ]
%9 = call i64 @read(i32 %0, i8* %4, i32 1)
%10 = icmp eq i64 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i8, i8* %4
%13 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %12, i8* %7
%14 = load i8, i8* %4
%15 = icmp eq i8 %14, 10
br i1 %15, label %20, label %16
16: ; preds = %6, %11
%17 = phi i8* [ %13, %11 ], [ %7, %6 ]
%18 = add nuw i64 %8, 1
%19 = icmp eq i64 %18, %2
br i1 %19, label %20, label %6
20: ; preds = %16, %11, %3
%21 = phi i64 [ 1, %3 ], [ %8, %11 ], [ %2, %16 ]
%22 = phi i8* [ %1, %3 ], [ %13, %11 ], [ %17, %16 ]
store i8 0, i8* %22
ret i64 %21
}