#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 EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ counting_free(char *);
char *counting_malloc(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fgetc(int /*<<< orphan*/);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, int);
int /*<<< orphan*/ stdin;
char *read_prompt() {
  char *buffer;
  int buffer_size;
  char *p;
  char *current_working_dir;
  int character;
  int length = 0;
  current_working_dir = getcwd(NULL, 0);
  printf("%s> ", current_working_dir);
  counting_free(current_working_dir);
  buffer = counting_malloc(20);
  if (buffer == NULL) {
    perror("Unable to allocate necessary memory");
    exit(EXIT_FAILURE);
  }
  buffer_size = 20;
  p = buffer;
  while (1) {
    character = fgetc(stdin);
    if (character == EOF) {
      if (length > 0) {
        *p = '\0';
      } else {
        counting_free(buffer);
        buffer = NULL;
      }
      break;
    } else if (character == '\n') {
      *p = '\0';
      break;
    }
    *p = character;
    p++;
    length++;
    if (length >= buffer_size) {
      buffer_size += 20;
      buffer = realloc(buffer, buffer_size);
      if (buffer == NULL) {
        perror("Unable to allocate necessary memory");
        exit(EXIT_FAILURE);
      }
      p = buffer + length;
    }
  }
  if (!buffer) {
    printf("\n");
  }
  return buffer;
}


