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


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c3 <readline+0xb3>
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x1,%eax
jne    117f <readline+0x6f>
mov    -0x1d(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movsbl -0x1d(%rbp),%eax
cmp    $0x2f6e,%eax
jne    117a <readline+0x6a>
jmp    11c3 <readline+0xb3>
jmp    11b0 <readline+0xa0>
cmpl   $0x0,-0x1c(%rbp)
jne    11a4 <readline+0x94>
cmpl   $0x1,-0x18(%rbp)
jne    119f <readline+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <readline+0xc0>
jmp    11c3 <readline+0xb3>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <readline+0xc0>
jmp    11b5 <readline+0xa5>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <readline+0x19>
mov    -0x10(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%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
mov    %rsi,%rbx
mov    $0x1,%ebp
cmp    $0x2,%edx
jl     1178 <readline+0x68>
mov    %edx,%r12d
mov    %edi,%r14d
mov    $0x1,%ebp
lea    0xf(%rsp),%r15
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    116d <readline+0x5d>
movzbl 0xf(%rsp),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%ebp
cmp    %ebp,%r12d
jne    1140 <readline+0x30>
mov    %r12d,%ebp
jmp    1178 <readline+0x68>
test   %eax,%eax
jne    118a <readline+0x7a>
xor    %eax,%eax
cmp    $0x1,%ebp
je     117d <readline+0x6d>
movb   $0x30,(%rbx)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    117d <readline+0x6d>

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

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