#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_cflag;
  int c_iflag;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
};
typedef int /*<<< orphan*/ newtty;

/* Variables and functions */
int B9600;
int CLOCAL;
int CREAD;
int CS8;
int ICRNL;
int IGNPAR;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VEOF;
size_t VMIN;
int /*<<< orphan*/ exit(int);
char *malloc(int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  struct termios oldtty, newtty;
  int ttyfd, i = 0;
  char *inp = malloc(64 * sizeof(char));
  char c;
  ttyfd = open("/dev/pts/2", O_RDWR | O_NOCTTY);
  if (ttyfd < 0) {
    perror("/dev/pts/2");
    exit(-1);
  }
  tcgetattr(ttyfd, &oldtty);
  memset(&newtty, 0, sizeof(newtty));
  newtty.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  newtty.c_iflag = IGNPAR | ICRNL;
  newtty.c_oflag = 0;
  newtty.c_lflag = 0;
  newtty.c_cc[VEOF] = 4;
  newtty.c_cc[VMIN] = 1;
  tcflush(ttyfd, TCIFLUSH);
  tcsetattr(ttyfd, TCSANOW, &newtty);
  i = read(1, inp, 63);
  inp[i + 1] = '\0';
  write(ttyfd, inp, 64);
  tcflush(ttyfd, TCIFLUSH);
  for (i = 0; i < 64; i++) {
    read(ttyfd, &inp[i], 1);
    if (inp[i] == '\0')
      break;
  }
  printf("%s", inp);
  tcsetattr(ttyfd, TCSANOW, &oldtty);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x50(%rbp)
mov    $0x40,%edi
call   1080 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
or     (%rax),%esi
lea    0xe13(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1215 <main+0x65>
lea    0xdfa(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x4c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <tcgetattr@plt>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    0x2d7f(%rip),%rcx
or     (%rcx),%eax
mov    0x2d3e(%rip),%rcx
or     (%rcx),%eax
mov    0x2d75(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d39(%rip),%rax
mov    (%rax),%eax
mov    0x2d78(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2cf8(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x4,(%rax,%rcx,4)
mov    -0x40(%rbp),%rax
mov    0x2d1b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x4c(%rbp),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x4c(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   10a0 <tcsetattr@plt>
mov    -0x58(%rbp),%rsi
mov    $0x1,%edi
mov    $0x3f,%edx
call   1070 <read@plt>
mov    %eax,-0x50(%rbp)
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x4c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    $0x40,%edx
call   1030 <write@plt>
mov    -0x4c(%rbp),%edi
mov    0x2c99(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
movl   $0x0,-0x50(%rbp)
cmpl   $0x40,-0x50(%rbp)
jge    136c <main+0x1bc>
mov    -0x4c(%rbp),%edi
mov    -0x58(%rbp),%rsi
movslq -0x50(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x58(%rbp),%rax
movslq -0x50(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1359 <main+0x1a9>
jmp    136c <main+0x1bc>
jmp    135e <main+0x1ae>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    131d <main+0x16d>
mov    -0x58(%rbp),%rsi
lea    0xc94(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4c(%rbp),%edi
mov    0x2c52(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   10a0 <tcsetattr@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    $0x40,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    0x2e16(%rip),%rax
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe1c(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1345 <main+0x195>
mov    %eax,%r15d
lea    0x28(%rsp),%rsi
mov    %eax,%edi
call   1090 <tcgetattr@plt>
lea    0x8(%rsp),%r14
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2db3(%rip),%rax
mov    0x2d9c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d59(%rip),%rax
or     (%rax),%ecx
mov    0x2d90(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2d53(%rip),%rax
mov    0x2d94(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0xc(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2d18(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x4,(%rax,%rcx,4)
mov    0x2d3f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d1e(%rip),%r13
mov    0x0(%r13),%esi
mov    %r15d,%edi
call   1040 <tcflush@plt>
mov    0x2d3b(%rip),%r12
mov    (%r12),%esi
mov    %r15d,%edi
mov    %r14,%rdx
call   10a0 <tcsetattr@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x3f,%edx
call   1070 <read@plt>
cltq
movb   $0x0,0x1(%rax,%rbx,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1030 <write@plt>
mov    0x0(%r13),%esi
mov    %r15d,%edi
call   1040 <tcflush@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%rbp,1),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
call   1070 <read@plt>
cmpb   $0x0,(%rbx,%rbp,1)
je     1314 <main+0x164>
lea    0x1(%rbp),%rax
cmp    $0x3f,%rbp
mov    %rax,%rbp
jne    12f0 <main+0x140>
lea    0xcf0(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    (%r12),%esi
lea    0x28(%rsp),%rdx
mov    %r15d,%edi
call   10a0 <tcsetattr@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb4(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@.str = [11 x i8] c"/dev/pts/2\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B9600 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@ICRNL = global i32 0
@VEOF = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8
store i32 0, i32* %1
store i32 0, i32* %5
%8 = call i8* @malloc(i32 64)
store i8* %8, i8** %6
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %0
%19 = load i32, i32* %4
%20 = call i32 @tcgetattr(i32 %19, %struct.termios* %2)
%21 = call i32 @memset(%struct.termios* %3, i32 0, i32 32)
%22 = load i32, i32* @B9600
%23 = load i32, i32* @CS8
%24 = or i32 %22, %23
%25 = load i32, i32* @CLOCAL
%26 = or i32 %24, %25
%27 = load i32, i32* @CREAD
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @IGNPAR
%31 = load i32, i32* @ICRNL
%32 = or i32 %30, %31
%33 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 4
store i64 0, i64* %34
%35 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 3
store i64 0, i64* %35
%36 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
%37 = load i32*, i32** %36
%38 = load i64, i64* @VEOF
%39 = getelementptr inbounds i32, i32* %37, i64 %38
store i32 4, i32* %39
%40 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
%41 = load i32*, i32** %40
%42 = load i64, i64* @VMIN
%43 = getelementptr inbounds i32, i32* %41, i64 %42
store i32 1, i32* %43
%44 = load i32, i32* %4
%45 = load i32, i32* @TCIFLUSH
%46 = call i32 @tcflush(i32 %44, i32 %45)
%47 = load i32, i32* %4
%48 = load i32, i32* @TCSANOW
%49 = call i32 @tcsetattr(i32 %47, i32 %48, %struct.termios* %3)
%50 = load i8*, i8** %6
%51 = call i32 @read(i32 1, i8* %50, i32 63)
store i32 %51, i32* %5
%52 = load i8*, i8** %6
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
store i8 0, i8* %56
%57 = load i32, i32* %4
%58 = load i8*, i8** %6
%59 = call i32 @write(i32 %57, i8* %58, i32 64)
%60 = load i32, i32* %4
%61 = load i32, i32* @TCIFLUSH
%62 = call i32 @tcflush(i32 %60, i32 %61)
store i32 0, i32* %5
br label %63
63: ; preds = %82, %18
%64 = load i32, i32* %5
%65 = icmp slt i32 %64, 64
br i1 %65, label %66, label %85
66: ; preds = %63
%67 = load i32, i32* %4
%68 = load i8*, i8** %6
%69 = load i32, i32* %5
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = call i32 @read(i32 %67, i8* %71, i32 1)
%73 = load i8*, i8** %6
%74 = load i32, i32* %5
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %66
br label %85
81: ; preds = %66
br label %82
82: ; preds = %81
%83 = load i32, i32* %5
%84 = add nsw i32 %83, 1
store i32 %84, i32* %5
br label %63
85: ; preds = %80, %63
%86 = load i8*, i8** %6
%87 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %86)
%88 = load i32, i32* %4
%89 = load i32, i32* @TCSANOW
%90 = call i32 @tcsetattr(i32 %88, i32 %89, %struct.termios* %2)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@.str = [11 x i8] c"/dev/pts/2\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B9600 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@ICRNL = global i32 0
@VEOF = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = tail call dereferenceable_or_null(64) i8* @malloc(i32 64)
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = call i32 @tcgetattr(i32 %9, %struct.termios* %1)
%16 = call i32 @memset(%struct.termios* %2, i32 0, i32 32)
%17 = load i32, i32* @B9600
%18 = load i32, i32* @CS8
%19 = or i32 %18, %17
%20 = load i32, i32* @CLOCAL
%21 = or i32 %19, %20
%22 = load i32, i32* @CREAD
%23 = or i32 %21, %22
%24 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @IGNPAR
%26 = load i32, i32* @ICRNL
%27 = or i32 %26, %25
%28 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%30 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%31 = bitcast i64* %29 to i8*
%32 = load i32*, i32** %30
%33 = load i64, i64* @VEOF
%34 = getelementptr inbounds i32, i32* %32, i64 %33
store i32 4, i32* %34
%35 = load i64, i64* @VMIN
%36 = getelementptr inbounds i32, i32* %32, i64 %35
store i32 1, i32* %36
%37 = load i32, i32* @TCIFLUSH
%38 = call i32 @tcflush(i32 %9, i32 %37)
%39 = load i32, i32* @TCSANOW
%40 = call i32 @tcsetattr(i32 %9, i32 %39, %struct.termios* %2)
%41 = call i32 @read(i32 1, i8* %5, i32 63)
%42 = add nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %5, i64 %43
store i8 0, i8* %44
%45 = call i32 @write(i32 %9, i8* %5, i32 64)
%46 = load i32, i32* @TCIFLUSH
%47 = call i32 @tcflush(i32 %9, i32 %46)
br label %48
48: ; preds = %48, %14
%49 = phi i64 [ 0, %14 ], [ %54, %48 ]
%50 = getelementptr inbounds i8, i8* %5, i64 %49
%51 = call i32 @read(i32 %9, i8* %50, i32 1)
%52 = load i8, i8* %50
%53 = icmp eq i8 %52, 0
%54 = add nuw nsw i64 %49, 1
%55 = icmp eq i64 %54, 64
%56 = select i1 %53, i1 true, i1 %55
br i1 %56, label %57, label %48
57: ; preds = %48
%58 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%59 = load i32, i32* @TCSANOW
%60 = call i32 @tcsetattr(i32 %9, i32 %59, %struct.termios* %1)
ret i32 0
}