#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/ *);
char *strdup(char *);
int strlen(char *);
char *read_a_line(FILE *file) {
  char buffer[10000];
  char *returnval;
  char *newlinepos;
  returnval = fgets(buffer, sizeof(buffer), file);
  if (returnval) {
    newlinepos = buffer + strlen(buffer) - 1;
    if (*newlinepos == '\n') {
      *newlinepos = '\0';
    };
    return strdup(buffer);
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<read_a_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2740,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x2720(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x2710,%esi
call   1040 <fgets@plt>
mov    %rax,-0x2728(%rbp)
cmpq   $0x0,-0x2728(%rbp)
je     11cf <read_a_line+0x9f>
lea    -0x2720(%rbp),%rax
mov    %rax,-0x2738(%rbp)
lea    -0x2720(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2738(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x2730(%rbp)
mov    -0x2730(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    11ba <read_a_line+0x8a>
mov    -0x2730(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x2720(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11d7 <read_a_line+0xa7>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2740,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_a_line>:
sub    $0x2718,%rsp
mov    %rdi,%rdx
mov    %rsp,%rdi
mov    $0x2710,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     1172 <read_a_line+0x42>
mov    %rsp,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rsp,%rax,1)
jne    1162 <read_a_line+0x32>
movb   $0x0,-0x1(%rsp,%rax,1)
mov    %rsp,%rdi
call   1050 <strdup@plt>
add    $0x2718,%rsp
ret
xor    %eax,%eax
add    $0x2718,%rsp
ret

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

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