#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 */
scalar_t__ feof(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int strlen(char *);
int read_line(FILE *f, char *line, int size) {
  if (!f)
    return -1;
  line = fgets(line, size, f);
  if (!line || feof(f))
    return -1;
  while (strlen(line) &&
         (line[strlen(line) - 1] == 13 || line[strlen(line) - 1] == 10))
    line[strlen(line) - 1] = 0;
  return strlen(line);
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115a <read_line+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <read_line+0x127>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118c <read_line+0x5c>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1198 <read_line+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <read_line+0x127>
jmp    119d <read_line+0x6d>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
je     1215 <read_line+0xe5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xd,%ecx
mov    %al,-0x1e(%rbp)
je     120f <read_line+0xdf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
sete   %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1225 <read_line+0xf5>
jmp    124b <read_line+0x11b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    119d <read_line+0x6d>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     115e <read_line+0x2e>
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %edx,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     115e <read_line+0x2e>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     117f <read_line+0x4f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     11ad <read_line+0x7d>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xd,-0x1(%rax,%rbx,1)
je     1170 <read_line+0x40>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     1170 <read_line+0x40>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <strlen@plt>

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

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