x86 assembly (clang, -O0):
<read_prompt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <counting_free@plt>
mov    $0x14,%edi
call   1070 <counting_malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11ec <read_prompt+0x6c>
lea    0xe2c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x14,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1060 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%eax
jne    124a <read_prompt+0xca>
cmpl   $0x0,-0x28(%rbp)
jle    1234 <read_prompt+0xb4>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    1245 <read_prompt+0xc5>
mov    -0x8(%rbp),%rdi
call   1030 <counting_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <read_prompt+0x163>
cmpl   $0xa,-0x24(%rbp)
jne    1260 <read_prompt+0xe0>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    12e3 <read_prompt+0x163>
jmp    1265 <read_prompt+0xe5>
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     12de <read_prompt+0x15e>
mov    -0xc(%rbp),%eax
add    $0x14,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    12cf <read_prompt+0x14f>
lea    0xd49(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    11fb <read_prompt+0x7b>
cmpq   $0x0,-0x8(%rbp)
jne    12fc <read_prompt+0x17c>
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_prompt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,%rbx
lea    0xe52(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <counting_free@plt>
mov    $0x14,%edi
call   1080 <counting_malloc@plt>
test   %rax,%rax
je     129a <read_prompt+0x10a>
mov    %rax,%r14
mov    0x2e0b(%rip),%r15
mov    (%r15),%edi
call   1070 <fgetc@plt>
mov    0x2df4(%rip),%r13
mov    0x0(%r13),%ecx
cmp    %ecx,%eax
jne    120b <read_prompt+0x7b>
mov    %r14,%rdi
call   1030 <counting_free@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
xor    %r14d,%r14d
jmp    1288 <read_prompt+0xf8>
mov    %ecx,(%rsp)
mov    $0x14,%ebp
mov    $0x1,%r12d
mov    %eax,0x4(%rsp)
mov    %r14,%rbx
jmp    1246 <read_prompt+0xb6>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rbx
mov    (%r15),%edi
call   1070 <fgetc@plt>
add    $0x1,%r12
cmp    0x0(%r13),%eax
je     126f <read_prompt+0xdf>
cmp    $0xa,%eax
je     127c <read_prompt+0xec>
mov    %al,(%rbx)
cmp    %r12d,%ebp
jg     1230 <read_prompt+0xa0>
add    $0x14,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1090 <realloc@plt>
test   %rax,%rax
je     129a <read_prompt+0x10a>
mov    %rax,%r14
mov    %rax,%rbx
add    %r12,%rbx
jmp    1234 <read_prompt+0xa4>
mov    (%rsp),%eax
cmp    %eax,0x4(%rsp)
je     11f4 <read_prompt+0x64>
movb   $0x0,(%rbx)
test   %r14,%r14
je     11fc <read_prompt+0x6c>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd64(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%s> \00"
@.str.1 = [36 x i8] c"Unable to allocate necessary memory\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32 0
@EOF = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define i8* @read_prompt() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %6
%7 = call i8* @getcwd(i32* null, i32 0)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %4
%11 = call i32 @counting_free(i8* %10)
%12 = call i8* @counting_malloc(i32 20)
store i8* %12, i8** %1
%13 = load i8*, i8** %1
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %0
store i32 20, i32* %2
%20 = load i8*, i8** %1
store i8* %20, i8** %3
br label %21
21: ; preds = %19, %70
%22 = load i32, i32* @stdin
%23 = call i32 @fgetc(i32 %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %36
27: ; preds = %21
%28 = load i32, i32* %6
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = load i8*, i8** %3
store i8 0, i8* %31
br label %35
32: ; preds = %27
%33 = load i8*, i8** %1
%34 = call i32 @counting_free(i8* %33)
store i8* null, i8** %1
br label %35
35: ; preds = %32, %30
br label %71
36: ; preds = %21
%37 = load i32, i32* %5
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i8*, i8** %3
store i8 0, i8* %40
br label %71
41: ; preds = %36
br label %42
42: ; preds = %41
%43 = load i32, i32* %5
%44 = trunc i32 %43 to i8
%45 = load i8*, i8** %3
store i8 %44, i8* %45
%46 = load i8*, i8** %3
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %3
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = load i32, i32* %2
%52 = icmp sge i32 %50, %51
br i1 %52, label %53, label %70
53: ; preds = %42
%54 = load i32, i32* %2
%55 = add nsw i32 %54, 20
store i32 %55, i32* %2
%56 = load i8*, i8** %1
%57 = load i32, i32* %2
%58 = call i8* @realloc(i8* %56, i32 %57)
store i8* %58, i8** %1
%59 = load i8*, i8** %1
%60 = icmp eq i8* %59, null
br i1 %60, label %61, label %65
61: ; preds = %53
%62 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %53
%66 = load i8*, i8** %1
%67 = load i32, i32* %6
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
store i8* %69, i8** %3
br label %70
70: ; preds = %65, %42
br label %21
71: ; preds = %39, %35
%72 = load i8*, i8** %1
%73 = icmp ne i8* %72, null
br i1 %73, label %76, label %74
74: ; preds = %71
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %76
76: ; preds = %74, %71
%77 = load i8*, i8** %1
ret i8* %77
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s> \00"
@.str.1 = [36 x i8] c"Unable to allocate necessary memory\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32 0
@EOF = global i32 0
define i8* @read_prompt() {
%1 = tail call i8* @getcwd(i32* null, i32 0)
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %1)
%3 = tail call i32 @counting_free(i8* %1)
%4 = tail call i8* @counting_malloc(i32 20)
%5 = icmp eq i8* %4, null
br i1 %5, label %11, label %6
6: ; preds = %0
%7 = load i32, i32* @stdin
%8 = tail call i32 @fgetc(i32 %7)
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %8, %9
br i1 %10, label %16, label %19
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %42
br i1 %10, label %16, label %50
16: ; preds = %6, %15
%17 = phi i8* [ %45, %15 ], [ %4, %6 ]
%18 = tail call i32 @counting_free(i8* %17)
br label %54
19: ; preds = %6, %42
%20 = phi i64 [ %29, %42 ], [ 0, %6 ]
%21 = phi i32 [ %47, %42 ], [ %8, %6 ]
%22 = phi i8* [ %45, %42 ], [ %4, %6 ]
%23 = phi i32 [ %44, %42 ], [ 20, %6 ]
%24 = phi i8* [ %43, %42 ], [ %4, %6 ]
%25 = icmp eq i32 %21, 10
br i1 %25, label %50, label %26
26: ; preds = %19
%27 = trunc i32 %21 to i8
store i8 %27, i8* %24
%28 = getelementptr inbounds i8, i8* %24, i64 1
%29 = add nuw i64 %20, 1
%30 = trunc i64 %29 to i32
%31 = icmp sgt i32 %23, %30
br i1 %31, label %42, label %32
32: ; preds = %26
%33 = add nsw i32 %23, 20
%34 = tail call i8* @realloc(i8* %22, i32 %33)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = getelementptr inbounds i8, i8* %34, i64 %29
br label %42
42: ; preds = %40, %26
%43 = phi i8* [ %41, %40 ], [ %28, %26 ]
%44 = phi i32 [ %33, %40 ], [ %23, %26 ]
%45 = phi i8* [ %34, %40 ], [ %22, %26 ]
%46 = load i32, i32* @stdin
%47 = tail call i32 @fgetc(i32 %46)
%48 = load i32, i32* @EOF
%49 = icmp eq i32 %47, %48
br i1 %49, label %15, label %19
50: ; preds = %19, %15
%51 = phi i8* [ %43, %15 ], [ %24, %19 ]
%52 = phi i8* [ %45, %15 ], [ %22, %19 ]
store i8 0, i8* %51
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %56
54: ; preds = %16, %50
%55 = tail call i32 @putchar(i32 10)
br label %56
56: ; preds = %54, %50
%57 = phi i8* [ null, %54 ], [ %52, %50 ]
ret i8* %57
}