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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ m_burn(char *, int);
char *m_strdup(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
unsigned int strlen(char *);
char *get_response(char *prompt) {
  FILE *tty = NULL;
  char *response = NULL;
  char buf[1024];
  char *ret = NULL;
  fprintf(stderr, "%s", prompt);
  tty = fopen("/dev/tty", "r");
  if (tty) {
    ret = fgets(buf, sizeof(buf), tty);
    fclose(tty);
  } else {
    ret = fgets(buf, sizeof(buf), stdin);
  }
  if (ret == NULL) {
    response = m_strdup("");
  } else {
    unsigned int buflen = strlen(buf);
    if (buflen > 0 && buf[buflen - 1] == '\n')
      buf[buflen - 1] = '\0';
    response = m_strdup(buf);
  }
  m_burn(buf, sizeof(buf));
  return response;
}


x86 assembly (clang, -O0):
<get_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x428(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe52(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xe49(%rip),%rdi
lea    0xe4b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ff <get_response+0x8f>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %rax,-0x428(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1221 <get_response+0xb1>
lea    -0x420(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1244 <get_response+0xd4>
lea    0xdd7(%rip),%rdi
call   1090 <m_strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    12a2 <get_response+0x132>
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jbe    1292 <get_response+0x122>
mov    -0x42c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,%eax
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1292 <get_response+0x122>
mov    -0x42c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,%eax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
call   1090 <m_strdup@plt>
mov    %rax,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   1050 <m_burn@plt>
mov    -0x18(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_response>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rdx
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1070 <fprintf@plt>
lea    0xe6a(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11eb <get_response+0x7b>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     120a <get_response+0x9a>
mov    %rsp,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     11e6 <get_response+0x76>
add    $0xffffffff,%eax
cmpb   $0xa,(%rsp,%rax,1)
jne    11e6 <get_response+0x76>
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
jmp    1211 <get_response+0xa1>
mov    0x2df6(%rip),%rax
mov    (%rax),%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %rax,%r14
test   %r14,%r14
jne    11cd <get_response+0x5d>
lea    0xdfc(%rip),%rdi
call   1090 <m_strdup@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1050 <m_burn@plt>
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [9 x i8] c"/dev/tty\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.3 = [1 x i8] zeroinitializer
define i8* @get_response(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %2
store i32* null, i32** %3
store i8* null, i8** %4
store i8* null, i8** %6
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %2
%10 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %11, i32** %3
%12 = load i32*, i32** %3
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = load i32*, i32** %3
%17 = call i8* @fgets(i8* %15, i32 1024, i32* %16)
store i8* %17, i8** %6
%18 = load i32*, i32** %3
%19 = call i32 @fclose(i32* %18)
br label %24
20: ; preds = %1
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = load i32*, i32** @stdin
%23 = call i8* @fgets(i8* %21, i32 1024, i32* %22)
store i8* %23, i8** %6
br label %24
24: ; preds = %20, %14
%25 = load i8*, i8** %6
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i8* @m_strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
store i8* %28, i8** %4
br label %50
29: ; preds = %24
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp ugt i32 %32, 0
br i1 %33, label %34, label %47
34: ; preds = %29
%35 = load i32, i32* %7
%36 = sub i32 %35, 1
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 10
br i1 %41, label %42, label %47
42: ; preds = %34
%43 = load i32, i32* %7
%44 = sub i32 %43, 1
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %45
store i8 0, i8* %46
br label %47
47: ; preds = %42, %34, %29
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%49 = call i8* @m_strdup(i8* %48)
store i8* %49, i8** %4
br label %50
50: ; preds = %47, %27
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%52 = call i32 @m_burn(i8* %51, i32 1024)
%53 = load i8*, i8** %4
ret i8* %53
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [9 x i8] c"/dev/tty\00"
@.str.2 = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.3 = [1 x i8] zeroinitializer
define i8* @get_response(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = call i8* @fgets(i8* %3, i32 1024, i32* %6)
%10 = call i32 @fclose(i32* %6)
br label %14
11: ; preds = %1
%12 = load i32*, i32** @stdin
%13 = call i8* @fgets(i8* %3, i32 1024, i32* %12)
br label %14
14: ; preds = %11, %8
%15 = phi i8* [ %9, %8 ], [ %13, %11 ]
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i8* @m_strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
br label %31
19: ; preds = %14
%20 = call i32 @strlen(i8* %3)
%21 = icmp eq i32 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %19
%23 = add i32 %20, -1
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 10
br i1 %27, label %28, label %29
28: ; preds = %22
store i8 0, i8* %25
br label %29
29: ; preds = %28, %22, %19
%30 = call i8* @m_strdup(i8* %3)
br label %31
31: ; preds = %29, %17
%32 = phi i8* [ %18, %17 ], [ %30, %29 ]
%33 = call i32 @m_burn(i8* %3, i32 1024)
ret i8* %32
}