#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);
    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    $0x1020,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <isUnsupportedTerm@plt>
cmp    $0x0,%rax
je     1207 <linenoise+0x97>
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    124c <linenoise+0xdc>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x1020(%rbp)
lea    -0x1010(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    124c <linenoise+0xdc>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <linenoiseRaw@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0xffffffff,-0x1014(%rbp)
jne    123c <linenoise+0xcc>
movq   $0x0,-0x8(%rbp)
jmp    124c <linenoise+0xdc>
lea    -0x1010(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linenoise>:
push   %rbx
sub    $0x1000,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <isUnsupportedTerm@plt>
test   %rax,%rax
je     11b9 <linenoise+0x59>
lea    0xe82(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11ce <linenoise+0x6e>
mov    %rsp,%rdi
call   1080 <strdup@plt>
jmp    11d0 <linenoise+0x70>
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1030 <linenoiseRaw@plt>
cmp    $0xffffffff,%eax
jne    11af <linenoise+0x4f>
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 %24
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 %34
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
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%23 = call i8* @strdup(i8* %22)
store i8* %23, i8** %2
br label %34
24: ; preds = %1
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%26 = load i8*, i8** %3
%27 = call i32 @linenoiseRaw(i8* %25, i32 4096, i8* %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %24
store i8* null, i8** %2
br label %34
31: ; preds = %24
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%33 = call i8* @strdup(i8* %32)
store i8* %33, i8** %2
br label %34
34: ; preds = %31, %30, %19, %18
%35 = load i8*, i8** %2
ret i8* %35
}

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 %13, 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 %18, label %16
13: ; preds = %1
%14 = call i32 @linenoiseRaw(i8* %3, i32 4096, i8* %0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %18, label %16
16: ; preds = %13, %6
%17 = call i8* @strdup(i8* %3)
br label %18
18: ; preds = %16, %13, %6
%19 = phi i8* [ null, %6 ], [ null, %13 ], [ %17, %16 ]
ret i8* %19
}