#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*/);
scalar_t__ isUnsupportedTerm();
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 (isUnsupportedTerm()) {
    size_t len;
    printf("%s", prompt);
    fflush(stdout);
    if (fgets(buf, 4096, stdin) == NULL) {
      return NULL;
    }
    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 NULL;
    }
    return strdup(buf);
  }
}


x86 assembly (clang, -O0):
<linenoise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <isUnsupportedTerm@plt>
cmp    $0x0,%rax
je     12a5 <linenoise+0x135>
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    11df <linenoise+0x6f>
movq   $0x0,-0x8(%rbp)
jmp    12ea <linenoise+0x17a>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x1020(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x1020(%rbp)
mov    %al,-0x1021(%rbp)
je     1257 <linenoise+0xe7>
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     124b <linenoise+0xdb>
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    126a <linenoise+0xfa>
jmp    1290 <linenoise+0x120>
mov    -0x1020(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x1020(%rbp)
mov    -0x1020(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
jmp    11f2 <linenoise+0x82>
lea    -0x1010(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12ea <linenoise+0x17a>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <linenoiseRaw@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0xffffffff,-0x1014(%rbp)
jne    12da <linenoise+0x16a>
movq   $0x0,-0x8(%rbp)
jmp    12ea <linenoise+0x17a>
lea    -0x1010(%rbp),%rdi
call   1090 <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
xor    %eax,%eax
call   1060 <isUnsupportedTerm@plt>
test   %rax,%rax
je     11ec <linenoise+0x7c>
lea    0xe72(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     120b <linenoise+0x9b>
mov    %rsp,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
jne    11db <linenoise+0x6b>
jmp    1201 <linenoise+0x91>
xchg   %ax,%ax
movb   $0x0,-0x1(%rsp,%rax,1)
add    $0xffffffffffffffff,%rax
je     1201 <linenoise+0x91>
movzbl -0x1(%rsp,%rax,1),%ecx
cmp    $0xd,%cl
je     11d0 <linenoise+0x60>
cmp    $0xa,%cl
je     11d0 <linenoise+0x60>
jmp    1201 <linenoise+0x91>
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1040 <linenoiseRaw@plt>
cmp    $0xffffffff,%eax
je     120b <linenoise+0x9b>
mov    %rsp,%rdi
call   1090 <strdup@plt>
jmp    120d <linenoise+0x9d>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
ret

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

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