#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 c_lflag;
};

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
char *fgets(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
char *get_line(const char *prompt, char *dest, size_t length) {
  struct termios tio, tio_orig;
  int tio_fail;
  char *retval;
  tio_fail = tcgetattr(0, &tio_orig);
  if (!tio_fail) {
    tio = tio_orig;
    tio.c_lflag |= ICANON | ECHO;
    tcsetattr(0, TCSAFLUSH, &tio);
  }
  printf("%s: ", prompt);
  fflush(stdout);
  retval = fgets(dest, length, stdin);
  if (!tio_fail)
    tcsetattr(0, TCSAFLUSH, &tio_orig);
  return retval;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %edi,%edi
lea    -0x28(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11ae <get_line+0x5e>
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    0x2e46(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x8(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1204 <get_line+0xb4>
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1060 <tcgetattr@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11a4 <get_line+0x54>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    0x2e53(%rip),%rcx
or     0x8(%rsp),%eax
or     (%rcx),%eax
mov    %eax,0x10(%rsp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1070 <tcsetattr@plt>
lea    0xe55(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <fgets@plt>
mov    %rax,%rbx
test   %ebp,%ebp
jne    11f1 <get_line+0xa1>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1070 <tcsetattr@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
@.str = [5 x i8] c"%s: \00"
@stdout = global i32 0
@stdin = global i32 0
define i8* @get_line(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.termios
%8 = alloca %struct.termios
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = call i32 @tcgetattr(i32 0, %struct.termios* %8)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = icmp ne i32 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %3
%15 = bitcast %struct.termios* %7 to i8*
%16 = bitcast %struct.termios* %8 to i8*
%17 = load i32, i32* @ICANON
%18 = load i32, i32* @ECHO
%19 = or i32 %17, %18
%20 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%21 = load i32, i32* %20
%22 = or i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @TCSAFLUSH
%24 = call i32 @tcsetattr(i32 0, i32 %23, %struct.termios* %7)
br label %25
25: ; preds = %14, %3
%26 = load i8*, i8** %4
%27 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @stdout
%29 = call i32 @fflush(i32 %28)
%30 = load i8*, i8** %5
%31 = load i64, i64* %6
%32 = load i32, i32* @stdin
%33 = call i8* @fgets(i8* %30, i64 %31, i32 %32)
store i8* %33, i8** %10
%34 = load i32, i32* %9
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %25
%37 = load i32, i32* @TCSAFLUSH
%38 = call i32 @tcsetattr(i32 0, i32 %37, %struct.termios* %8)
br label %39
39: ; preds = %36, %25
%40 = load i8*, i8** %10
ret i8* %40
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSAFLUSH = global i32 0
@.str = [5 x i8] c"%s: \00"
@stdout = global i32 0
@stdin = global i32 0
define i8* @get_line(i8* %0, i8* %1, i64 %2) {
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = bitcast %struct.termios* %4 to i8*
%7 = bitcast %struct.termios* %5 to i8*
%8 = call i32 @tcgetattr(i32 0, %struct.termios* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %3
%11 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%12 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%13 = load i32, i32* %11
%14 = load i32, i32* @ICANON
%15 = load i32, i32* @ECHO
%16 = or i32 %14, %13
%17 = or i32 %16, %15
store i32 %17, i32* %12
%18 = load i32, i32* @TCSAFLUSH
%19 = call i32 @tcsetattr(i32 0, i32 %18, %struct.termios* %4)
br label %20
20: ; preds = %10, %3
%21 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = load i32, i32* @stdin
%25 = call i8* @fgets(i8* %1, i64 %2, i32 %24)
br i1 %9, label %26, label %29
26: ; preds = %20
%27 = load i32, i32* @TCSAFLUSH
%28 = call i32 @tcsetattr(i32 0, i32 %27, %struct.termios* %5)
br label %29
29: ; preds = %26, %20
ret i8* %25
}