#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__ EINTR;
scalar_t__ EINVAL;
scalar_t__ errno;
int readLine(int fd, void *buffer, int n) {
  int numRead;
  int totRead;
  char *buf;
  char ch;
  if (n <= 0 || buffer == NULL) {
    errno = EINVAL;
    return -1;
  }
  buf = buffer;
  totRead = 0;
  while (true) {
    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):
<readLine>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1123 <readLine+0x23>
cmpq   $0x0,-0x10(%rbp)
jne    1143 <readLine+0x43>
mov    0x2ea6(%rip),%rax
mov    (%rax),%rcx
mov    0x2eac(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <readLine+0x108>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1187 <readLine+0x87>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jne    117b <readLine+0x7b>
jmp    1152 <readLine+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <readLine+0x108>
cmpl   $0x0,-0x18(%rbp)
jne    11ac <readLine+0xac>
cmpl   $0x0,-0x1c(%rbp)
jne    11a7 <readLine+0xa7>
movl   $0x0,-0x4(%rbp)
jmp    1208 <readLine+0x108>
jmp    11fb <readLine+0xfb>
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11da <readLine+0xda>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    11ec <readLine+0xec>
jmp    11fb <readLine+0xfb>
jmp    11f1 <readLine+0xf1>
jmp    11f6 <readLine+0xf6>
jmp    1152 <readLine+0x52>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
test   %rsi,%rsi
je     1112 <readLine+0x12>
test   %edx,%edx
jle    1112 <readLine+0x12>
nopl   0x0(%rax)
jmp    1110 <readLine+0x10>
mov    0x2ebf(%rip),%rax
mov    (%rax),%rax
mov    0x2ebd(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
@EINVAL = global i64 0
@errno = global i64 0
@EINTR = global i64 0
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*
%11 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i32, i32* %7
%13 = icmp sle i32 %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 %60
19: ; preds = %14
%20 = load i8*, i8** %6
store i8* %20, i8** %10
store i32 0, i32* %9
br label %21
21: ; preds = %19, %28, %56
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
br label %21
29: ; preds = %24
store i32 -1, i32* %4
br label %60
30: ; preds = %21
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 0, i32* %4
br label %60
37: ; preds = %33
br label %57
38: ; preds = %30
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = sub nsw i32 %40, 1
%42 = icmp slt i32 %39, %41
br i1 %42, label %43, label %49
43: ; preds = %38
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %9
%46 = load i8, i8* %11
%47 = load i8*, i8** %10
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %10
store i8 %46, i8* %47
br label %49
49: ; preds = %43, %38
%50 = load i8, i8* %11
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 10
br i1 %52, label %53, label %54
53: ; preds = %49
br label %57
54: ; preds = %49
br label %55
55: ; preds = %54
br label %56
56: ; preds = %55
br label %21
57: ; preds = %53, %37
%58 = load i8*, i8** %10
store i8 0, i8* %58
%59 = load i32, i32* %9
store i32 %59, i32* %4
br label %60
60: ; preds = %57, %36, %29, %17
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
@EINVAL = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @readLine(i32 %0, i8* readnone %1, i32 %2) {
%4 = icmp slt i32 %2, 1
%5 = icmp eq i8* %1, null
%6 = or i1 %5, %4
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = load i64, i64* @EINVAL
store i64 %8, i64* @errno
ret i32 -1
9: ; preds = %3, %9
br label %9
}