#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*/ STDIN_FILENO;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ isUnsupportedTerm();
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
char *linenoiseNoTTY();
int linenoiseRaw(char *, int, char const *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strdup(char *);
size_t strlen(char *);
char *linenoise(const char *prompt) {
  char buf[4096];
  int count;
  if (!isatty(STDIN_FILENO)) {
    return linenoiseNoTTY();
  } else if (isUnsupportedTerm()) {
    size_t len;
    printf("%s", prompt);
    fflush(stdout);
    if (fgets(buf, 4096, stdin) == 0)
      return 0;
    len = strlen(buf);
    while (len && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) {
      len--;
      buf[len] = '\0';
    }
    return strdup(buf);
  } else {
    count = linenoiseRaw(buf, 4096, prompt);
    if (count == -1)
      return 0;
    return strdup(buf);
  }
}


x86 assembly (clang, -O0):
<linenoise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    11c6 <linenoise+0x36>
mov    $0x0,%al
call   10a0 <linenoiseNoTTY@plt>
mov    %rax,-0x8(%rbp)
jmp    1331 <linenoise+0x1a1>
mov    $0x0,%al
call   1070 <isUnsupportedTerm@plt>
cmp    $0x0,%rax
je     12ec <linenoise+0x15c>
mov    -0x10(%rbp),%rsi
lea    0xe1e(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    1226 <linenoise+0x96>
movq   $0x0,-0x8(%rbp)
jmp    1331 <linenoise+0x1a1>
lea    -0x1010(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x1020(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x1020(%rbp)
mov    %al,-0x1021(%rbp)
je     129e <linenoise+0x10e>
mov    -0x1020(%rbp),%rax
sub    $0x1,%rax
movsbl -0x1010(%rbp,%rax,1),%ecx
ff
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x1022(%rbp)
je     1292 <linenoise+0x102>
mov    -0x1020(%rbp),%rax
sub    $0x1,%rax
movsbl -0x1010(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
sete   %al
mov    %al,-0x1022(%rbp)
mov    -0x1022(%rbp),%al
mov    %al,-0x1021(%rbp)
mov    -0x1021(%rbp),%al
test   $0x1,%al
jne    12b1 <linenoise+0x121>
jmp    12d7 <linenoise+0x147>
mov    -0x1020(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x1020(%rbp)
mov    -0x1020(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
jmp    1239 <linenoise+0xa9>
lea    -0x1010(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1331 <linenoise+0x1a1>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <linenoiseRaw@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0xffffffff,-0x1014(%rbp)
jne    1321 <linenoise+0x191>
movq   $0x0,-0x8(%rbp)
jmp    1331 <linenoise+0x1a1>
lea    -0x1010(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linenoise>:
push   %rbx
sub    $0x1000,%rsp
mov    %rdi,%rbx
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     121c <linenoise+0x8c>
xor    %eax,%eax
call   1070 <isUnsupportedTerm@plt>
test   %rax,%rax
je     122b <linenoise+0x9b>
lea    0xe40(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     124a <linenoise+0xba>
mov    %rsp,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
jne    120b <linenoise+0x7b>
jmp    1240 <linenoise+0xb0>
movb   $0x0,-0x1(%rsp,%rax,1)
add    $0xffffffffffffffff,%rax
je     1240 <linenoise+0xb0>
movzbl -0x1(%rsp,%rax,1),%ecx
cmp    $0xd,%cl
je     1200 <linenoise+0x70>
cmp    $0xa,%cl
je     1200 <linenoise+0x70>
jmp    1240 <linenoise+0xb0>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
jmp    10a0 <linenoiseNoTTY@plt>
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1050 <linenoiseRaw@plt>
cmp    $0xffffffff,%eax
je     124a <linenoise+0xba>
mov    %rsp,%rdi
call   10b0 <strdup@plt>
jmp    124c <linenoise+0xbc>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@stdin = global i32 0
define i8* @linenoise(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @isatty(i32 %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = call i8* (...) @linenoiseNoTTY()
store i8* %11, i8** %2
br label %67
12: ; preds = %1
%13 = call i64 (...) @isUnsupportedTerm()
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %57
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @stdout
%19 = call i32 @fflush(i32 %18)
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%21 = load i32, i32* @stdin
%22 = call i64 @fgets(i8* %20, i32 4096, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %15
store i8* null, i8** %2
br label %67
25: ; preds = %15
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%27 = call i64 @strlen(i8* %26)
store i64 %27, i64* %6
br label %28
28: ; preds = %49, %25
%29 = load i64, i64* %6
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %47
31: ; preds = %28
%32 = load i64, i64* %6
%33 = sub i64 %32, 1
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 10
br i1 %37, label %45, label %38
38: ; preds = %31
%39 = load i64, i64* %6
%40 = sub i64 %39, 1
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 13
br label %45
45: ; preds = %38, %31
%46 = phi i1 [ true, %31 ], [ %44, %38 ]
br label %47
47: ; preds = %45, %28
%48 = phi i1 [ false, %28 ], [ %46, %45 ]
br i1 %48, label %49, label %54
49: ; preds = %47
%50 = load i64, i64* %6
%51 = add i64 %50, -1
store i64 %51, i64* %6
%52 = load i64, i64* %6
%53 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %52
store i8 0, i8* %53
br label %28
54: ; preds = %47
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%56 = call i8* @strdup(i8* %55)
store i8* %56, i8** %2
br label %67
57: ; preds = %12
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%59 = load i8*, i8** %3
%60 = call i32 @linenoiseRaw(i8* %58, i32 4096, i8* %59)
store i32 %60, i32* %5
%61 = load i32, i32* %5
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %64
63: ; preds = %57
store i8* null, i8** %2
br label %67
64: ; preds = %57
%65 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%66 = call i8* @strdup(i8* %65)
store i8* %66, i8** %2
br label %67
67: ; preds = %64, %63, %54, %24, %10
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@stdin = global i32 0
define i8* @linenoise(i8* %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @STDIN_FILENO
%5 = tail call i32 @isatty(i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i8* (...) @linenoiseNoTTY()
br label %36
9: ; preds = %1
%10 = tail call i64 (...) @isUnsupportedTerm()
%11 = icmp eq i64 %10, 0
br i1 %11, label %31, label %12
12: ; preds = %9
%13 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = load i32, i32* @stdout
%15 = tail call i32 @fflush(i32 %14)
%16 = load i32, i32* @stdin
%17 = call i64 @fgets(i8* %3, i32 4096, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %36, label %19
19: ; preds = %12
%20 = call i64 @strlen(i8* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %19, %27
%23 = phi i64 [ %24, %27 ], [ %20, %19 ]
%24 = add i64 %23, -1
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %24
%26 = load i8, i8* %25
switch i8 %26, label %29 [
i8 10, label %27
i8 13, label %27
]
27: ; preds = %22, %22
store i8 0, i8* %25
%28 = icmp eq i64 %24, 0
br i1 %28, label %29, label %22
29: ; preds = %27, %22, %19
%30 = call i8* @strdup(i8* %3)
br label %36
31: ; preds = %9
%32 = call i32 @linenoiseRaw(i8* %3, i32 4096, i8* %0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i8* @strdup(i8* %3)
br label %36
36: ; preds = %31, %29, %12, %34, %7
%37 = phi i8* [ %35, %34 ], [ %8, %7 ], [ %30, %29 ], [ null, %12 ], [ null, %31 ]
ret i8* %37
}