#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strdup(char *);
int strlen(char *);
char *readstring(const char *prompt) {
  char buf[256], *ret;
  int i;
  fprintf(stdout, "%s: ", prompt);
  if (!fgets(buf, sizeof(buf), stdin))
    return NULL;
  for (i = strlen(buf) - 1; i >= 0 && buf[i] == '\n'; i--)
    buf[i] = '\0';
  for (i = 0; i < strlen(buf); i++) {
    if (buf[i] == '#') {
      buf[i] = '\0';
      break;
    }
  }
  for (i = strlen(buf) - 1; i >= 0 && buf[i] == ' '; i--)
    buf[i] = '\0';
  if (!isatty(fileno(stdin)))
    printf("%s\n", buf);
  ret = strdup(buf);
  return ret;
}


x86 assembly (clang, -O0):
<readstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe6d(%rip),%rsi
call   1070 <fprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    11c8 <readstring+0x58>
movq   $0x0,-0x8(%rbp)
jmp    1384 <readstring+0x214>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x11c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x11c(%rbp)
mov    %al,-0x11d(%rbp)
jl     120d <readstring+0x9d>
movslq -0x11c(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%eax
cmp    $0xa,%eax
sete   %al
mov    %al,-0x11d(%rbp)
mov    -0x11d(%rbp),%al
test   $0x1,%al
jne    1220 <readstring+0xb0>
jmp    1243 <readstring+0xd3>
movslq -0x11c(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    -0x11c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x11c(%rbp)
jmp    11dd <readstring+0x6d>
movl   $0x0,-0x11c(%rbp)
mov    -0x11c(%rbp),%eax
mov    %eax,-0x124(%rbp)
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x124(%rbp),%eax
cmp    %ecx,%eax
jge    12ba <readstring+0x14a>
movslq -0x11c(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x23,%eax
jne    12a1 <readstring+0x131>
movslq -0x11c(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
jmp    12ba <readstring+0x14a>
jmp    12a6 <readstring+0x136>
mov    -0x11c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x11c(%rbp)
jmp    124d <readstring+0xdd>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x11c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x11c(%rbp)
mov    %al,-0x125(%rbp)
jl     12ff <readstring+0x18f>
movslq -0x11c(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x20,%eax
sete   %al
mov    %al,-0x125(%rbp)
mov    -0x125(%rbp),%al
test   $0x1,%al
jne    1312 <readstring+0x1a2>
jmp    1335 <readstring+0x1c5>
movslq -0x11c(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    -0x11c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x11c(%rbp)
jmp    12cf <readstring+0x15f>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1366 <readstring+0x1f6>
lea    -0x110(%rbp),%rsi
lea    0xca4(%rip),%rdi
call   1050 <printf@plt>
lea    -0x110(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readstring>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%rdx
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2e4d(%rip),%r14
mov    (%r14),%edx
mov    %rsp,%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
test   %eax,%eax
je     121b <readstring+0xab>
mov    %rsp,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11e7 <readstring+0x77>
mov    %eax,%eax
add    $0x1,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    -0x2(%rax),%ecx
cmpb   $0xa,(%rsp,%rcx,1)
jne    11e7 <readstring+0x77>
movb   $0x0,(%rsp,%rcx,1)
add    $0xffffffffffffffff,%rax
cmp    $0x1,%rax
ja     11d0 <readstring+0x60>
mov    %rsp,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1223 <readstring+0xb3>
xor    %ebx,%ebx
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
cmpb   $0x23,(%rsp,%rbx,1)
je     121f <readstring+0xaf>
add    $0x1,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1200 <readstring+0x90>
jmp    1223 <readstring+0xb3>
xor    %eax,%eax
jmp    1281 <readstring+0x111>
movb   $0x0,(%rsp,%rbx,1)
mov    %rsp,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1257 <readstring+0xe7>
mov    %eax,%eax
add    $0x1,%rax
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x2(%rax),%ecx
cmpb   $0x20,(%rsp,%rcx,1)
jne    1257 <readstring+0xe7>
movb   $0x0,(%rsp,%rcx,1)
add    $0xffffffffffffffff,%rax
cmp    $0x1,%rax
ja     1240 <readstring+0xd0>
mov    (%r14),%edi
call   1080 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
test   %eax,%eax
jne    1279 <readstring+0x109>
lea    0xd94(%rip),%rdi
mov    %rsp,%rsi
call   1050 <printf@plt>
mov    %rsp,%rdi
call   1090 <strdup@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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