#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*/ _exit(int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
size_t __attribute__((no)) readn(int fd, char *buf, size_t size) {
  size_t pos;
  for (pos = 0; pos < size; pos++) {
    char b;
    if (read(fd, &b, 1) <= 0) {
      _exit(0);
    }
    if (b == '\n') {
      break;
    }
    buf[pos] = b;
  }
  return pos;
}


x86 assembly (clang, -O0):
<readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    119c <readn+0x7c>
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jg     116b <readn+0x4b>
xor    %edi,%edi
call   1030 <_exit@plt>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    117d <readn+0x5d>
jmp    119c <readn+0x7c>
mov    -0x21(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    113b <readn+0x1b>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdx,%rdx
je     116f <readn+0x4f>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0xf(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    1181 <readn+0x61>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     1171 <readn+0x51>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1140 <readn+0x20>
mov    %r14,%rbx
jmp    1171 <readn+0x51>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

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

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