#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*/ tio;
struct termios {
  int c_cflag;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  scalar_t__ c_iflag;
};
typedef int /*<<< orphan*/ stdio;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ B57600;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int O_RDWR;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, unsigned char *, int);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ write(int, unsigned char *, int);
int main(int argc, char **argv) {
  struct termios tio;
  struct termios stdio;
  int tty_fd;
  fd_set rdset;
  unsigned char c = 'D';
  printf("Please start with %s /dev/ttyS1 (for example)\n", argv[0]);
  memset(&stdio, 0, sizeof(stdio));
  stdio.c_iflag = 0;
  stdio.c_oflag = 0;
  stdio.c_cflag = 0;
  stdio.c_lflag = 0;
  stdio.c_cc[VMIN] = 1;
  stdio.c_cc[VTIME] = 0;
  tcsetattr(STDOUT_FILENO, TCSANOW, &stdio);
  tcsetattr(STDOUT_FILENO, TCSAFLUSH, &stdio);
  fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
  memset(&tio, 0, sizeof(tio));
  tio.c_iflag = 0;
  tio.c_oflag = 0;
  tio.c_cflag = CS8 | CREAD | CLOCAL;
  tio.c_lflag = 0;
  tio.c_cc[VMIN] = 1;
  tio.c_cc[VTIME] = 5;
  tty_fd = open(argv[1], O_RDWR | O_NONBLOCK);
  cfsetospeed(&tio, B57600);
  cfsetispeed(&tio, B57600);
  tcsetattr(tty_fd, TCSANOW, &tio);
  while (c != 'q') {
    if (read(tty_fd, &c, 1) > 0)
      write(STDOUT_FILENO, &c, 1);
    if (read(STDIN_FILENO, &c, 1) > 0)
      write(tty_fd, &c, 1);
  }
  close(tty_fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x44,-0x69(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe38(%rip),%rdi
call   1060 <printf@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movl   $0x0,-0x60(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x58(%rbp),%rax
mov    0x2da9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x58(%rbp),%rax
mov    0x2d74(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   10a0 <tcsetattr@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   10a0 <tcsetattr@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2d15(%rip),%rax
mov    (%rax),%eax
mov    0x2d14(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2ce3(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2cae(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
mov    0x2ccc(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x64(%rbp)
mov    0x2c93(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1050 <cfsetospeed@plt>
mov    0x2c81(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   10c0 <cfsetispeed@plt>
mov    -0x64(%rbp),%edi
mov    0x2c8c(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   10a0 <tcsetattr@plt>
movzbl -0x69(%rbp),%eax
cmp    $0x71,%eax
je     13bd <main+0x21d>
mov    -0x64(%rbp),%edi
lea    -0x69(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jle    1386 <main+0x1e6>
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
lea    -0x69(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x2bfb(%rip),%rax
mov    (%rax),%edi
lea    -0x69(%rbp),%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jle    13b8 <main+0x218>
mov    -0x64(%rbp),%edi
lea    -0x69(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1347 <main+0x1a7>
mov    -0x64(%rbp),%edi
call   1080 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsi,%rbx
movb   $0x44,0x7(%rsp)
mov    (%rsi),%rsi
lea    0xe50(%rip),%rdi
call   1060 <printf@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
movl   $0x0,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x10(%rsp),%rax
mov    0x2db2(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d81(%rip),%r13
mov    0x0(%r13),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dcf(%rip),%r15
mov    (%r15),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdx
call   10a0 <tcsetattr@plt>
mov    (%r15),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdx
call   10a0 <tcsetattr@plt>
mov    0x2d38(%rip),%r12
mov    (%r12),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
mov    0x2d6c(%rip),%rbp
mov    0x0(%rbp),%edx
call   1030 <fcntl@plt>
lea    0x30(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
mov    0x2d31(%rip),%rax
mov    0x2d32(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xorps  %xmm0,%xmm0
movups %xmm0,0x48(%rsp)
mov    0x2cd7(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x30(%rsp)
movq   $0x0,0x40(%rsp)
mov    0x38(%rsp),%rax
mov    0x2cf4(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x0(%r13),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x8(%rbx),%rdi
mov    0x2d04(%rip),%rax
mov    0x0(%rbp),%esi
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,%ebp
mov    0x2cb9(%rip),%rbx
mov    (%rbx),%esi
mov    %r14,%rdi
call   1050 <cfsetospeed@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   10c0 <cfsetispeed@plt>
mov    0x2cbe(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   10a0 <tcsetattr@plt>
lea    0x7(%rsp),%rbx
jmp    1327 <main+0x187>
nopl   (%rax)
cmpb   $0x71,0x7(%rsp)
je     1372 <main+0x1d2>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    134b <main+0x1ab>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    1320 <main+0x180>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1320 <main+0x180>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i64, i64 }
@.str = [47 x i8] c"Please start with %s /dev/ttyS1 (for example)\0A\00"
@VMIN = global i64 0
@VTIME = global i64 0
@STDOUT_FILENO = global i32 0
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@STDIN_FILENO = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@O_RDWR = global i32 0
@B57600 = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.termios
%7 = alloca %struct.termios
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 68, i8* %10
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @memset(%struct.termios* %7, i32 0, i32 40)
%16 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 4
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
store i32 0, i32* %18
%19 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%21 = load i32*, i32** %20
%22 = load i64, i64* @VMIN
%23 = getelementptr inbounds i32, i32* %21, i64 %22
store i32 1, i32* %23
%24 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%25 = load i32*, i32** %24
%26 = load i64, i64* @VTIME
%27 = getelementptr inbounds i32, i32* %25, i64 %26
store i32 0, i32* %27
%28 = load i32, i32* @STDOUT_FILENO
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %28, i32 %29, %struct.termios* %7)
%31 = load i32, i32* @STDOUT_FILENO
%32 = load i32, i32* @TCSAFLUSH
%33 = call i32 @tcsetattr(i32 %31, i32 %32, %struct.termios* %7)
%34 = load i32, i32* @STDIN_FILENO
%35 = load i32, i32* @F_SETFL
%36 = load i32, i32* @O_NONBLOCK
%37 = call i32 @fcntl(i32 %34, i32 %35, i32 %36)
%38 = call i32 @memset(%struct.termios* %6, i32 0, i32 40)
%39 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
store i64 0, i64* %39
%40 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i64 0, i64* %40
%41 = load i32, i32* @CS8
%42 = load i32, i32* @CREAD
%43 = or i32 %41, %42
%44 = load i32, i32* @CLOCAL
%45 = or i32 %43, %44
%46 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i64 0, i64* %47
%48 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%49 = load i32*, i32** %48
%50 = load i64, i64* @VMIN
%51 = getelementptr inbounds i32, i32* %49, i64 %50
store i32 1, i32* %51
%52 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%53 = load i32*, i32** %52
%54 = load i64, i64* @VTIME
%55 = getelementptr inbounds i32, i32* %53, i64 %54
store i32 5, i32* %55
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 1
%58 = load i8*, i8** %57
%59 = load i32, i32* @O_RDWR
%60 = load i32, i32* @O_NONBLOCK
%61 = or i32 %59, %60
%62 = call i32 @open(i8* %58, i32 %61)
store i32 %62, i32* %8
%63 = load i32, i32* @B57600
%64 = call i32 @cfsetospeed(%struct.termios* %6, i32 %63)
%65 = load i32, i32* @B57600
%66 = call i32 @cfsetispeed(%struct.termios* %6, i32 %65)
%67 = load i32, i32* %8
%68 = load i32, i32* @TCSANOW
%69 = call i32 @tcsetattr(i32 %67, i32 %68, %struct.termios* %6)
br label %70
70: ; preds = %88, %2
%71 = load i8, i8* %10
%72 = zext i8 %71 to i32
%73 = icmp ne i32 %72, 113
br i1 %73, label %74, label %89
74: ; preds = %70
%75 = load i32, i32* %8
%76 = call i64 @read(i32 %75, i8* %10, i32 1)
%77 = icmp sgt i64 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = load i32, i32* @STDOUT_FILENO
%80 = call i32 @write(i32 %79, i8* %10, i32 1)
br label %81
81: ; preds = %78, %74
%82 = load i32, i32* @STDIN_FILENO
%83 = call i64 @read(i32 %82, i8* %10, i32 1)
%84 = icmp sgt i64 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %81
%86 = load i32, i32* %8
%87 = call i32 @write(i32 %86, i8* %10, i32 1)
br label %88
88: ; preds = %85, %81
br label %70
89: ; preds = %70
%90 = load i32, i32* %8
%91 = call i32 @close(i32 %90)
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i64, i64 }
@.str = [47 x i8] c"Please start with %s /dev/ttyS1 (for example)\0A\00"
@VMIN = global i64 0
@VTIME = global i64 0
@STDOUT_FILENO = global i32 0
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@STDIN_FILENO = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@O_RDWR = global i32 0
@B57600 = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i8
%6 = bitcast %struct.termios* %3 to i8*
%7 = bitcast %struct.termios* %4 to i8*
store i8 68, i8* %5
%8 = load i8*, i8** %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = call i32 @memset(%struct.termios* %4, i32 0, i32 40)
%11 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 2
%13 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%14 = bitcast i64* %12 to i8*
%15 = load i32*, i32** %13
%16 = load i64, i64* @VMIN
%17 = getelementptr inbounds i32, i32* %15, i64 %16
store i32 1, i32* %17
%18 = load i64, i64* @VTIME
%19 = getelementptr inbounds i32, i32* %15, i64 %18
store i32 0, i32* %19
%20 = load i32, i32* @STDOUT_FILENO
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 %20, i32 %21, %struct.termios* %4)
%23 = load i32, i32* @STDOUT_FILENO
%24 = load i32, i32* @TCSAFLUSH
%25 = call i32 @tcsetattr(i32 %23, i32 %24, %struct.termios* %4)
%26 = load i32, i32* @STDIN_FILENO
%27 = load i32, i32* @F_SETFL
%28 = load i32, i32* @O_NONBLOCK
%29 = call i32 @fcntl(i32 %26, i32 %27, i32 %28)
%30 = call i32 @memset(%struct.termios* %3, i32 0, i32 40)
%31 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%32 = bitcast i64* %31 to i8*
%33 = load i32, i32* @CS8
%34 = load i32, i32* @CREAD
%35 = or i32 %34, %33
%36 = load i32, i32* @CLOCAL
%37 = or i32 %35, %36
%38 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
store i64 0, i64* %39
%40 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%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 i64, i64* @VTIME
%45 = getelementptr inbounds i32, i32* %41, i64 %44
store i32 5, i32* %45
%46 = getelementptr inbounds i8*, i8** %1, i64 1
%47 = load i8*, i8** %46
%48 = load i32, i32* @O_RDWR
%49 = load i32, i32* @O_NONBLOCK
%50 = or i32 %49, %48
%51 = call i32 @open(i8* %47, i32 %50)
%52 = load i32, i32* @B57600
%53 = call i32 @cfsetospeed(%struct.termios* %3, i32 %52)
%54 = load i32, i32* @B57600
%55 = call i32 @cfsetispeed(%struct.termios* %3, i32 %54)
%56 = load i32, i32* @TCSANOW
%57 = call i32 @tcsetattr(i32 %51, i32 %56, %struct.termios* %3)
br label %58
58: ; preds = %2, %70
%59 = call i64 @read(i32 %51, i8* %5, i32 1)
%60 = icmp sgt i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i32, i32* @STDOUT_FILENO
%63 = call i32 @write(i32 %62, i8* %5, i32 1)
br label %64
64: ; preds = %61, %58
%65 = load i32, i32* @STDIN_FILENO
%66 = call i64 @read(i32 %65, i8* %5, i32 1)
%67 = icmp sgt i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 @write(i32 %51, i8* %5, i32 1)
br label %70
70: ; preds = %68, %64
%71 = load i8, i8* %5
%72 = icmp eq i8 %71, 113
br i1 %72, label %73, label %58
73: ; preds = %70
%74 = call i32 @close(i32 %51)
ret i32 0
}