#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 *ptr, int max) {
  int count = 0;
  while (read(fd, ptr, 1) == 1) {
    if (++count == max)
      break;
    if (*ptr == '\n')
      break;
    ptr++;
  }
  *ptr = '\0';
  return count;
}


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,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1180 <readline+0x70>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmp    -0x14(%rbp),%eax
jne    115a <readline+0x4a>
jmp    1180 <readline+0x70>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    116f <readline+0x5f>
jmp    1180 <readline+0x70>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1129 <readline+0x19>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    $0x1,%ebp
mov    $0x1,%edx
call   1030 <read@plt>
xor    %r12d,%r12d
cmp    $0x1,%eax
jne    1174 <readline+0x64>
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
cmp    %ebp,%r14d
je     116c <readline+0x5c>
cmpb   $0xa,(%rbx)
je     1171 <readline+0x61>
add    $0x1,%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
add    $0x1,%ebp
add    $0x1,%r12d
cmp    $0x1,%eax
je     1140 <readline+0x30>
jmp    1174 <readline+0x64>
mov    %r14d,%r12d
jmp    1174 <readline+0x64>
mov    %ebp,%r12d
movb   $0x0,(%rbx)
mov    %r12d,%eax
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 i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %25, %3
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = call i32 @read(i32 %9, i8* %10, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %28
13: ; preds = %8
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 1
store i32 %15, i32* %7
%16 = load i32, i32* %6
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %13
br label %28
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %25
24: ; preds = %19
br label %28
25: ; preds = %19
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
br label %8
28: ; preds = %24, %18, %8
%29 = load i8*, i8** %5
store i8 0, i8* %29
%30 = load i32, i32* %7
ret i32 %30
}

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