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


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)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1164 <get_line+0x44>
movq   $0x0,-0x8(%rbp)
jmp    11ad <get_line+0x8d>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11a5 <get_line+0x85>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11a5 <get_line+0x85>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%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     1149 <get_line+0x29>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     114b <get_line+0x2b>
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    114b <get_line+0x2b>
movb   $0x0,-0x1(%rbx,%rax,1)
jmp    114b <get_line+0x2b>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @get_line(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 %15, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = add i64 %7, -1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %15
14: ; preds = %9
store i8 0, i8* %11
br label %15
15: ; preds = %6, %9, %14, %3
%16 = phi i8* [ null, %3 ], [ %0, %14 ], [ %0, %9 ], [ %0, %6 ]
ret i8* %16
}