#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 */
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
char *strdup(char *);
char *readline(char *prompt) {
  char buffer[256];
  char *result;
  if (prompt) {
    printf("%s ", prompt);
  }
  result = fgets(buffer, sizeof(buffer), stdin);
  if (result) {
    result = strdup(result);
  }
  return result;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     115a <readline+0x2a>
mov    -0x8(%rbp),%rsi
lea    0xeab(%rip),%rdi
call   1030 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1040 <fgets@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
je     119c <readline+0x6c>
mov    -0x118(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
sub    $0x108,%rsp
test   %rdi,%rdi
je     114b <readline+0x1b>
mov    %rdi,%rsi
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x100,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     1176 <readline+0x46>
mov    %rax,%rdi
call   1050 <strdup@plt>
add    $0x108,%rsp
ret
xor    %eax,%eax
add    $0x108,%rsp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s \00"
@stdin = global i32 0
define i8* @readline(i8* %0) {
%2 = alloca i8*
%3 = alloca [256 x i8]
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %7, %1
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* @stdin
%13 = call i8* @fgets(i8* %11, i32 256, i32 %12)
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i8*, i8** %4
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %4
br label %19
19: ; preds = %16, %10
%20 = load i8*, i8** %4
ret i8* %20
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s \00"
@stdin = global i32 0
define noalias i8* @readline(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %7
7: ; preds = %5, %1
%8 = load i32, i32* @stdin
%9 = call i8* @fgets(i8* %3, i32 256, i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = call i8* @strdup(i8* %9)
br label %13
13: ; preds = %11, %7
%14 = phi i8* [ %12, %11 ], [ null, %7 ]
ret i8* %14
}