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

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
int getchar();
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int /*<<< orphan*/ ungetch(int);
void chomp_line() {
  int c;
  struct termios attr;
  tcgetattr(0, &attr);
  attr.c_cc[VMIN] = sizeof(char);
  attr.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(0, TCSANOW, &attr);
  c = getchar();
  attr.c_cc[VMIN] = 0;
  attr.c_lflag |= ICANON | ECHO;
  tcsetattr(0, TCSANOW, &attr);
  ungetch(c);
}


x86 assembly (clang, -O0):
<chomp_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x18(%rbp),%rax
mov    0x2e7a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    0x2e68(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    0x2e1a(%rip),%rcx
or     (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x4(%rbp),%edi
call   1040 <ungetch@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chomp_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x8(%rsp),%r12
xor    %edi,%edi
mov    %r12,%rsi
call   1050 <tcgetattr@plt>
mov    0x8(%rsp),%rax
mov    0x2e6f(%rip),%r14
mov    (%r14),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e4e(%rip),%r15
mov    0x2e5f(%rip),%r13
mov    0x0(%r13),%eax
or     (%r15),%eax
not    %eax
and    %eax,0x10(%rsp)
mov    0x2e53(%rip),%rbx
mov    (%rbx),%esi
xor    %edi,%edi
mov    %r12,%rdx
call   1060 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rax
mov    (%r14),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x0(%r13),%eax
or     (%r15),%eax
or     %eax,0x10(%rsp)
mov    (%rbx),%esi
xor    %edi,%edi
mov    %r12,%rdx
call   1060 <tcsetattr@plt>
mov    %ebp,%edi
call   1040 <ungetch@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
@VMIN = global i64 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define void @chomp_line() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%4 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%5 = load i32*, i32** %4
%6 = load i64, i64* @VMIN
%7 = getelementptr inbounds i32, i32* %5, i64 %6
store i32 1, i32* %7
%8 = load i32, i32* @ICANON
%9 = load i32, i32* @ECHO
%10 = or i32 %8, %9
%11 = xor i32 %10, -1
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%13 = load i32, i32* %12
%14 = and i32 %13, %11
store i32 %14, i32* %12
%15 = load i32, i32* @TCSANOW
%16 = call i32 @tcsetattr(i32 0, i32 %15, %struct.termios* %2)
%17 = call i32 (...) @getchar()
store i32 %17, i32* %1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%19 = load i32*, i32** %18
%20 = load i64, i64* @VMIN
%21 = getelementptr inbounds i32, i32* %19, i64 %20
store i32 0, i32* %21
%22 = load i32, i32* @ICANON
%23 = load i32, i32* @ECHO
%24 = or i32 %22, %23
%25 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%26 = load i32, i32* %25
%27 = or i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* @TCSANOW
%29 = call i32 @tcsetattr(i32 0, i32 %28, %struct.termios* %2)
%30 = load i32, i32* %1
%31 = call i32 @ungetch(i32 %30)
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@VMIN = global i64 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define void @chomp_line() {
%1 = alloca %struct.termios
%2 = bitcast %struct.termios* %1 to i8*
%3 = call i32 @tcgetattr(i32 0, %struct.termios* %1)
%4 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%5 = load i32*, i32** %4
%6 = load i64, i64* @VMIN
%7 = getelementptr inbounds i32, i32* %5, i64 %6
store i32 1, i32* %7
%8 = load i32, i32* @ICANON
%9 = load i32, i32* @ECHO
%10 = or i32 %9, %8
%11 = xor i32 %10, -1
%12 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%13 = load i32, i32* %12
%14 = and i32 %13, %11
store i32 %14, i32* %12
%15 = load i32, i32* @TCSANOW
%16 = call i32 @tcsetattr(i32 0, i32 %15, %struct.termios* %1)
%17 = call i32 (...) @getchar()
%18 = load i32*, i32** %4
%19 = load i64, i64* @VMIN
%20 = getelementptr inbounds i32, i32* %18, i64 %19
store i32 0, i32* %20
%21 = load i32, i32* @ICANON
%22 = load i32, i32* @ECHO
%23 = or i32 %22, %21
%24 = load i32, i32* %12
%25 = or i32 %23, %24
store i32 %25, i32* %12
%26 = load i32, i32* @TCSANOW
%27 = call i32 @tcsetattr(i32 0, i32 %26, %struct.termios* %1)
%28 = call i32 @ungetch(i32 %17)
ret void
}