#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 */

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
char *get_line(char *buffer, int count) {
  size_t l;
  if (fgets(buffer, count, stdin) == NULL ||
      buffer[l = strlen(buffer) - 1] != '\n') {
    fputs("Error reading line", stderr);
    return NULL;
  } else {
    buffer[l] = '\0';
    return buffer;
  }
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     118c <get_line+0x5c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11ae <get_line+0x7e>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
call   1040 <fputs@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c2 <get_line+0x92>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%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
mov    0x2ead(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
test   %rax,%rax
je     115f <get_line+0x2f>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    115f <get_line+0x2f>
movb   $0x0,-0x1(%rbx,%rax,1)
jmp    1176 <get_line+0x46>
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1040 <fputs@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [19 x i8] c"Error reading line\00"
@stderr = global i32 0
define i8* @get_line(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = load i32, i32* %5
%9 = load i32, i32* @stdin
%10 = call i32* @fgets(i8* %7, i32 %8, i32 %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %22, label %12
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = sub nsw i32 %15, 1
%17 = sext i32 %16 to i64
store i64 %17, i64* %6
%18 = getelementptr inbounds i8, i8* %13, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 10
br i1 %21, label %22, label %25
22: ; preds = %12, %2
%23 = load i32, i32* @stderr
%24 = call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %23)
store i8* null, i8** %3
br label %30
25: ; preds = %12
%26 = load i8*, i8** %4
%27 = load i64, i64* %6
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %4
store i8* %29, i8** %3
br label %30
30: ; preds = %25, %22
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [19 x i8] c"Error reading line\00"
@stderr = global i32 0
define i8* @get_line(i8* %0, i32 %1) {
%3 = load i32, i32* @stdin
%4 = tail call i32* @fgets(i8* %0, i32 %1, i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %16, label %13
13: ; preds = %6, %2
%14 = load i32, i32* @stderr
%15 = tail call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %17
16: ; preds = %6
store i8 0, i8* %10
br label %17
17: ; preds = %16, %13
%18 = phi i8* [ null, %13 ], [ %0, %16 ]
ret i8* %18
}