#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__ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int readline(FILE *fp, char *line, int len) {
  int ichar, ipos = 0;
  while ((ichar = fgetc(fp)) != EOF) {
    if (ichar == '\r')
      break;
    if (ichar == '\n')
      break;
    line[ipos++] = ichar;
    if (ipos >= len) {
      fprintf(stderr,
              "readline Warning: end of line not found (maxlen = %d)!\n", len);
      break;
    }
  }
  line[ipos] = 0;
  if (feof(fp) && ipos == 0)
    return (0);
  return (1);
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e8b(%rip),%rcx
cmp    (%rcx),%eax
je     11cb <readline+0x9b>
cmpl   $0xd,-0x20(%rbp)
jne    1174 <readline+0x44>
jmp    11cb <readline+0x9b>
cmpl   $0xa,-0x20(%rbp)
jne    1183 <readline+0x53>
jmp    11cb <readline+0x9b>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     11c6 <readline+0x96>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe44(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11cb <readline+0x9b>
jmp    114a <readline+0x1a>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1200 <readline+0xd0>
cmpl   $0x0,-0x24(%rbp)
jne    1200 <readline+0xd0>
movl   $0x0,-0x4(%rbp)
jmp    1207 <readline+0xd7>
movl   $0x1,-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    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r12
cmp    $0x2,%edx
mov    $0x1,%ebx
cmovge %edx,%ebx
xor    %ebp,%ebp
mov    0x2e90(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <fgetc@plt>
cmp    0x0(%r13),%eax
je     119f <readline+0x6f>
cmp    $0xa,%eax
je     119f <readline+0x6f>
cmp    $0xd,%eax
je     119f <readline+0x6f>
mov    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1160 <readline+0x30>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
mov    %r15d,%edx
call   1040 <fprintf@plt>
jmp    11a1 <readline+0x71>
mov    %ebp,%ebx
movslq %ebx,%rbx
movb   $0x0,(%r14,%rbx,1)
mov    %r12,%rdi
call   1050 <feof@plt>
test   %rax,%rax
sete   %al
test   %ebx,%ebx
setne  %cl
or     %al,%cl
movzbl %cl,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@stderr = global i32 0
@.str = [56 x i8] c"readline Warning: end of line not found (maxlen = %d)!\0A\00"
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
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %10
10: ; preds = %38, %3
%11 = load i32*, i32** %5
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* %8
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %39
15: ; preds = %10
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, 13
br i1 %17, label %18, label %19
18: ; preds = %15
br label %39
19: ; preds = %15
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 10
br i1 %21, label %22, label %23
22: ; preds = %19
br label %39
23: ; preds = %19
%24 = load i32, i32* %8
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %6
%27 = load i32, i32* %9
%28 = add nsw i32 %27, 1
store i32 %28, i32* %9
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8 %25, i8* %30
%31 = load i32, i32* %9
%32 = load i32, i32* %7
%33 = icmp sge i32 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %23
%35 = load i32, i32* @stderr
%36 = load i32, i32* %7
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %36)
br label %39
38: ; preds = %23
br label %10
39: ; preds = %34, %22, %18, %10
%40 = load i8*, i8** %6
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i32*, i32** %5
%45 = call i64 @feof(i32* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %39
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %47
store i32 0, i32* %4
br label %52
51: ; preds = %47, %39
store i32 1, i32* %4
br label %52
52: ; preds = %51, %50
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@EOF = global i32 0
@stderr = global i32 0
@.str = [56 x i8] c"readline Warning: end of line not found (maxlen = %d)!\0A\00"
define i32 @readline(i32* nocapture %0, i8* nocapture writeonly %1, i32 %2) {
%4 = call i32 @llvm.smax.i32(i32 %2, i32 1)
%5 = zext i32 %4 to i64
br label %6
6: ; preds = %13, %3
%7 = phi i64 [ %15, %13 ], [ 0, %3 ]
%8 = tail call i32 @fgetc(i32* %0)
%9 = load i32, i32* @EOF
%10 = freeze i32 %9
%11 = icmp eq i32 %8, %10
br i1 %11, label %21, label %12
12: ; preds = %6
switch i32 %8, label %13 [
i32 13, label %21
i32 10, label %21
]
13: ; preds = %12
%14 = trunc i32 %8 to i8
%15 = add nuw nsw i64 %7, 1
%16 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 %14, i8* %16
%17 = icmp eq i64 %15, %5
br i1 %17, label %18, label %6
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %23
21: ; preds = %6, %12, %12
%22 = trunc i64 %7 to i32
br label %23
23: ; preds = %21, %18
%24 = phi i32 [ %4, %18 ], [ %22, %21 ]
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 0, i8* %26
%27 = tail call i64 @feof(i32* %0)
%28 = icmp eq i64 %27, 0
%29 = icmp ne i32 %24, 0
%30 = select i1 %28, i1 true, i1 %29
%31 = zext i1 %30 to i32
ret i32 %31
}