#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 /*<<< orphan*/ E2BIG;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int strlen(char *);
int str_readline(FILE *stream, char *line, int max_len) {
  char *r;
  int len;
  r = fgets(line, max_len, stream);
  if (r == NULL) {
    if (ferror(stream)) {
      return -1;
    } else {
      line[0] = '\0';
      return 0;
    }
  }
  len = strlen(line);
  if (len + 1 == max_len && line[len - 1] != '\n' && !feof(stream)) {
    errno = E2BIG;
    return -1;
  }
  if (line[len - 1] == '\n') {
    line[len - 1] = '\0';
    len--;
  }
  return len;
}


x86 assembly (clang, -O0):
<str_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a4 <str_readline+0x64>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1191 <str_readline+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <str_readline+0x103>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1243 <str_readline+0x103>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jne    1209 <str_readline+0xc9>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     1209 <str_readline+0xc9>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
jne    1209 <str_readline+0xc9>
mov    0x2df6(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddd(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <str_readline+0x103>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    123d <str_readline+0xfd>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_readline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
mov    %edx,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     119f <str_readline+0x5f>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0x1(%rbp),%eax
movslq %ebp,%rbx
add    $0xffffffffffffffff,%rbx
cmp    %r12d,%eax
jne    118f <str_readline+0x4f>
cmpb   $0xa,(%r14,%rbx,1)
je     118f <str_readline+0x4f>
mov    %r15,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11b9 <str_readline+0x79>
cmpb   $0xa,(%r14,%rbx,1)
jne    11d0 <str_readline+0x90>
movb   $0x0,(%r14,%rbx,1)
mov    %ebx,%ebp
jmp    11d0 <str_readline+0x90>
mov    %r15,%rdi
call   1030 <ferror@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11d0 <str_readline+0x90>
movb   $0x0,(%r14)
xor    %ebp,%ebp
jmp    11d0 <str_readline+0x90>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    0x2e0f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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