#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*/ *);
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/ *);
size_t strlen(char *);
int readline(char *buf, size_t len, FILE *fin) {
  if (fgets(buf, len, fin) == NULL)
    return feof(fin) ? 0 : -1;
  len = strlen(buf);
  if (buf[len - 1] == '\n')
    buf[--len] = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1181 <readline+0x51>
mov    -0x20(%rbp),%rdi
call   1050 <feof@plt>
mov    %rax,%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11c6 <readline+0x96>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11bf <readline+0x8f>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x18(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
call   1040 <fgets@plt>
test   %rax,%rax
je     1162 <readline+0x32>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    $0x1,%eax
cmpb   $0xa,-0x1(%rcx,%rbx,1)
jne    1170 <readline+0x40>
movb   $0x0,-0x1(%rbx,%rcx,1)
jmp    1170 <readline+0x40>
mov    %r14,%rdi
call   1050 <feof@plt>
cmp    $0x1,%rax
sbb    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32 @readline(i8* %0, i64 %1, i32* nocapture %2) {
%4 = tail call i32* @fgets(i8* %0, i64 %1, i32* %2)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = tail call i64 @feof(i32* %2)
%8 = icmp eq i64 %7, 0
%9 = sext i1 %8 to i32
br label %17
10: ; preds = %3
%11 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%12 = add i64 %11, -1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %17
16: ; preds = %10
store i8 0, i8* %13
br label %17
17: ; preds = %10, %16, %6
%18 = phi i32 [ %9, %6 ], [ 1, %16 ], [ 1, %10 ]
ret i32 %18
}