#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 */
char *fgets(char *, size_t, int /*<<< orphan*/ *);
size_t strlen(char *);
size_t get_line(char *s, size_t n, FILE *f) {
  char *p = fgets(s, n, f);
  if (p == NULL)
    return -1;
  size_t len = strlen(s);
  if (s[len - 1] == '\n') {
    s[--len] = '\0';
  }
  return len;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1161 <get_line+0x41>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11a7 <get_line+0x87>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    119f <get_line+0x7f>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbx
mov    %rdi,%rbx
call   1040 <fgets@plt>
test   %rax,%rax
je     1147 <get_line+0x27>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmpb   $0xa,-0x1(%rbx,%rax,1)
jne    1145 <get_line+0x25>
add    $0xffffffffffffffff,%rax
movb   $0x0,(%rbx,%rax,1)
pop    %rbx
ret
mov    $0xffffffffffffffff,%rax
pop    %rbx
ret

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

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