#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 */
scalar_t__ read(int, char *, int);
int readline(int fd, char *str) {
  int i = 0;
  char c;
  while (read(fd, &c, 1) > 0 && c != '\n') {
    str[i++] = c;
  }
  str[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x16(%rbp)
jle    1156 <readline+0x46>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
test   $0x1,%al
jne    1166 <readline+0x56>
jmp    1183 <readline+0x73>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1126 <readline+0x16>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
xor    %ebx,%ebx
test   %rax,%rax
jle    1175 <readline+0x65>
mov    0x7(%rsp),%al
cmp    $0xa,%al
je     1175 <readline+0x65>
xor    %ebx,%ebx
lea    0x7(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    1175 <readline+0x65>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
jne    1150 <readline+0x40>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readline(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %7
7: ; preds = %17, %2
%8 = load i32, i32* %3
%9 = call i64 @read(i32 %8, i8* %6, i32 1)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i8, i8* %6
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 10
br label %15
15: ; preds = %11, %7
%16 = phi i1 [ false, %7 ], [ %14, %11 ]
br i1 %16, label %17, label %24
17: ; preds = %15
%18 = load i8, i8* %6
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
store i32 %21, i32* %5
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 %18, i8* %23
br label %7
24: ; preds = %15
%25 = load i8*, i8** %4
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i32, i32* %5
ret i32 %29
}

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