#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *get_prompt();
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
char *fetchline(void) {
  char *p, *line = malloc(1024);
  if (!line)
    return NULL;
  printf("%s", get_prompt());
  fflush(stdout);
  if (!fgets(line, 1024, stdin)) {
    free(line);
    return NULL;
  }
  p = line + strlen(line);
  if (p != line && p[-1] == '\n')
    p[-1] = '\0';
  return line;
}


x86 assembly (clang, -O0):
<fetchline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119e <fetchline+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    1248 <fetchline+0xd8>
mov    $0x0,%al
call   1040 <get_prompt@plt>
mov    %rax,%rsi
lea    0xe51(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%eax
jne    11f8 <fetchline+0x88>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1248 <fetchline+0xd8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1240 <fetchline+0xd0>
mov    -0x10(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0xa,%eax
jne    1240 <fetchline+0xd0>
mov    -0x10(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetchline>:
push   %rbx
mov    $0x400,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11e5 <fetchline+0x75>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <get_prompt@plt>
lea    0xe6f(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
test   %eax,%eax
je     11dd <fetchline+0x6d>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     11e7 <fetchline+0x77>
cltq
cmpb   $0xa,-0x1(%rbx,%rax,1)
jne    11e7 <fetchline+0x77>
movb   $0x0,-0x1(%rbx,%rax,1)
jmp    11e7 <fetchline+0x77>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@stdin = global i32 0
define i8* @fetchline() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = call i8* @malloc(i32 1024)
store i8* %4, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
store i8* null, i8** %1
br label %40
8: ; preds = %0
%9 = call i8* (...) @get_prompt()
%10 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @stdout
%12 = call i32 @fflush(i32 %11)
%13 = load i8*, i8** %3
%14 = load i32, i32* @stdin
%15 = call i32 @fgets(i8* %13, i32 1024, i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %8
%18 = load i8*, i8** %3
%19 = call i32 @free(i8* %18)
store i8* null, i8** %1
br label %40
20: ; preds = %8
%21 = load i8*, i8** %3
%22 = load i8*, i8** %3
%23 = call i32 @strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8* %25, i8** %2
%26 = load i8*, i8** %2
%27 = load i8*, i8** %3
%28 = icmp ne i8* %26, %27
br i1 %28, label %29, label %38
29: ; preds = %20
%30 = load i8*, i8** %2
%31 = getelementptr inbounds i8, i8* %30, i64 -1
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 10
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i8*, i8** %2
%37 = getelementptr inbounds i8, i8* %36, i64 -1
store i8 0, i8* %37
br label %38
38: ; preds = %35, %29, %20
%39 = load i8*, i8** %3
store i8* %39, i8** %1
br label %40
40: ; preds = %38, %17, %7
%41 = load i8*, i8** %1
ret i8* %41
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@stdin = global i32 0
define i8* @fetchline() {
%1 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%2 = icmp eq i8* %1, null
br i1 %2, label %23, label %3
3: ; preds = %0
%4 = tail call i8* (...) @get_prompt()
%5 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = load i32, i32* @stdout
%7 = tail call i32 @fflush(i32 %6)
%8 = load i32, i32* @stdin
%9 = tail call i32 @fgets(i8* %1, i32 1024, i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @free(i8* %1)
br label %23
13: ; preds = %3
%14 = tail call i32 @strlen(i8* %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %13
%17 = sext i32 %14 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = getelementptr inbounds i8, i8* %18, i64 -1
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 10
br i1 %21, label %22, label %23
22: ; preds = %16
store i8 0, i8* %19
br label %23
23: ; preds = %13, %16, %22, %0, %11
%24 = phi i8* [ null, %11 ], [ null, %0 ], [ %1, %22 ], [ %1, %16 ], [ %1, %13 ]
ret i8* %24
}