#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCIOFF;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCION;
int /*<<< orphan*/ TCOOFF;
int /*<<< orphan*/ TCOON;
int /*<<< orphan*/ TIOCSTI;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcmp(char *, char *);
scalar_t__ tcflow(int, int /*<<< orphan*/);
scalar_t__ tcflush(int, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  if (argc < 3) {
    printf("test_termios <tty file> <cmd>\n");
    return -1;
  }
  fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  char *cmd = argv[2];
  if (!strcmp(cmd, "flush")) {
    if (tcflush(fd, TCIOFLUSH) < 0) {
      perror("tcflush");
    }
  } else if (!strcmp(cmd, "insert")) {
    if (ioctl(fd, TIOCSTI, "\r") < 0) {
      perror("ioctl");
    }
  } else if (!strcmp(cmd, "restart")) {
    if (tcflush(fd, TCOON) < 0) {
      perror("tcflush");
    }
  } else if (!strcmp(cmd, "suspend")) {
    if (tcflush(fd, TCOOFF) < 0) {
      perror("tcflush");
    }
  } else if (!strcmp(cmd, "stop")) {
    if (tcflow(fd, TCIOFF) < 0) {
      perror("tcflow");
    }
  } else if (!strcmp(cmd, "stop")) {
    if (tcflow(fd, TCION) < 0) {
      perror("tcflow");
    }
  } else {
    printf("Invalid cmd\n");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11b8 <main+0x38>
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d8 <main+0x258>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f3 <main+0x73>
lea    0xe3d(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d8 <main+0x258>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1244 <main+0xc4>
mov    -0x14(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
cmp    $0x0,%rax
jge    123f <main+0xbf>
lea    0xdea(%rip),%rdi
call   1090 <perror@plt>
jmp    13c9 <main+0x249>
mov    -0x20(%rbp),%rdi
lea    0xddd(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1290 <main+0x110>
mov    -0x14(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
lea    0xdc3(%rip),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    128b <main+0x10b>
lea    0xdaf(%rip),%rdi
call   1090 <perror@plt>
jmp    13c4 <main+0x244>
mov    -0x20(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12d5 <main+0x155>
mov    -0x14(%rbp),%edi
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
cmp    $0x0,%rax
jge    12d0 <main+0x150>
lea    0xd59(%rip),%rdi
call   1090 <perror@plt>
jmp    13bf <main+0x23f>
mov    -0x20(%rbp),%rdi
lea    0xd63(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    131a <main+0x19a>
mov    -0x14(%rbp),%edi
mov    0x2cc0(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
cmp    $0x0,%rax
jge    1315 <main+0x195>
lea    0xd14(%rip),%rdi
call   1090 <perror@plt>
jmp    13ba <main+0x23a>
mov    -0x20(%rbp),%rdi
lea    0xd26(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    135f <main+0x1df>
mov    -0x14(%rbp),%edi
mov    0x2c9b(%rip),%rax
mov    (%rax),%esi
call   10a0 <tcflow@plt>
cmp    $0x0,%rax
jge    135a <main+0x1da>
lea    0xcfb(%rip),%rdi
call   1090 <perror@plt>
jmp    13b5 <main+0x235>
mov    -0x20(%rbp),%rdi
lea    0xce1(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    13a4 <main+0x224>
mov    -0x14(%rbp),%edi
mov    0x2c3e(%rip),%rax
mov    (%rax),%esi
call   10a0 <tcflow@plt>
cmp    $0x0,%rax
jge    139f <main+0x21f>
lea    0xcb6(%rip),%rdi
call   1090 <perror@plt>
jmp    13b0 <main+0x230>
lea    0xcac(%rip),%rdi
call   1040 <printf@plt>
jmp    13b5 <main+0x235>
jmp    13ba <main+0x23a>
jmp    13bf <main+0x23f>
jmp    13c4 <main+0x244>
jmp    13c9 <main+0x249>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     119e <main+0x1e>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    128c <main+0x10c>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1293 <main+0x113>
mov    %eax,%ebp
mov    0x10(%rbx),%rbx
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1232 <main+0xb2>
lea    0xe32(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     123b <main+0xbb>
lea    0xe2e(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1265 <main+0xe5>
lea    0xe23(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     126e <main+0xee>
lea    0xe18(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     12a6 <main+0x126>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
jmp    1283 <main+0x103>
mov    0x2d7f(%rip),%rax
jmp    1275 <main+0xf5>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    0xdc9(%rip),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
jns    1283 <main+0x103>
lea    0xdb8(%rip),%rdi
call   1090 <perror@plt>
jmp    1283 <main+0x103>
mov    0x2d64(%rip),%rax
jmp    1275 <main+0xf5>
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
test   %rax,%rax
js     12c9 <main+0x149>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd66(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    128c <main+0x10c>
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10a0 <tcflow@plt>
test   %rax,%rax
jns    1283 <main+0x103>
lea    0xd6f(%rip),%rdi
call   1090 <perror@plt>
jmp    1283 <main+0x103>
lea    0xd35(%rip),%rdi
call   1090 <perror@plt>
jmp    1283 <main+0x103>

LLVM IR (-O0):
@.str = [31 x i8] c"test_termios <tty file> <cmd>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"flush\00"
@TCIOFLUSH = global i32 0
@.str.3 = [8 x i8] c"tcflush\00"
@.str.4 = [7 x i8] c"insert\00"
@TIOCSTI = global i32 0
@.str.5 = [2 x i8] c"\0D\00"
@.str.6 = [6 x i8] c"ioctl\00"
@.str.7 = [8 x i8] c"restart\00"
@TCOON = global i32 0
@.str.8 = [8 x i8] c"suspend\00"
@TCOOFF = global i32 0
@.str.9 = [5 x i8] c"stop\00"
@TCIOFF = global i32 0
@.str.10 = [7 x i8] c"tcflow\00"
@TCION = global i32 0
@.str.11 = [13 x i8] c"Invalid cmd\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 3
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %107
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %107
22: ; preds = %12
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = call i32 @strcmp(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp ne i32 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %22
%30 = load i32, i32* %6
%31 = load i32, i32* @TCIOFLUSH
%32 = call i64 @tcflush(i32 %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
br label %104
37: ; preds = %22
%38 = load i8*, i8** %7
%39 = call i32 @strcmp(i8* %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%40 = icmp ne i32 %39, 0
br i1 %40, label %49, label %41
41: ; preds = %37
%42 = load i32, i32* %6
%43 = load i32, i32* @TIOCSTI
%44 = call i64 @ioctl(i32 %42, i32 %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %46, %41
br label %103
49: ; preds = %37
%50 = load i8*, i8** %7
%51 = call i32 @strcmp(i8* %50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%52 = icmp ne i32 %51, 0
br i1 %52, label %61, label %53
53: ; preds = %49
%54 = load i32, i32* %6
%55 = load i32, i32* @TCOON
%56 = call i64 @tcflush(i32 %54, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %58, %53
br label %102
61: ; preds = %49
%62 = load i8*, i8** %7
%63 = call i32 @strcmp(i8* %62, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
%64 = icmp ne i32 %63, 0
br i1 %64, label %73, label %65
65: ; preds = %61
%66 = load i32, i32* %6
%67 = load i32, i32* @TCOOFF
%68 = call i64 @tcflush(i32 %66, i32 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %65
%71 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %72
72: ; preds = %70, %65
br label %101
73: ; preds = %61
%74 = load i8*, i8** %7
%75 = call i32 @strcmp(i8* %74, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%76 = icmp ne i32 %75, 0
br i1 %76, label %85, label %77
77: ; preds = %73
%78 = load i32, i32* %6
%79 = load i32, i32* @TCIOFF
%80 = call i64 @tcflow(i32 %78, i32 %79)
%81 = icmp slt i64 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %77
%83 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
br label %84
84: ; preds = %82, %77
br label %100
85: ; preds = %73
%86 = load i8*, i8** %7
%87 = call i32 @strcmp(i8* %86, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%88 = icmp ne i32 %87, 0
br i1 %88, label %97, label %89
89: ; preds = %85
%90 = load i32, i32* %6
%91 = load i32, i32* @TCION
%92 = call i64 @tcflow(i32 %90, i32 %91)
%93 = icmp slt i64 %92, 0
br i1 %93, label %94, label %96
94: ; preds = %89
%95 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
br label %96
96: ; preds = %94, %89
br label %99
97: ; preds = %85
%98 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0))
br label %99
99: ; preds = %97, %96
br label %100
100: ; preds = %99, %84
br label %101
101: ; preds = %100, %72
br label %102
102: ; preds = %101, %60
br label %103
103: ; preds = %102, %48
br label %104
104: ; preds = %103, %36
%105 = load i32, i32* %6
%106 = call i32 @close(i32 %105)
store i32 0, i32* %3
br label %107
107: ; preds = %104, %20, %10
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"flush\00"
@TCIOFLUSH = global i32 0
@.str.3 = [8 x i8] c"tcflush\00"
@.str.4 = [7 x i8] c"insert\00"
@TIOCSTI = global i32 0
@.str.5 = [2 x i8] c"\0D\00"
@.str.6 = [6 x i8] c"ioctl\00"
@.str.7 = [8 x i8] c"restart\00"
@TCOON = global i32 0
@.str.8 = [8 x i8] c"suspend\00"
@TCOOFF = global i32 0
@.str.9 = [5 x i8] c"stop\00"
@TCIOFF = global i32 0
@.str.10 = [7 x i8] c"tcflow\00"
@TCION = global i32 0
@str = [12 x i8] c"Invalid cmd\00"
@str.12 = [30 x i8] c"test_termios <tty file> <cmd>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.12, i64 0, i64 0))
br label %65
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %65
14: ; preds = %6
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32, i32* @TCIOFLUSH
%21 = tail call i64 @tcflush(i32 %10, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %63
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %63
25: ; preds = %14
%26 = tail call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32, i32* @TIOCSTI
%30 = tail call i64 @ioctl(i32 %10, i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %63
32: ; preds = %28
%33 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %63
34: ; preds = %25
%35 = tail call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load i32, i32* @TCOON
%39 = tail call i64 @tcflush(i32 %10, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %63
41: ; preds = %37
%42 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %63
43: ; preds = %34
%44 = tail call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %43
%47 = load i32, i32* @TCOOFF
%48 = tail call i64 @tcflush(i32 %10, i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %63
50: ; preds = %46
%51 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %63
52: ; preds = %43
%53 = tail call i32 @strcmp(i8* dereferenceable(1) %16, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = load i32, i32* @TCIOFF
%57 = tail call i64 @tcflow(i32 %10, i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %55
%60 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
br label %63
61: ; preds = %52
%62 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %63
63: ; preds = %32, %28, %50, %46, %61, %55, %59, %37, %41, %19, %23
%64 = tail call i32 @close(i32 %10)
br label %65
65: ; preds = %63, %12, %4
%66 = phi i32 [ -1, %4 ], [ -1, %12 ], [ 0, %63 ]
ret i32 %66
}