#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;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int ECHO;
int ECHONL;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ forkpty(int *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int master;
  pid_t pid;
  pid = forkpty(&master, NULL, NULL, NULL);
  if (pid < 0) {
    return 1;
  } else if (pid == 0) {
    char *args[] = {NULL};
    execvp("/usr/bin/bc", args);
  } else {
    struct termios tios;
    tcgetattr(master, &tios);
    tios.c_lflag &= ~(ECHO | ECHONL);
    tcsetattr(master, TCSAFLUSH, &tios);
    for (;;) {
      fd_set read_fd;
      fd_set write_fd;
      fd_set except_fd;
      FD_ZERO(&read_fd);
      FD_ZERO(&write_fd);
      FD_ZERO(&except_fd);
      FD_SET(master, &read_fd);
      FD_SET(STDIN_FILENO, &read_fd);
      select(master + 1, &read_fd, &write_fd, &except_fd, NULL);
      char input;
      char output;
      if (FD_ISSET(master, &read_fd)) {
        if (read(master, &output, 1) != -1)
          write(STDOUT_FILENO, &output, 1);
        else
          break;
      }
      if (FD_ISSET(STDIN_FILENO, &read_fd)) {
        read(STDIN_FILENO, &input, 1);
        write(master, &input, 1);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   10a0 <forkpty@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11ed <main+0x3d>
movl   $0x1,-0x4(%rbp)
jmp    135e <main+0x1ae>
cmpq   $0x0,-0x10(%rbp)
jne    121d <main+0x6d>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    -0x18(%rbp),%rsi
lea    0xded(%rip),%rdi
call   10c0 <execvp@plt>
jmp    1352 <main+0x1a2>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <tcgetattr@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    0x2d8f(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   10b0 <tcsetattr@plt>
lea    -0x24(%rbp),%rdi
call   1070 <FD_ZERO@plt>
lea    -0x28(%rbp),%rdi
call   1070 <FD_ZERO@plt>
lea    -0x2c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <select@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1304 <main+0x154>
mov    -0x8(%rbp),%edi
lea    -0x2e(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     12fa <main+0x14a>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    -0x2e(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12ff <main+0x14f>
jmp    134d <main+0x19d>
jmp    1304 <main+0x154>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1348 <main+0x198>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    -0x8(%rbp),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1259 <main+0xa9>
jmp    1352 <main+0x1a2>
jmp    1357 <main+0x1a7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <forkpty@plt>
test   %rax,%rax
js     12ee <main+0x14e>
je     12f5 <main+0x155>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1080 <tcgetattr@plt>
mov    0x2df3(%rip),%rax
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x10(%rsp)
mov    0x8(%rsp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
call   10a0 <tcsetattr@plt>
lea    0x24(%rsp),%rbx
lea    0x20(%rsp),%r14
lea    0x1c(%rsp),%r15
mov    0x2d9f(%rip),%rbp
lea    0xf(%rsp),%r12
lea    0xe(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    0x8(%rsp),%edi
add    $0x1,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
xor    %r8d,%r8d
call   1070 <select@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12b4 <main+0x114>
mov    0x8(%rsp),%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     130f <main+0x16f>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1230 <main+0x90>
mov    0x0(%rbp),%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    0x8(%rsp),%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1230 <main+0x90>
mov    $0x1,%eax
jmp    1311 <main+0x171>
movq   $0x0,0x10(%rsp)
lea    0xcfb(%rip),%rdi
lea    0x10(%rsp),%rsi
call   10b0 <execvp@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [12 x i8] c"/usr/bin/bc\00"
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSAFLUSH = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca [1 x i8*]
%5 = alloca %struct.termios
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i8
store i32 0, i32* %1
%11 = call i64 @forkpty(i32* %2, i32* null, i32* null, i32* null)
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %0
store i32 1, i32* %1
br label %71
15: ; preds = %0
%16 = load i64, i64* %3
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = bitcast [1 x i8*]* %4 to i8*
%20 = getelementptr inbounds [1 x i8*], [1 x i8*]* %4, i64 0, i64 0
%21 = call i32 @execvp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %20)
br label %69
22: ; preds = %15
%23 = load i32, i32* %2
%24 = call i32 @tcgetattr(i32 %23, %struct.termios* %5)
%25 = load i32, i32* @ECHO
%26 = load i32, i32* @ECHONL
%27 = or i32 %25, %26
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%30 = load i32, i32* %29
%31 = and i32 %30, %28
store i32 %31, i32* %29
%32 = load i32, i32* %2
%33 = load i32, i32* @TCSAFLUSH
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %5)
br label %35
35: ; preds = %67, %22
%36 = call i32 @FD_ZERO(i32* %6)
%37 = call i32 @FD_ZERO(i32* %7)
%38 = call i32 @FD_ZERO(i32* %8)
%39 = load i32, i32* %2
%40 = call i32 @FD_SET(i32 %39, i32* %6)
%41 = load i32, i32* @STDIN_FILENO
%42 = call i32 @FD_SET(i32 %41, i32* %6)
%43 = load i32, i32* %2
%44 = add nsw i32 %43, 1
%45 = call i32 @select(i32 %44, i32* %6, i32* %7, i32* %8, i32* null)
%46 = load i32, i32* %2
%47 = call i64 @FD_ISSET(i32 %46, i32* %6)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %58
49: ; preds = %35
%50 = load i32, i32* %2
%51 = call i32 @read(i32 %50, i8* %10, i32 1)
%52 = icmp ne i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* @STDOUT_FILENO
%55 = call i32 @write(i32 %54, i8* %10, i32 1)
br label %57
56: ; preds = %49
br label %68
57: ; preds = %53
br label %58
58: ; preds = %57, %35
%59 = load i32, i32* @STDIN_FILENO
%60 = call i64 @FD_ISSET(i32 %59, i32* %6)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %58
%63 = load i32, i32* @STDIN_FILENO
%64 = call i32 @read(i32 %63, i8* %9, i32 1)
%65 = load i32, i32* %2
%66 = call i32 @write(i32 %65, i8* %9, i32 1)
br label %67
67: ; preds = %62, %58
br label %35
68: ; preds = %56
br label %69
69: ; preds = %68, %18
br label %70
70: ; preds = %69
store i32 0, i32* %1
br label %71
71: ; preds = %70, %14
%72 = load i32, i32* %1
ret i32 %72
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [12 x i8] c"/usr/bin/bc\00"
@ECHO = global i32 0
@ECHONL = global i32 0
@TCSAFLUSH = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [1 x i8*]
%3 = alloca %struct.termios
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = bitcast i32* %1 to i8*
%10 = call i64 @forkpty(i32* %1, i32* null, i32* null, i32* null)
%11 = icmp slt i64 %10, 0
br i1 %11, label %68, label %12
12: ; preds = %0
%13 = icmp eq i64 %10, 0
br i1 %13, label %14, label %19
14: ; preds = %12
%15 = bitcast [1 x i8*]* %2 to i8*
%16 = bitcast [1 x i8*]* %2 to i64*
store i64 0, i64* %16
%17 = getelementptr inbounds [1 x i8*], [1 x i8*]* %2, i64 0, i64 0
%18 = call i32 @execvp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %17)
br label %68
19: ; preds = %12
%20 = bitcast %struct.termios* %3 to i8*
%21 = load i32, i32* %1
%22 = call i32 @tcgetattr(i32 %21, %struct.termios* %3)
%23 = load i32, i32* @ECHO
%24 = load i32, i32* @ECHONL
%25 = or i32 %24, %23
%26 = xor i32 %25, -1
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = and i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* %1
%31 = load i32, i32* @TCSAFLUSH
%32 = call i32 @tcsetattr(i32 %30, i32 %31, %struct.termios* %3)
%33 = bitcast i32* %4 to i8*
%34 = bitcast i32* %5 to i8*
%35 = bitcast i32* %6 to i8*
br label %36
36: ; preds = %66, %19
%37 = call i32 @FD_ZERO(i32* %4)
%38 = call i32 @FD_ZERO(i32* %5)
%39 = call i32 @FD_ZERO(i32* %6)
%40 = load i32, i32* %1
%41 = call i32 @FD_SET(i32 %40, i32* %4)
%42 = load i32, i32* @STDIN_FILENO
%43 = call i32 @FD_SET(i32 %42, i32* %4)
%44 = load i32, i32* %1
%45 = add nsw i32 %44, 1
%46 = call i32 @select(i32 %45, i32* %4, i32* %5, i32* %6, i32* null)
%47 = load i32, i32* %1
%48 = call i64 @FD_ISSET(i32 %47, i32* %4)
%49 = icmp eq i64 %48, 0
br i1 %49, label %57, label %50
50: ; preds = %36
%51 = load i32, i32* %1
%52 = call i32 @read(i32 %51, i8* %8, i32 1)
%53 = icmp eq i32 %52, -1
br i1 %53, label %67, label %54
54: ; preds = %50
%55 = load i32, i32* @STDOUT_FILENO
%56 = call i32 @write(i32 %55, i8* %8, i32 1)
br label %57
57: ; preds = %54, %36
%58 = load i32, i32* @STDIN_FILENO
%59 = call i64 @FD_ISSET(i32 %58, i32* %4)
%60 = icmp eq i64 %59, 0
br i1 %60, label %66, label %61
61: ; preds = %57
%62 = load i32, i32* @STDIN_FILENO
%63 = call i32 @read(i32 %62, i8* %7, i32 1)
%64 = load i32, i32* %1
%65 = call i32 @write(i32 %64, i8* %7, i32 1)
br label %66
66: ; preds = %57, %61
br label %36
67: ; preds = %50
br label %68
68: ; preds = %67, %14, %0
%69 = phi i32 [ 1, %0 ], [ 0, %14 ], [ 0, %67 ]
ret i32 %69
}