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

/* Variables and functions */
int EOF;
int O_NOCTTY;
int O_WRONLY;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCSTI;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ close(int);
int getchar();
scalar_t__ geteuid();
int ioctl(int, int /*<<< orphan*/, int *);
scalar_t__ isatty(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(struct termios *, struct termios *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Usage: %s tty\n", argv[0]);
    return 2;
  }
  int fd = open(argv[1], O_WRONLY | O_NOCTTY);
  if (fd == -1) {
    perror(argv[1]);
    return 1;
  }
  if (geteuid() != 0) {
    printf("Warning: you are not running %s as root. This may be necessary for "
           "proper functioning.\n",
           argv[0]);
  }
  int rawmode = 0;
  struct termios tios_saved;
  if (isatty(0)) {
    rawmode = 1;
    puts("Terminal now in raw mode; input will be sent as typed without "
         "pressing ENTER.");
    puts("Press C-a C-d to exit, or C-a C-a to send C-a.");
    struct termios tios;
    tcgetattr(0, &tios);
    memcpy(&tios_saved, &tios, sizeof(struct termios));
    cfmakeraw(&tios);
    tcsetattr(0, TCSANOW, &tios);
  }
  int error = 0;
  int ctrla = 0;
  int c;
  while ((c = getchar()) != EOF) {
    if (!ctrla && c == 1) {
      ctrla = 1;
    } else if (ctrla && c == 4) {
      break;
    } else {
      ctrla = 0;
      c &= 0xFF;
      if (ioctl(fd, TIOCSTI, &c) == -1) {
        putchar('\n');
        perror(argv[1]);
        error = 1;
        break;
      }
    }
  }
  if (rawmode) {
    tcsetattr(0, TCSANOW, &tios_saved);
  }
  close(fd);
  return error;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     121f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf2(%rip),%rdi
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13e7 <main+0x207>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    0x2d91(%rip),%rax
or     (%rax),%esi
call   10f0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1264 <main+0x84>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e7 <main+0x207>
mov    $0x0,%al
call   1070 <geteuid@plt>
cmp    $0x0,%rax
je     1288 <main+0xa8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd8c(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x18(%rbp)
xor    %edi,%edi
call   1050 <isatty@plt>
cmp    $0x0,%rax
je     12f9 <main+0x119>
movl   $0x1,-0x18(%rbp)
lea    0xdb9(%rip),%rdi
call   1040 <puts@plt>
lea    0xdfb(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
lea    -0x28(%rbp),%rsi
call   10d0 <tcgetattr@plt>
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <memcpy@plt>
lea    -0x28(%rbp),%rdi
call   10c0 <cfmakeraw@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   10e0 <tcsetattr@plt>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   10a0 <getchar@plt>
mov    %eax,-0x34(%rbp)
mov    0x2cc0(%rip),%rcx
cmp    (%rcx),%eax
je     13bb <main+0x1db>
cmpl   $0x0,-0x30(%rbp)
jne    1340 <main+0x160>
cmpl   $0x1,-0x34(%rbp)
jne    1340 <main+0x160>
movl   $0x1,-0x30(%rbp)
jmp    13b6 <main+0x1d6>
cmpl   $0x0,-0x30(%rbp)
je     1359 <main+0x179>
cmpl   $0x4,-0x34(%rbp)
jne    1359 <main+0x179>
jmp    13bb <main+0x1db>
movl   $0x0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2c73(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdx
call   1080 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    13ac <main+0x1cc>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x2c(%rbp)
jmp    13bb <main+0x1db>
jmp    13b1 <main+0x1d1>
jmp    13b6 <main+0x1d6>
jmp    1307 <main+0x127>
cmpl   $0x0,-0x18(%rbp)
je     13d9 <main+0x1f9>
mov    0x2c14(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x20(%rbp),%rdx
call   10e0 <tcsetattr@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1344 <main+0x164>
mov    0x8(%rbx),%rdi
mov    0x2db3(%rip),%rax
mov    0x2dbc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   10f0 <open@plt>
cmp    $0xffffffff,%eax
je     1369 <main+0x189>
mov    %eax,%r13d
xor    %eax,%eax
call   1070 <geteuid@plt>
test   %rax,%rax
je     123c <main+0x5c>
mov    (%rbx),%rsi
lea    0xdd8(%rip),%rdi
call   1060 <printf@plt>
mov    %rbx,0x10(%rsp)
xor    %ebp,%ebp
xor    %edi,%edi
call   1050 <isatty@plt>
mov    %rax,0x18(%rsp)
test   %rax,%rax
je     12a8 <main+0xc8>
lea    0xe0c(%rip),%rdi
call   1040 <puts@plt>
lea    0xe4e(%rip),%rdi
call   1040 <puts@plt>
lea    0x20(%rsp),%r12
xor    %edi,%edi
mov    %r12,%rsi
call   10d0 <tcgetattr@plt>
lea    0x8(%rsp),%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   10b0 <memcpy@plt>
mov    %r12,%rdi
call   10c0 <cfmakeraw@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    %r12,%rdx
call   10e0 <tcsetattr@plt>
xor    %eax,%eax
call   10a0 <getchar@plt>
mov    %eax,0x4(%rsp)
mov    0x2d1e(%rip),%rbx
cmp    (%rbx),%eax
je     131d <main+0x13d>
xor    %ecx,%ecx
mov    0x2d21(%rip),%r15
lea    0x4(%rsp),%r12
xor    %ebp,%ebp
jmp    1303 <main+0x123>
movzbl %al,%eax
mov    %eax,0x4(%rsp)
mov    (%r15),%esi
mov    %r13d,%edi
mov    %r12,%rdx
call   1080 <ioctl@plt>
xor    %r14d,%r14d
cmp    $0xffffffff,%eax
je     1379 <main+0x199>
xor    %eax,%eax
call   10a0 <getchar@plt>
mov    %eax,0x4(%rsp)
mov    %r14d,%ecx
cmp    (%rbx),%eax
je     131d <main+0x13d>
test   %ecx,%ecx
jne    1312 <main+0x132>
mov    $0x1,%r14d
cmp    $0x1,%eax
je     12f1 <main+0x111>
test   %ecx,%ecx
je     12d0 <main+0xf0>
cmp    $0x4,%eax
jne    12d0 <main+0xf0>
xor    %ebp,%ebp
cmpq   $0x0,0x18(%rsp)
je     133a <main+0x15a>
mov    0x2cb4(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   10e0 <tcsetattr@plt>
mov    %r13d,%edi
call   1090 <close@plt>
jmp    1358 <main+0x178>
mov    (%rbx),%rsi
lea    0xcb2(%rip),%rdi
call   1060 <printf@plt>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rdi
call   1100 <perror@plt>
mov    $0x1,%ebp
jmp    1358 <main+0x178>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%rdi
call   1100 <perror@plt>
mov    $0x1,%ebp
cmpq   $0x0,0x18(%rsp)
jne    1325 <main+0x145>
jmp    133a <main+0x15a>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [15 x i8] c"Usage: %s tty\0A\00"
@O_WRONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [88 x i8] c"Warning: you are not running %s as root. This may be necessary for proper functioning.\0A\00"
@.str.2 = [78 x i8] c"Terminal now in raw mode; input will be sent as typed without pressing ENTER.\00"
@.str.3 = [47 x i8] c"Press C-a C-d to exit, or C-a C-a to send C-a.\00"
@TCSANOW = global i32 0
@EOF = global i32 0
@TIOCSTI = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
%9 = alloca %struct.termios
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 2, i32* %3
br label %99
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_NOCTTY
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %23, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %20
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @perror(i8* %33)
store i32 1, i32* %3
br label %99
35: ; preds = %20
%36 = call i64 (...) @geteuid()
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = load i8*, i8** %40
%42 = call i32 @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %38, %35
store i32 0, i32* %7
%44 = call i64 @isatty(i32 0)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %54
46: ; preds = %43
store i32 1, i32* %7
%47 = call i32 @puts(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @puts(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @tcgetattr(i32 0, %struct.termios* %9)
%50 = call i32 @memcpy(%struct.termios* %8, %struct.termios* %9, i32 4)
%51 = call i32 @cfmakeraw(%struct.termios* %9)
%52 = load i32, i32* @TCSANOW
%53 = call i32 @tcsetattr(i32 0, i32 %52, %struct.termios* %9)
br label %54
54: ; preds = %46, %43
store i32 0, i32* %10
store i32 0, i32* %11
br label %55
55: ; preds = %88, %54
%56 = call i32 (...) @getchar()
store i32 %56, i32* %12
%57 = load i32, i32* @EOF
%58 = icmp ne i32 %56, %57
br i1 %58, label %59, label %89
59: ; preds = %55
%60 = load i32, i32* %11
%61 = icmp ne i32 %60, 0
br i1 %61, label %66, label %62
62: ; preds = %59
%63 = load i32, i32* %12
%64 = icmp eq i32 %63, 1
br i1 %64, label %65, label %66
65: ; preds = %62
store i32 1, i32* %11
br label %88
66: ; preds = %62, %59
%67 = load i32, i32* %11
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %66
%70 = load i32, i32* %12
%71 = icmp eq i32 %70, 4
br i1 %71, label %72, label %73
72: ; preds = %69
br label %89
73: ; preds = %69, %66
store i32 0, i32* %11
%74 = load i32, i32* %12
%75 = and i32 %74, 255
store i32 %75, i32* %12
%76 = load i32, i32* %6
%77 = load i32, i32* @TIOCSTI
%78 = call i32 @ioctl(i32 %76, i32 %77, i32* %12)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %86
80: ; preds = %73
%81 = call i32 @putchar(i8 signext 10)
%82 = load i8**, i8*** %5
%83 = getelementptr inbounds i8*, i8** %82, i64 1
%84 = load i8*, i8** %83
%85 = call i32 @perror(i8* %84)
store i32 1, i32* %10
br label %89
86: ; preds = %73
br label %87
87: ; preds = %86
br label %88
88: ; preds = %87, %65
br label %55
89: ; preds = %80, %72, %55
%90 = load i32, i32* %7
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i32, i32* @TCSANOW
%94 = call i32 @tcsetattr(i32 0, i32 %93, %struct.termios* %8)
br label %95
95: ; preds = %92, %89
%96 = load i32, i32* %6
%97 = call i32 @close(i32 %96)
%98 = load i32, i32* %10
store i32 %98, i32* %3
br label %99
99: ; preds = %95, %30, %15
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [15 x i8] c"Usage: %s tty\0A\00"
@O_WRONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [88 x i8] c"Warning: you are not running %s as root. This may be necessary for proper functioning.\0A\00"
@.str.2 = [78 x i8] c"Terminal now in raw mode; input will be sent as typed without pressing ENTER.\00"
@.str.3 = [47 x i8] c"Press C-a C-d to exit, or C-a C-a to send C-a.\00"
@TCSANOW = global i32 0
@EOF = global i32 0
@TIOCSTI = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i32
%6 = icmp eq i32 %0, 2
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %76
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* %12, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = load i8*, i8** %11
%20 = tail call i32 @perror(i8* %19)
br label %76
21: ; preds = %10
%22 = tail call i64 (...) @geteuid()
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i8*, i8** %1
%26 = tail call i32 @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %24, %21
%28 = bitcast %struct.termios* %3 to i8*
%29 = tail call i64 @isatty(i32 0)
%30 = icmp eq i64 %29, 0
br i1 %30, label %40, label %31
31: ; preds = %27
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([78 x i8], [78 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
%34 = bitcast %struct.termios* %4 to i8*
%35 = call i32 @tcgetattr(i32 0, %struct.termios* %4)
%36 = call i32 @memcpy(%struct.termios* %3, %struct.termios* %4, i32 4)
%37 = call i32 @cfmakeraw(%struct.termios* %4)
%38 = load i32, i32* @TCSANOW
%39 = call i32 @tcsetattr(i32 0, i32 %38, %struct.termios* %4)
br label %40
40: ; preds = %31, %27
%41 = bitcast i32* %5 to i8*
%42 = call i32 (...) @getchar()
store i32 %42, i32* %5
%43 = load i32, i32* @EOF
%44 = icmp eq i32 %42, %43
br i1 %44, label %69, label %45
45: ; preds = %40, %64
%46 = phi i32 [ %66, %64 ], [ %42, %40 ]
%47 = phi i32 [ %65, %64 ], [ 0, %40 ]
%48 = icmp eq i32 %47, 0
%49 = icmp eq i32 %46, 1
%50 = and i1 %48, %49
br i1 %50, label %64, label %51
51: ; preds = %45
%52 = icmp ne i32 %47, 0
%53 = icmp eq i32 %46, 4
%54 = and i1 %52, %53
br i1 %54, label %69, label %55
55: ; preds = %51
%56 = and i32 %46, 255
store i32 %56, i32* %5
%57 = load i32, i32* @TIOCSTI
%58 = call i32 @ioctl(i32 %16, i32 %57, i32* %5)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %64
60: ; preds = %55
%61 = call i32 @putchar(i8 signext 10)
%62 = load i8*, i8** %11
%63 = call i32 @perror(i8* %62)
br label %69
64: ; preds = %45, %55
%65 = phi i32 [ 0, %55 ], [ 1, %45 ]
%66 = call i32 (...) @getchar()
store i32 %66, i32* %5
%67 = load i32, i32* @EOF
%68 = icmp eq i32 %66, %67
br i1 %68, label %69, label %45
69: ; preds = %64, %51, %40, %60
%70 = phi i32 [ 1, %60 ], [ 0, %40 ], [ 0, %51 ], [ 0, %64 ]
br i1 %30, label %74, label %71
71: ; preds = %69
%72 = load i32, i32* @TCSANOW
%73 = call i32 @tcsetattr(i32 0, i32 %72, %struct.termios* %3)
br label %74
74: ; preds = %71, %69
%75 = call i32 @close(i32 %16)
br label %76
76: ; preds = %18, %74, %7
%77 = phi i32 [ 2, %7 ], [ 1, %18 ], [ %70, %74 ]
ret i32 %77
}