#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 read_line(int fd, size_t res_len, char *buf) {
  size_t i = 0;
  char c = 0;
  do {
    if (read(fd, &c, sizeof(char)) != sizeof(char)) {
      return -1;
    }
    buf[i++] = c;
  } while (c != '\n' && i < res_len);
  buf[i - 1] = 0;
  return 0;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cltq
cmp    $0x1,%rax
je     1158 <read_line+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <read_line+0xa3>
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
movsbl -0x21(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x22(%rbp)
je     1191 <read_line+0x81>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
setb   %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    112f <read_line+0x1f>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%ebp
movb   $0x0,0xf(%rsp)
mov    $0x1,%ebx
lea    0xf(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1179 <read_line+0x69>
movzbl 0xf(%rsp),%ecx
mov    %cl,-0x1(%r12,%rbx,1)
lea    0x1(%rbx),%rax
cmp    $0xa,%cl
je     116f <read_line+0x5f>
cmp    %r14,%rbx
mov    %rax,%rbx
jb     1140 <read_line+0x30>
movb   $0x0,-0x2(%r12,%rax,1)
xor    %eax,%eax
jmp    117e <read_line+0x6e>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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