#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 */
scalar_t__ feof(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ isspace(char);
char *last_error();
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
char *readline(const char *prompt) {
  char *buf = malloc(128);
  if (!buf) {
    fprintf(stdout, "readline: can't allocate memory: %s\n", last_error());
    return NULL;
  }
  for (;;) {
    printf("%s", prompt);
    fflush(stdout);
    if (fgets(buf, 128, stdin)) {
      int len = strlen(buf);
      while (len > 0 && isspace(buf[len - 1]))
        len--;
      buf[len] = 0;
      return buf;
    }
    if (feof(stdin))
      break;
    printf("\n");
  }
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x80,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f7 <readline+0x57>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <last_error@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe1b(%rip),%rsi
call   1090 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f0 <readline+0x150>
jmp    11fc <readline+0x5c>
mov    -0x10(%rbp),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12af <readline+0x10f>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x21(%rbp)
jle    1278 <readline+0xd8>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1288 <readline+0xe8>
jmp    1296 <readline+0xf6>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1249 <readline+0xa9>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f0 <readline+0x150>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10a0 <feof@plt>
cmp    $0x0,%rax
je     12cc <readline+0x12c>
jmp    12df <readline+0x13f>
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11fc <readline+0x5c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x80,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     12a6 <readline+0xf6>
mov    %rax,%rbx
lea    0xe4a(%rip),%rdi
xor    %r12d,%r12d
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2de9(%rip),%r13
mov    0x0(%r13),%edi
call   10d0 <fflush@plt>
mov    0x2de9(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1080 <fgets@plt>
test   %rax,%rax
jne    1266 <readline+0xb6>
lea    0xe0a(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
call   10b0 <feof@plt>
test   %rax,%rax
jne    12cb <readline+0x11b>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x0(%r13),%edi
call   10d0 <fflush@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1080 <fgets@plt>
test   %rax,%rax
je     1220 <readline+0x70>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
test   %eax,%eax
cmovs  %eax,%r12d
cs nopw 0x0(%rax,%rax,1)
test   %ebp,%ebp
jle    129d <readline+0xed>
movsbl -0x1(%rbx,%rbp,1),%edi
add    $0xffffffffffffffff,%rbp
call   1070 <isspace@plt>
test   %rax,%rax
jne    1280 <readline+0xd0>
add    $0x1,%ebp
mov    %ebp,%r12d
movslq %r12d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    12d5 <readline+0x125>
mov    0x2d2b(%rip),%rax
mov    (%rax),%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <last_error@plt>
lea    0xd41(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
jmp    12d5 <readline+0x125>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [37 x i8] c"readline: can't allocate memory: %s\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define i8* @readline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i8* @malloc(i32 128)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i32, i32* @stdout
%11 = call i8* (...) @last_error()
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %11)
store i8* null, i8** %2
br label %59
13: ; preds = %1
br label %14
14: ; preds = %54, %13
%15 = load i8*, i8** %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @stdout
%18 = call i32 @fflush(i32 %17)
%19 = load i8*, i8** %4
%20 = load i32, i32* @stdin
%21 = call i64 @fgets(i8* %19, i32 128, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %49
23: ; preds = %14
%24 = load i8*, i8** %4
%25 = call i32 @strlen(i8* %24)
store i32 %25, i32* %5
br label %26
26: ; preds = %40, %23
%27 = load i32, i32* %5
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i8*, i8** %4
%31 = load i32, i32* %5
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
%35 = load i8, i8* %34
%36 = call i64 @isspace(i8 signext %35)
%37 = icmp ne i64 %36, 0
br label %38
38: ; preds = %29, %26
%39 = phi i1 [ false, %26 ], [ %37, %29 ]
br i1 %39, label %40, label %43
40: ; preds = %38
%41 = load i32, i32* %5
%42 = add nsw i32 %41, -1
store i32 %42, i32* %5
br label %26
43: ; preds = %38
%44 = load i8*, i8** %4
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %4
store i8* %48, i8** %2
br label %59
49: ; preds = %14
%50 = load i32, i32* @stdin
%51 = call i64 @feof(i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %49
br label %56
54: ; preds = %49
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %14
56: ; preds = %53
%57 = load i8*, i8** %4
%58 = call i32 @free(i8* %57)
store i8* null, i8** %2
br label %59
59: ; preds = %56, %43, %9
%60 = load i8*, i8** %2
ret i8* %60
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [37 x i8] c"readline: can't allocate memory: %s\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@stdin = global i32 0
define i8* @readline(i8* %0) {
%2 = tail call dereferenceable_or_null(128) i8* @malloc(i32 128)
%3 = icmp eq i8* %2, null
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%6 = load i32, i32* @stdout
%7 = tail call i32 @fflush(i32 %6)
%8 = load i32, i32* @stdin
%9 = tail call i64 @fgets(i8* %2, i32 128, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %35, label %15
11: ; preds = %1
%12 = load i32, i32* @stdout
%13 = tail call i8* (...) @last_error()
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %49
15: ; preds = %39, %4
%16 = tail call i32 @strlen(i8* %2)
%17 = zext i32 %16 to i64
%18 = call i32 @llvm.smin.i32(i32 %16, i32 0)
br label %19
19: ; preds = %23, %15
%20 = phi i64 [ %24, %23 ], [ %17, %15 ]
%21 = trunc i64 %20 to i32
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %19
%24 = add nsw i64 %20, -1
%25 = getelementptr inbounds i8, i8* %2, i64 %24
%26 = load i8, i8* %25
%27 = tail call i64 @isspace(i8 signext %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %19
29: ; preds = %23
%30 = trunc i64 %20 to i32
br label %31
31: ; preds = %19, %29
%32 = phi i32 [ %30, %29 ], [ %18, %19 ]
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %2, i64 %33
store i8 0, i8* %34
br label %49
35: ; preds = %4, %39
%36 = load i32, i32* @stdin
%37 = tail call i64 @feof(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %35
%40 = tail call i32 @putchar(i32 10)
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%42 = load i32, i32* @stdout
%43 = tail call i32 @fflush(i32 %42)
%44 = load i32, i32* @stdin
%45 = tail call i64 @fgets(i8* %2, i32 128, i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %35, label %15
47: ; preds = %35
%48 = tail call i32 @free(i8* %2)
br label %49
49: ; preds = %47, %31, %11
%50 = phi i8* [ %2, %31 ], [ null, %47 ], [ null, %11 ]
ret i8* %50
}