#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 */
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strdup(char *);
size_t strlen(char *);
char *readline(const char *prompt) {
  static char buf[10240];
  fputs(prompt, stdout);
  char *pbuf = fgets(buf, 10240, stdin);
  if (!pbuf)
    return NULL;
  size_t len = strlen(buf);
  while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
    buf[--len] = 0;
  return pbuf ? strdup(buf) : NULL;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
lea    0x2ee2(%rip),%rdi
mov    $0x2800,%esi
call   1050 <fgets@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1194 <readline+0x54>
movq   $0x0,-0x8(%rbp)
jmp    125a <readline+0x11a>
lea    0x2eb5(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jbe    11f7 <readline+0xb7>
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
lea    0x2e8d(%rip),%rax
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x22(%rbp)
je     11f1 <readline+0xb1>
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
lea    0x2e6c(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
sete   %al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1207 <readline+0xc7>
jmp    1227 <readline+0xe7>
mov    -0x20(%rbp),%rcx
mov    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
lea    0x2e33(%rip),%rax
movb   $0x0,-0x1(%rax,%rcx,1)
jmp    11a4 <readline+0x64>
cmpq   $0x0,-0x18(%rbp)
je     1247 <readline+0x107>
lea    0x2e17(%rip),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x30(%rbp)
jmp    1252 <readline+0x112>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    1252 <readline+0x112>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
lea    0x2ef1(%rip),%rdi
mov    $0x2800,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11b7 <readline+0x77>
lea    0x2edb(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
jne    119b <readline+0x5b>
jmp    11aa <readline+0x6a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movb   $0x0,-0x1(%rax,%rbx,1)
add    $0xffffffffffffffff,%rax
je     11aa <readline+0x6a>
movzbl -0x1(%rax,%rbx,1),%ecx
cmp    $0xd,%cl
je     1190 <readline+0x50>
cmp    $0xa,%cl
je     1190 <readline+0x50>
lea    0x2e9f(%rip),%rdi
pop    %rbx
jmp    1060 <strdup@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@readline.buf = internal global [10240 x i8] zeroinitializer
@stdout = global i32 0
@stdin = global i32 0
define i8* @readline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @stdout
%8 = call i32 @fputs(i8* %6, i32 %7)
%9 = load i32, i32* @stdin
%10 = call i8* @fgets(i8* getelementptr inbounds ([10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 0), i32 10240, i32 %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i8* null, i8** %2
br label %49
14: ; preds = %1
%15 = call i64 @strlen(i8* getelementptr inbounds ([10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 0))
store i64 %15, i64* %5
br label %16
16: ; preds = %37, %14
%17 = load i64, i64* %5
%18 = icmp ugt i64 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %16
%20 = load i64, i64* %5
%21 = sub i64 %20, 1
%22 = getelementptr inbounds [10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %33, label %26
26: ; preds = %19
%27 = load i64, i64* %5
%28 = sub i64 %27, 1
%29 = getelementptr inbounds [10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 13
br label %33
33: ; preds = %26, %19
%34 = phi i1 [ true, %19 ], [ %32, %26 ]
br label %35
35: ; preds = %33, %16
%36 = phi i1 [ false, %16 ], [ %34, %33 ]
br i1 %36, label %37, label %41
37: ; preds = %35
%38 = load i64, i64* %5
%39 = add i64 %38, -1
store i64 %39, i64* %5
%40 = getelementptr inbounds [10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 %39
store i8 0, i8* %40
br label %16
41: ; preds = %35
%42 = load i8*, i8** %4
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i8* @strdup(i8* getelementptr inbounds ([10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 0))
br label %47
46: ; preds = %41
br label %47
47: ; preds = %46, %44
%48 = phi i8* [ %45, %44 ], [ null, %46 ]
store i8* %48, i8** %2
br label %49
49: ; preds = %47, %13
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@readline.buf = internal global [10240 x i8] zeroinitializer
@stdout = global i32 0
@stdin = global i32 0
define noalias i8* @readline(i8* %0) {
%2 = load i32, i32* @stdout
%3 = tail call i32 @fputs(i8* %0, i32 %2)
%4 = load i32, i32* @stdin
%5 = tail call i8* @fgets(i8* getelementptr inbounds ([10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 0), i32 10240, i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %19, label %7
7: ; preds = %1
%8 = tail call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %7, %15
%11 = phi i64 [ %12, %15 ], [ %8, %7 ]
%12 = add i64 %11, -1
%13 = getelementptr inbounds [10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 %12
%14 = load i8, i8* %13
switch i8 %14, label %17 [
i8 10, label %15
i8 13, label %15
]
15: ; preds = %10, %10
store i8 0, i8* %13
%16 = icmp eq i64 %12, 0
br i1 %16, label %17, label %10
17: ; preds = %10, %15, %7
%18 = tail call i8* @strdup(i8* getelementptr inbounds ([10240 x i8], [10240 x i8]* @readline.buf, i64 0, i64 0))
br label %19
19: ; preds = %1, %17
%20 = phi i8* [ %18, %17 ], [ null, %1 ]
ret i8* %20
}