#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
scalar_t__ errno;
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(scalar_t__);
int readline(char *line, int size, FILE *fp) {
  int c;
  int pos = 0;
  for (;;) {
    errno = 0;
    c = fgetc(fp);
    if (c == EOF) {
      if (!errno)
        return -1;
      printf("error: %s\n", strerror(errno));
      errno = 0;
      continue;
    }
    if (c == '\n') {
      break;
    }
    if (pos < size - 1) {
      line[pos++] = c;
    }
  }
  line[pos] = 0;
  return pos;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2e87(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%eax
jne    11c4 <readline+0x94>
mov    0x2e5b(%rip),%rax
cmpq   $0x0,(%rax)
jne    1193 <readline+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <readline+0xe5>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xe54(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e20(%rip),%rax
movq   $0x0,(%rax)
jmp    114a <readline+0x1a>
cmpl   $0xa,-0x24(%rbp)
jne    11d3 <readline+0xa3>
jmp    1203 <readline+0xd3>
mov    -0x28(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11fe <readline+0xce>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    114a <readline+0x1a>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %rdi,(%rsp)
add    $0xffffffff,%r12d
xor    %r15d,%r15d
mov    0x2e85(%rip),%rbp
mov    0x2e8e(%rip),%r14
lea    0xe9f(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movq   $0x0,0x0(%rbp)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    (%r14),%eax
jne    11a0 <readline+0x70>
mov    0x0(%rbp),%rdi
test   %rdi,%rdi
je     11ba <readline+0x8a>
call   1050 <strerror@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
jmp    1170 <readline+0x40>
cmp    $0xa,%eax
je     11d2 <readline+0xa2>
cmp    %r12d,%r15d
jge    1170 <readline+0x40>
mov    %r15d,%ecx
add    $0x1,%r15d
mov    (%rsp),%rdx
mov    %al,(%rdx,%rcx,1)
jmp    1170 <readline+0x40>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%eax
mov    (%rsp),%rcx
movb   $0x0,(%rcx,%rax,1)
jmp    11c0 <readline+0x90>

LLVM IR (-O0):
@errno = global i64 0
@EOF = global i32 0
@.str = [11 x i8] c"error: %s\0A\00"
define i32 @readline(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 0, i32* %9
br label %10
10: ; preds = %41, %20, %3
store i64 0, i64* @errno
%11 = load i32*, i32** %7
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %24
16: ; preds = %10
%17 = load i64, i64* @errno
%18 = icmp ne i64 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %16
store i32 -1, i32* %4
br label %48
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = call i8* @strerror(i64 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %22)
store i64 0, i64* @errno
br label %10
24: ; preds = %10
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %28
27: ; preds = %24
br label %42
28: ; preds = %24
%29 = load i32, i32* %9
%30 = load i32, i32* %6
%31 = sub nsw i32 %30, 1
%32 = icmp slt i32 %29, %31
br i1 %32, label %33, label %41
33: ; preds = %28
%34 = load i32, i32* %8
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %5
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
store i32 %38, i32* %9
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 %35, i8* %40
br label %41
41: ; preds = %33, %28
br label %10
42: ; preds = %27
%43 = load i8*, i8** %5
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
%47 = load i32, i32* %9
store i32 %47, i32* %4
br label %48
48: ; preds = %42, %19
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@errno = global i64 0
@EOF = global i32 0
@.str = [11 x i8] c"error: %s\0A\00"
define i32 @readline(i8* nocapture writeonly %0, i32 %1, i32* nocapture %2) {
%4 = add nsw i32 %1, -1
br label %5
5: ; preds = %25, %3
%6 = phi i32 [ %27, %25 ], [ 0, %3 ]
%7 = icmp slt i32 %6, %4
br label %8
8: ; preds = %5, %24
store i64 0, i64* @errno
%9 = tail call i32 @fgetc(i32* %2)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %21
12: ; preds = %8, %15
%13 = load i64, i64* @errno
%14 = icmp eq i64 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %12
%16 = tail call i8* @strerror(i64 %13)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %16)
store i64 0, i64* @errno
%18 = tail call i32 @fgetc(i32* %2)
%19 = load i32, i32* @EOF
%20 = icmp eq i32 %18, %19
br i1 %20, label %12, label %21
21: ; preds = %15, %8
%22 = phi i32 [ %9, %8 ], [ %18, %15 ]
%23 = icmp eq i32 %22, 10
br i1 %23, label %30, label %24
24: ; preds = %21
br i1 %7, label %25, label %8
25: ; preds = %24
%26 = trunc i32 %22 to i8
%27 = add nuw nsw i32 %6, 1
%28 = zext i32 %6 to i64
%29 = getelementptr inbounds i8, i8* %0, i64 %28
store i8 %26, i8* %29
br label %5
30: ; preds = %21
%31 = zext i32 %6 to i64
%32 = getelementptr inbounds i8, i8* %0, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %12, %30
%34 = phi i32 [ %6, %30 ], [ -1, %12 ]
ret i32 %34
}