#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 */
void *emalloc(size_t);
int /*<<< orphan*/ ending(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
char *readline(char *prompt) {
  struct textlist_s {
    char *text;
    struct textlist_s *next;
  };
  char buffer[256];
  char *retval, *rtmp;
  size_t total_len = 0;
  struct textlist_s *list_head = NULL, *list_tail = NULL, *tmp;
  printf("%s", prompt);
  do {
    tmp = emalloc(sizeof *tmp);
    if (fgets(buffer, 256, stdin) == NULL) {
      return NULL;
    }
    tmp->text = strdup(buffer);
    tmp->next = NULL;
    if (!list_head)
      list_head = tmp;
    if (list_tail)
      list_tail->next = tmp;
    list_tail = tmp;
  } while (!ending(tmp->text));
  for (tmp = list_head; tmp; tmp = tmp->next)
    total_len += tmp->next ? 256 - 1 : strlen(tmp->text) + 1;
  if (!total_len)
    return NULL;
  retval = emalloc(total_len);
  for (rtmp = retval, tmp = list_head; tmp; tmp = tmp->next, rtmp += 256 - 1)
    strcpy(rtmp, tmp->text);
  return retval;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x128(%rbp)
movq   $0x0,-0x130(%rbp)
movq   $0x0,-0x138(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1050 <printf@plt>
mov    $0x10,%edi
call   1070 <emalloc@plt>
mov    %rax,-0x140(%rbp)
lea    -0x110(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11f2 <readline+0x82>
movq   $0x0,-0x8(%rbp)
jmp    13aa <readline+0x23a>
lea    -0x110(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    -0x140(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x140(%rbp),%rax
movq   $0x0,0x8(%rax)
cmpq   $0x0,-0x130(%rbp)
jne    1236 <readline+0xc6>
mov    -0x140(%rbp),%rax
mov    %rax,-0x130(%rbp)
cmpq   $0x0,-0x138(%rbp)
je     1256 <readline+0xe6>
mov    -0x140(%rbp),%rcx
mov    -0x138(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x140(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x140(%rbp),%rax
mov    (%rax),%rdi
call   1090 <ending@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b0 <readline+0x40>
mov    -0x130(%rbp),%rax
mov    %rax,-0x140(%rbp)
cmpq   $0x0,-0x140(%rbp)
je     1306 <readline+0x196>
mov    -0x140(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     12c1 <readline+0x151>
mov    $0xff,%eax
mov    %eax,-0x144(%rbp)
jmp    12d9 <readline+0x169>
mov    -0x140(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x144(%rbp)
mov    -0x144(%rbp),%eax
cltq
add    -0x128(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x140(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x140(%rbp)
jmp    1291 <readline+0x121>
cmpq   $0x0,-0x128(%rbp)
jne    1321 <readline+0x1b1>
movq   $0x0,-0x8(%rbp)
jmp    13aa <readline+0x23a>
mov    -0x128(%rbp),%rdi
call   1070 <emalloc@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    -0x130(%rbp),%rax
mov    %rax,-0x140(%rbp)
cmpq   $0x0,-0x140(%rbp)
je     139f <readline+0x22f>
mov    -0x120(%rbp),%rdi
mov    -0x140(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x140(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x140(%rbp)
mov    -0x120(%rbp),%rax
add    $0xff,%rax
mov    %rax,-0x120(%rbp)
jmp    1350 <readline+0x1e0>
mov    -0x118(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%rsi
lea    0xe75(%rip),%rdi
call   1050 <printf@plt>
xor    %r14d,%r14d
mov    0x2e4e(%rip),%r13
mov    %rsp,%r15
xor    %r12d,%r12d
xor    %ebp,%ebp
jmp    11bf <readline+0x4f>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rax,%rdi
call   1090 <ending@plt>
mov    %rbx,%rbp
test   %eax,%eax
jne    120b <readline+0x9b>
mov    $0x10,%edi
call   1070 <emalloc@plt>
mov    %rax,%rbx
mov    0x0(%r13),%edx
mov    %r15,%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     1292 <readline+0x122>
mov    %r15,%rdi
call   1080 <strdup@plt>
mov    %rax,(%rbx)
movq   $0x0,0x8(%rbx)
test   %r12,%r12
cmove  %rbx,%r12
test   %rbp,%rbp
je     11b0 <readline+0x40>
mov    %rbx,0x8(%rbp)
jmp    11b0 <readline+0x40>
test   %r12,%r12
je     128f <readline+0x11f>
xor    %ebx,%ebx
mov    %r12,%rbp
jmp    122d <readline+0xbd>
nopl   0x0(%rax,%rax,1)
cltq
add    %rax,%rbx
mov    %rcx,%rbp
test   %rcx,%rcx
je     124d <readline+0xdd>
mov    0x8(%rbp),%rcx
mov    $0xff,%eax
test   %rcx,%rcx
jne    1220 <readline+0xb0>
mov    0x0(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    0x8(%rbp),%rcx
jmp    1220 <readline+0xb0>
test   %rbx,%rbx
je     128f <readline+0x11f>
mov    %rbx,%rdi
call   1070 <emalloc@plt>
mov    %rax,%r14
test   %r12,%r12
je     1292 <readline+0x122>
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    0x8(%r12),%r12
add    $0xff,%rbx
test   %r12,%r12
jne    1270 <readline+0x100>
jmp    1292 <readline+0x122>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.textlist_s = type { i8*, %struct.textlist_s* }
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
define i8* @readline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.textlist_s*
%9 = alloca %struct.textlist_s*
%10 = alloca %struct.textlist_s*
store i8* %0, i8** %3
store i64 0, i64* %7
store %struct.textlist_s* null, %struct.textlist_s** %8
store %struct.textlist_s* null, %struct.textlist_s** %9
%11 = load i8*, i8** %3
%12 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %13
13: ; preds = %41, %1
%14 = call i8* @emalloc(i64 16)
%15 = bitcast i8* %14 to %struct.textlist_s*
store %struct.textlist_s* %15, %struct.textlist_s** %10
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* @stdin
%18 = call i32* @fgets(i8* %16, i32 256, i32 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %13
store i8* null, i8** %2
br label %100
21: ; preds = %13
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%23 = call i8* @strdup(i8* %22)
%24 = load %struct.textlist_s*, %struct.textlist_s** %10
%25 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %24, i32 0, i32 0
store i8* %23, i8** %25
%26 = load %struct.textlist_s*, %struct.textlist_s** %10
%27 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %26, i32 0, i32 1
store %struct.textlist_s* null, %struct.textlist_s** %27
%28 = load %struct.textlist_s*, %struct.textlist_s** %8
%29 = icmp ne %struct.textlist_s* %28, null
br i1 %29, label %32, label %30
30: ; preds = %21
%31 = load %struct.textlist_s*, %struct.textlist_s** %10
store %struct.textlist_s* %31, %struct.textlist_s** %8
br label %32
32: ; preds = %30, %21
%33 = load %struct.textlist_s*, %struct.textlist_s** %9
%34 = icmp ne %struct.textlist_s* %33, null
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load %struct.textlist_s*, %struct.textlist_s** %10
%37 = load %struct.textlist_s*, %struct.textlist_s** %9
%38 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %37, i32 0, i32 1
store %struct.textlist_s* %36, %struct.textlist_s** %38
br label %39
39: ; preds = %35, %32
%40 = load %struct.textlist_s*, %struct.textlist_s** %10
store %struct.textlist_s* %40, %struct.textlist_s** %9
br label %41
41: ; preds = %39
%42 = load %struct.textlist_s*, %struct.textlist_s** %10
%43 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @ending(i8* %44)
%46 = icmp ne i32 %45, 0
%47 = xor i1 %46, true
br i1 %47, label %13, label %48
48: ; preds = %41
%49 = load %struct.textlist_s*, %struct.textlist_s** %8
store %struct.textlist_s* %49, %struct.textlist_s** %10
br label %50
50: ; preds = %70, %48
%51 = load %struct.textlist_s*, %struct.textlist_s** %10
%52 = icmp ne %struct.textlist_s* %51, null
br i1 %52, label %53, label %74
53: ; preds = %50
%54 = load %struct.textlist_s*, %struct.textlist_s** %10
%55 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %54, i32 0, i32 1
%56 = load %struct.textlist_s*, %struct.textlist_s** %55
%57 = icmp ne %struct.textlist_s* %56, null
br i1 %57, label %58, label %59
58: ; preds = %53
br label %65
59: ; preds = %53
%60 = load %struct.textlist_s*, %struct.textlist_s** %10
%61 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %60, i32 0, i32 0
%62 = load i8*, i8** %61
%63 = call i32 @strlen(i8* %62)
%64 = add nsw i32 %63, 1
br label %65
65: ; preds = %59, %58
%66 = phi i32 [ 255, %58 ], [ %64, %59 ]
%67 = sext i32 %66 to i64
%68 = load i64, i64* %7
%69 = add i64 %68, %67
store i64 %69, i64* %7
br label %70
70: ; preds = %65
%71 = load %struct.textlist_s*, %struct.textlist_s** %10
%72 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %71, i32 0, i32 1
%73 = load %struct.textlist_s*, %struct.textlist_s** %72
store %struct.textlist_s* %73, %struct.textlist_s** %10
br label %50
74: ; preds = %50
%75 = load i64, i64* %7
%76 = icmp ne i64 %75, 0
br i1 %76, label %78, label %77
77: ; preds = %74
store i8* null, i8** %2
br label %100
78: ; preds = %74
%79 = load i64, i64* %7
%80 = call i8* @emalloc(i64 %79)
store i8* %80, i8** %5
%81 = load i8*, i8** %5
store i8* %81, i8** %6
%82 = load %struct.textlist_s*, %struct.textlist_s** %8
store %struct.textlist_s* %82, %struct.textlist_s** %10
br label %83
83: ; preds = %92, %78
%84 = load %struct.textlist_s*, %struct.textlist_s** %10
%85 = icmp ne %struct.textlist_s* %84, null
br i1 %85, label %86, label %98
86: ; preds = %83
%87 = load i8*, i8** %6
%88 = load %struct.textlist_s*, %struct.textlist_s** %10
%89 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %88, i32 0, i32 0
%90 = load i8*, i8** %89
%91 = call i32 @strcpy(i8* %87, i8* %90)
br label %92
92: ; preds = %86
%93 = load %struct.textlist_s*, %struct.textlist_s** %10
%94 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %93, i32 0, i32 1
%95 = load %struct.textlist_s*, %struct.textlist_s** %94
store %struct.textlist_s* %95, %struct.textlist_s** %10
%96 = load i8*, i8** %6
%97 = getelementptr inbounds i8, i8* %96, i64 255
store i8* %97, i8** %6
br label %83
98: ; preds = %83
%99 = load i8*, i8** %5
store i8* %99, i8** %2
br label %100
100: ; preds = %98, %77, %20
%101 = load i8*, i8** %2
ret i8* %101
}

LLVM IR (-O2):
%struct.textlist_s = type { i8*, %struct.textlist_s* }
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
define i8* @readline(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %5
5: ; preds = %24, %1
%6 = phi %struct.textlist_s* [ null, %1 ], [ %19, %24 ]
%7 = phi %struct.textlist_s* [ null, %1 ], [ %9, %24 ]
%8 = call i8* @emalloc(i64 16)
%9 = bitcast i8* %8 to %struct.textlist_s*
%10 = load i32, i32* @stdin
%11 = call i32* @fgets(i8* %3, i32 256, i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %61, label %13
13: ; preds = %5
%14 = call i8* @strdup(i8* %3)
%15 = bitcast i8* %8 to i8**
store i8* %14, i8** %15
%16 = getelementptr inbounds i8, i8* %8, i64 8
%17 = bitcast i8* %16 to %struct.textlist_s**
store %struct.textlist_s* null, %struct.textlist_s** %17
%18 = icmp eq %struct.textlist_s* %6, null
%19 = select i1 %18, %struct.textlist_s* %9, %struct.textlist_s* %6
%20 = icmp eq %struct.textlist_s* %7, null
br i1 %20, label %24, label %21
21: ; preds = %13
%22 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %7, i64 0, i32 1
%23 = bitcast %struct.textlist_s** %22 to i8**
store i8* %8, i8** %23
br label %24
24: ; preds = %21, %13
%25 = call i32 @ending(i8* %14)
%26 = icmp eq i32 %25, 0
br i1 %26, label %5, label %27
27: ; preds = %24
%28 = icmp eq %struct.textlist_s* %19, null
br i1 %28, label %61, label %29
29: ; preds = %27, %41
%30 = phi %struct.textlist_s* [ %42, %41 ], [ %19, %27 ]
%31 = phi i64 [ %45, %41 ], [ 0, %27 ]
%32 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %30, i64 0, i32 1
%33 = load %struct.textlist_s*, %struct.textlist_s** %32
%34 = icmp eq %struct.textlist_s* %33, null
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %30, i64 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @strlen(i8* %37)
%39 = add nsw i32 %38, 1
%40 = load %struct.textlist_s*, %struct.textlist_s** %32
br label %41
41: ; preds = %29, %35
%42 = phi %struct.textlist_s* [ %40, %35 ], [ %33, %29 ]
%43 = phi i32 [ %39, %35 ], [ 255, %29 ]
%44 = sext i32 %43 to i64
%45 = add i64 %31, %44
%46 = icmp eq %struct.textlist_s* %42, null
br i1 %46, label %47, label %29
47: ; preds = %41
%48 = icmp eq i64 %45, 0
br i1 %48, label %61, label %49
49: ; preds = %47
%50 = call i8* @emalloc(i64 %45)
br i1 %28, label %61, label %51
51: ; preds = %49, %51
%52 = phi %struct.textlist_s* [ %58, %51 ], [ %19, %49 ]
%53 = phi i8* [ %59, %51 ], [ %50, %49 ]
%54 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %52, i64 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @strcpy(i8* %53, i8* %55)
%57 = getelementptr inbounds %struct.textlist_s, %struct.textlist_s* %52, i64 0, i32 1
%58 = load %struct.textlist_s*, %struct.textlist_s** %57
%59 = getelementptr inbounds i8, i8* %53, i64 255
%60 = icmp eq %struct.textlist_s* %58, null
br i1 %60, label %61, label %51
61: ; preds = %5, %51, %27, %49, %47
%62 = phi i8* [ null, %47 ], [ %50, %49 ], [ null, %27 ], [ %50, %51 ], [ null, %5 ]
ret i8* %62
}