#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 */
struct termios {
  int /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char getchar();
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
bool get_key(char *usr_in) {
  fprintf(stdout, "key: ");
  static struct termios old, new;
  tcgetattr(0, &old);
  new = old;
  new.c_lflag &= ~ICANON;
  new.c_lflag &= ~ECHO;
  tcsetattr(0, TCSANOW, &new);
  char c;
  int i;
  c = i = 0;
  while ((c = getchar()) != '\n')
    usr_in[i++] = c;
  tcsetattr(0, TCSANOW, &old);
  fprintf(stdout, "\n");
  return i != 0 ? !1 : 1;
}


x86 assembly (clang, -O0):
<get_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
lea    0xea4(%rip),%rsi
call   1040 <fprintf@plt>
xor    %edi,%edi
lea    0x2ede(%rip),%rsi
call   1050 <tcgetattr@plt>
mov    0x2ed3(%rip),%eax
mov    %eax,0x2ed1(%rip)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    0x2ebf(%rip),%eax
mov    %eax,0x2eb9(%rip)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    0x2ea7(%rip),%eax
mov    %eax,0x2ea1(%rip)
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2e8f(%rip),%rdx
call   1060 <tcsetattr@plt>
movl   $0x0,-0x10(%rbp)
movb   $0x0,-0x9(%rbp)
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x9(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     1200 <get_key+0xc0>
mov    -0x9(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11cd <get_key+0x8d>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2e36(%rip),%rdx
call   1060 <tcsetattr@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_key>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e7f(%rip),%r14
mov    (%r14),%edi
lea    0xea5(%rip),%rsi
call   1040 <fprintf@plt>
lea    0x2ee1(%rip),%rsi
xor    %edi,%edi
call   1050 <tcgetattr@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    0x2e62(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    0x2ec0(%rip),%eax
mov    %eax,0x2ebe(%rip)
lea    0x2eb7(%rip),%rdx
mov    0x2e4c(%rip),%r15
mov    (%r15),%esi
xor    %edi,%edi
call   1060 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebp
cmp    $0xa,%al
je     11d1 <get_key+0x91>
mov    %ebp,%eax
cs nopw 0x0(%rax,%rax,1)
nop
mov    %al,(%rbx)
xor    %eax,%eax
call   1030 <getchar@plt>
add    $0x1,%rbx
cmp    $0xa,%al
jne    11c0 <get_key+0x80>
xor    %ebx,%ebx
cmp    $0xa,%bpl
sete   %bl
mov    (%r15),%esi
lea    0x2e64(%rip),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
mov    (%r14),%edi
lea    0xe11(%rip),%rsi
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@stdout = global i32 0
@.str = [6 x i8] c"key: \00"
@get_key.old = internal global %struct.termios zeroinitializer
@get_key.new = internal global %struct.termios zeroinitializer
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @get_key(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @stdout
%6 = call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @tcgetattr(i32 0, %struct.termios* @get_key.old)
%8 = load i32, i32* @ICANON
%9 = xor i32 %8, -1
%10 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @get_key.new, i32 0, i32 0)
%11 = and i32 %10, %9
store i32 %11, i32* getelementptr inbounds (%struct.termios, %struct.termios* @get_key.new, i32 0, i32 0)
%12 = load i32, i32* @ECHO
%13 = xor i32 %12, -1
%14 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @get_key.new, i32 0, i32 0)
%15 = and i32 %14, %13
store i32 %15, i32* getelementptr inbounds (%struct.termios, %struct.termios* @get_key.new, i32 0, i32 0)
%16 = load i32, i32* @TCSANOW
%17 = call i32 @tcsetattr(i32 0, i32 %16, %struct.termios* @get_key.new)
store i32 0, i32* %4
store i8 0, i8* %3
br label %18
18: ; preds = %22, %1
%19 = call signext i8 (...) @getchar()
store i8 %19, i8* %3
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 10
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = load i8, i8* %3
%24 = load i8*, i8** %2
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
store i8 %23, i8* %28
br label %18
29: ; preds = %18
%30 = load i32, i32* @TCSANOW
%31 = call i32 @tcsetattr(i32 0, i32 %30, %struct.termios* @get_key.old)
%32 = load i32, i32* @stdout
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
%36 = zext i1 %35 to i64
%37 = select i1 %35, i32 0, i32 1
ret i32 %37
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdout = global i32 0
@.str = [6 x i8] c"key: \00"
@get_key.old = internal global %struct.termios zeroinitializer
@get_key.new = internal global %struct.termios zeroinitializer
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @get_key(i8* nocapture writeonly %0) {
%2 = load i32, i32* @stdout
%3 = tail call i32 @fprintf(i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @tcgetattr(i32 0, %struct.termios* @get_key.old)
%5 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @get_key.old, i64 0, i32 0)
%6 = load i32, i32* @ICANON
%7 = load i32, i32* @ECHO
%8 = or i32 %6, %7
%9 = xor i32 %8, -1
%10 = and i32 %5, %9
store i32 %10, i32* getelementptr inbounds (%struct.termios, %struct.termios* @get_key.new, i64 0, i32 0)
%11 = load i32, i32* @TCSANOW
%12 = tail call i32 @tcsetattr(i32 0, i32 %11, %struct.termios* @get_key.new)
%13 = tail call signext i8 (...) @getchar()
%14 = icmp eq i8 %13, 10
br i1 %14, label %22, label %15
15: ; preds = %1, %15
%16 = phi i64 [ %18, %15 ], [ 0, %1 ]
%17 = phi i8 [ %20, %15 ], [ %13, %1 ]
%18 = add nuw i64 %16, 1
%19 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 %17, i8* %19
%20 = tail call signext i8 (...) @getchar()
%21 = icmp eq i8 %20, 10
br i1 %21, label %22, label %15
22: ; preds = %15, %1
%23 = load i32, i32* @TCSANOW
%24 = tail call i32 @tcsetattr(i32 0, i32 %23, %struct.termios* @get_key.old)
%25 = load i32, i32* @stdout
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = zext i1 %14 to i32
ret i32 %27
}