#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 read(int, char *, int);
int readline(int fd, char *buf, int size) {
  char c;
  int n;
  for (n = 0; n < size; n++) {
    if (read(fd, &c, 1) == 1) {
      if (c == '\n') {
        buf[n] = 0;
        break;
      } else {
        buf[n] = c;
      }
    } else {
      buf[n] = 0;
      break;
    }
  }
  return n;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a4 <readline+0x94>
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1180 <readline+0x70>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    116d <readline+0x5d>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a4 <readline+0x94>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1191 <readline+0x81>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a4 <readline+0x94>
jmp    1196 <readline+0x86>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1129 <readline+0x19>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edx,%edx
jle    1166 <readline+0x56>
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edx,%r12d
xor    %ebx,%ebx
lea    0xf(%rsp),%r15
nop
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    115f <readline+0x4f>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     115f <readline+0x4f>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <readline+0x20>
mov    %r12d,%ebx
jmp    1168 <readline+0x58>
movb   $0x0,(%r14,%rbx,1)
jmp    1168 <readline+0x58>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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