#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 */
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EINVAL;
scalar_t__ errno;
int read(int, char *, int);
ssize_t read_line(int fd, void *buffer, size_t n) {
  ssize_t numRead;
  size_t totRead;
  char *buf;
  char ch;
  if (n <= 0 || buffer == NULL) {
    errno = EINVAL;
    return -1;
  }
  buf = buffer;
  totRead = 0;
  for (;;) {
    numRead = read(fd, &ch, 1);
    if (numRead == -1) {
      if (errno == EINTR)
        continue;
      else
        return -1;
    } else if (numRead == 0) {
      if (totRead == 0)
        return 0;
      else
        break;
    } else {
      if (totRead < n - 1) {
        totRead++;
        *buf++ = ch;
      }
      if (ch == '\n')
        break;
    }
  }
  *buf = '\0';
  return totRead;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    1139 <read_line+0x29>
cmpq   $0x0,-0x10(%rbp)
jne    1159 <read_line+0x49>
mov    0x2e90(%rip),%rax
mov    (%rax),%rcx
mov    0x2e96(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <read_line+0x12c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b2 <read_line+0xa2>
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
jne    11a6 <read_line+0x96>
jmp    1169 <read_line+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <read_line+0x12c>
cmpl   $0x0,-0x1c(%rbp)
jne    11d8 <read_line+0xc8>
cmpq   $0x0,-0x28(%rbp)
jne    11d3 <read_line+0xc3>
movl   $0x0,-0x4(%rbp)
jmp    123c <read_line+0x12c>
jmp    122e <read_line+0x11e>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jae    120d <read_line+0xfd>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x31(%rbp),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    121f <read_line+0x10f>
jmp    122e <read_line+0x11e>
jmp    1224 <read_line+0x114>
jmp    1229 <read_line+0x119>
jmp    1169 <read_line+0x59>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rsi,%rsi
je     119a <read_line+0x8a>
mov    %rdx,%r12
test   %rdx,%rdx
je     119a <read_line+0x8a>
mov    %edi,%ebp
mov    %rsi,0x10(%rsp)
add    $0xffffffffffffffff,%r12
xor    %r15d,%r15d
lea    0xf(%rsp),%rbx
mov    0x2e9b(%rip),%r13
mov    0x2e8c(%rip),%r14
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    116f <read_line+0x5f>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1150 <read_line+0x40>
jmp    11c5 <read_line+0xb5>
test   %eax,%eax
je     11b6 <read_line+0xa6>
cmp    %r12,%r15
jae    1191 <read_line+0x81>
add    $0x1,%r15
movzbl 0xf(%rsp),%eax
mov    0x10(%rsp),%rcx
mov    %al,(%rcx)
add    $0x1,%rcx
mov    %rcx,0x10(%rsp)
cmpb   $0xa,0xf(%rsp)
jne    1150 <read_line+0x40>
jmp    11bb <read_line+0xab>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e35(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%r15d
jmp    11d0 <read_line+0xc0>
test   %r15,%r15
je     11cd <read_line+0xbd>
mov    0x10(%rsp),%rax
movb   $0x0,(%rax)
jmp    11d0 <read_line+0xc0>
mov    $0xffffffff,%r15d
jmp    11d0 <read_line+0xc0>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @read_line(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i64, i64* %7
%13 = icmp ule i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14, %3
%18 = load i64, i64* @EINVAL
store i64 %18, i64* @errno
store i32 -1, i32* %4
br label %63
19: ; preds = %14
%20 = load i8*, i8** %6
store i8* %20, i8** %10
store i64 0, i64* %9
br label %21
21: ; preds = %58, %30, %19
%22 = load i32, i32* %5
%23 = call i32 @read(i32 %22, i8* %11, i32 1)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
br label %21
31: ; preds = %26
store i32 -1, i32* %4
br label %63
32: ; preds = %21
%33 = load i32, i32* %8
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i64, i64* %9
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 0, i32* %4
br label %63
39: ; preds = %35
br label %59
40: ; preds = %32
%41 = load i64, i64* %9
%42 = load i64, i64* %7
%43 = sub i64 %42, 1
%44 = icmp ult i64 %41, %43
br i1 %44, label %45, label %51
45: ; preds = %40
%46 = load i64, i64* %9
%47 = add i64 %46, 1
store i64 %47, i64* %9
%48 = load i8, i8* %11
%49 = load i8*, i8** %10
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %10
store i8 %48, i8* %49
br label %51
51: ; preds = %45, %40
%52 = load i8, i8* %11
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 10
br i1 %54, label %55, label %56
55: ; preds = %51
br label %59
56: ; preds = %51
br label %57
57: ; preds = %56
br label %58
58: ; preds = %57
br label %21
59: ; preds = %55, %39
%60 = load i8*, i8** %10
store i8 0, i8* %60
%61 = load i64, i64* %9
%62 = trunc i64 %61 to i32
store i32 %62, i32* %4
br label %63
63: ; preds = %59, %38, %31, %17
%64 = load i32, i32* %4
ret i32 %64
}

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