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

/* Variables and functions */
int O_NOCTTY;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TCSANOW;
int atoi(char *);
int /*<<< orphan*/ cfmakeraw(struct termios *);
scalar_t__ cfsetspeed(struct termios *, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
scalar_t__ read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strerror(int);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ usage();
int /*<<< orphan*/ usleep(int);
scalar_t__ writen(int /*<<< orphan*/, unsigned char *, scalar_t__);
int main(int argc, char **argv) {
  int fd;
  ssize_t readlen;
  ssize_t writelen;
  unsigned char buf[8192];
  struct termios tio;
  int baudrate = 300;
  if (argc < 2 || (0 == strcmp(argv[1], "-h"))) {
    usage();
    return -1;
  }
  if (argc > 2) {
    baudrate = atoi(argv[2]);
  }
  fd = open(argv[1], O_RDONLY | O_NOCTTY | O_NONBLOCK);
  if (fd < 0) {
    fprintf(stderr, "Failed to open %s. errno = %d (%s).\n", argv[1], errno,
            strerror(errno));
    return -1;
  }
  cfmakeraw(&tio);
  if (cfsetspeed(&tio, baudrate) < 0) {
    fprintf(stderr, "Failed to set baudrate to %d. errno = %d (%s).\n",
            baudrate, errno, strerror(errno));
    return -1;
  }
  if (tcsetattr(fd, TCSANOW, &tio) < 0) {
    fprintf(stderr, "tcsetattr failed. errno = %d (%s).\n", errno,
            strerror(errno));
    return -1;
  }
  for (;;) {
    readlen = read(fd, &buf[0], sizeof(buf));
    if (readlen > 0) {
      writelen = writen(STDOUT_FILENO, buf, readlen);
      if (writelen < 0) {
        fprintf(stderr, "Error writing to stdout. errno = %d (%s).\n", errno,
                strerror(errno));
      }
    }
    (void)usleep(100);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2070,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x12c,-0x203c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     120f <main+0x4f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe04(%rip),%rsi
call   1050 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1222 <main+0x62>
mov    $0x0,%al
call   1080 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1492 <main+0x2d2>
cmpl   $0x2,-0x8(%rbp)
jle    123f <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x203c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    0x2d69(%rip),%rax
or     (%rax),%esi
mov    0x2d80(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12df <main+0x11f>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x204c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x2048(%rbp)
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2040(%rbp)
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x204c(%rbp),%edi
mov    -0x2048(%rbp),%rdx
mov    -0x2040(%rbp),%ecx
mov    %rax,%r8
lea    0xd37(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1492 <main+0x2d2>
lea    -0x2038(%rbp),%rdi
call   1070 <cfmakeraw@plt>
mov    -0x203c(%rbp),%esi
lea    -0x2038(%rbp),%rdi
call   1030 <cfsetspeed@plt>
cmp    $0x0,%rax
jge    136e <main+0x1ae>
mov    0x2caa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2058(%rbp)
mov    -0x203c(%rbp),%eax
mov    %eax,-0x2054(%rbp)
mov    0x2c87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2050(%rbp)
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x2058(%rbp),%edi
mov    -0x2054(%rbp),%edx
mov    -0x2050(%rbp),%ecx
mov    %rax,%r8
lea    0xccd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1492 <main+0x2d2>
mov    -0x14(%rbp),%edi
mov    0x2c58(%rip),%rax
mov    (%rax),%esi
lea    -0x2038(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
jge    13e5 <main+0x225>
mov    0x2c21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2060(%rbp)
mov    0x2c0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x205c(%rbp)
mov    0x2bfb(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x2060(%rbp),%edi
mov    -0x205c(%rbp),%edx
mov    %rax,%rcx
lea    0xc86(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1492 <main+0x2d2>
jmp    13ea <main+0x22a>
mov    -0x14(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0x2000,%edx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    1483 <main+0x2c3>
mov    0x2bdc(%rip),%rax
mov    (%rax),%edi
lea    -0x2030(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   10b0 <writen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    147e <main+0x2be>
mov    0x2b7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2068(%rbp)
mov    0x2b65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2064(%rbp)
mov    0x2b56(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x2068(%rbp),%edi
mov    -0x2064(%rbp),%edx
mov    %rax,%rcx
lea    0xc05(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1483 <main+0x2c3>
mov    $0x64,%edi
call   10e0 <usleep@plt>
jmp    13ea <main+0x22a>
mov    -0x4(%rbp),%eax
add    $0x2070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
cmp    $0x2,%edi
jl     12ff <main+0x13f>
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
lea    0xe16(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     12ff <main+0x13f>
mov    $0x12c,%r14d
cmp    $0x3,%ebx
jb     120f <main+0x4f>
mov    0x10(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r14d
mov    0x2dc2(%rip),%rax
mov    0x2da3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbp),%rdi
mov    0x2db4(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
js     131d <main+0x15d>
mov    %eax,%r15d
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <cfmakeraw@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1030 <cfsetspeed@plt>
test   %rax,%rax
js     1354 <main+0x194>
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
call   1090 <tcsetattr@plt>
test   %rax,%rax
js     138a <main+0x1ca>
lea    0x10(%rsp),%r12
mov    0x2d66(%rip),%r13
lea    0xdeb(%rip),%r14
jmp    12aa <main+0xea>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x64,%edi
call   10e0 <usleep@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x2000,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    12a0 <main+0xe0>
mov    0x0(%r13),%edi
mov    %r12,%rsi
mov    %rax,%rdx
call   10b0 <writen@plt>
test   %rax,%rax
jns    12a0 <main+0xe0>
mov    0x2cde(%rip),%rax
mov    (%rax),%ebx
mov    0x2ccd(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10d0 <strerror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12a0 <main+0xe0>
xor    %eax,%eax
call   1080 <usage@plt>
mov    $0xffffffff,%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c94(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%rbp),%r14
mov    0x2c7f(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10d0 <strerror@plt>
lea    0xcc2(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %ebp,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1306 <main+0x146>
mov    0x2c5d(%rip),%rax
mov    (%rax),%ebx
mov    0x2c4c(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10d0 <strerror@plt>
lea    0xcb4(%rip),%rsi
mov    %ebx,%edi
mov    %r14d,%edx
mov    %ebp,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1306 <main+0x146>
mov    0x2c27(%rip),%rax
mov    (%rax),%ebx
mov    0x2c16(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10d0 <strerror@plt>
lea    0xcae(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1306 <main+0x146>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [3 x i8] c"-h\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"Failed to open %s. errno = %d (%s).\0A\00"
@errno = global i32 0
@.str.2 = [48 x i8] c"Failed to set baudrate to %d. errno = %d (%s).\0A\00"
@TCSANOW = global i32 0
@.str.3 = [36 x i8] c"tcsetattr failed. errno = %d (%s).\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [43 x i8] c"Error writing to stdout. errno = %d (%s).\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca [8192 x i8]
%10 = alloca %struct.termios
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 300, i32* %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 0, %18
br i1 %19, label %20, label %22
20: ; preds = %14, %2
%21 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %97
22: ; preds = %14
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 2
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %11
br label %30
30: ; preds = %25, %22
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDONLY
%35 = load i32, i32* @O_NOCTTY
%36 = or i32 %34, %35
%37 = load i32, i32* @O_NONBLOCK
%38 = or i32 %36, %37
%39 = call i32 @open(i8* %33, i32 %38)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %30
%43 = load i32, i32* @stderr
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = load i32, i32* @errno
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %46, i32 %47, i8* %49)
store i32 -1, i32* %3
br label %97
51: ; preds = %30
%52 = call i32 @cfmakeraw(%struct.termios* %10)
%53 = load i32, i32* %11
%54 = call i64 @cfsetspeed(%struct.termios* %10, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %51
%57 = load i32, i32* @stderr
%58 = load i32, i32* %11
%59 = load i32, i32* @errno
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %58, i32 %59, i8* %61)
store i32 -1, i32* %3
br label %97
63: ; preds = %51
%64 = load i32, i32* %6
%65 = load i32, i32* @TCSANOW
%66 = call i64 @tcsetattr(i32 %64, i32 %65, %struct.termios* %10)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %74
68: ; preds = %63
%69 = load i32, i32* @stderr
%70 = load i32, i32* @errno
%71 = load i32, i32* @errno
%72 = call i8* @strerror(i32 %71)
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %70, i8* %72)
store i32 -1, i32* %3
br label %97
74: ; preds = %63
br label %75
75: ; preds = %95, %74
%76 = load i32, i32* %6
%77 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%78 = call i64 @read(i32 %76, i8* %77, i32 8192)
store i64 %78, i64* %7
%79 = load i64, i64* %7
%80 = icmp sgt i64 %79, 0
br i1 %80, label %81, label %95
81: ; preds = %75
%82 = load i32, i32* @STDOUT_FILENO
%83 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%84 = load i64, i64* %7
%85 = call i64 @writen(i32 %82, i8* %83, i64 %84)
store i64 %85, i64* %8
%86 = load i64, i64* %8
%87 = icmp slt i64 %86, 0
br i1 %87, label %88, label %94
88: ; preds = %81
%89 = load i32, i32* @stderr
%90 = load i32, i32* @errno
%91 = load i32, i32* @errno
%92 = call i8* @strerror(i32 %91)
%93 = call i32 (i32, i8*, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %90, i8* %92)
br label %94
94: ; preds = %88, %81
br label %95
95: ; preds = %94, %75
%96 = call i32 @usleep(i32 100)
br label %75
97: ; preds = %68, %56, %42, %20
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [3 x i8] c"-h\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"Failed to open %s. errno = %d (%s).\0A\00"
@errno = global i32 0
@.str.2 = [48 x i8] c"Failed to set baudrate to %d. errno = %d (%s).\0A\00"
@TCSANOW = global i32 0
@.str.3 = [36 x i8] c"tcsetattr failed. errno = %d (%s).\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [43 x i8] c"Error writing to stdout. errno = %d (%s).\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = alloca %struct.termios
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.termios* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8, %2
%14 = tail call i32 (...) @usage()
br label %69
15: ; preds = %8
%16 = icmp ugt i32 %0, 2
br i1 %16, label %17, label %21
17: ; preds = %15
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
br label %21
21: ; preds = %17, %15
%22 = phi i32 [ %20, %17 ], [ 300, %15 ]
%23 = load i8*, i8** %9
%24 = load i32, i32* @O_RDONLY
%25 = load i32, i32* @O_NOCTTY
%26 = or i32 %25, %24
%27 = load i32, i32* @O_NONBLOCK
%28 = or i32 %26, %27
%29 = tail call i32 @open(i8* %23, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %9
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %33, i32 %34, i8* %35)
br label %69
37: ; preds = %21
%38 = call i32 @cfmakeraw(%struct.termios* %4)
%39 = call i64 @cfsetspeed(%struct.termios* %4, i32 %22)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %43, i8* %44)
br label %69
46: ; preds = %37
%47 = load i32, i32* @TCSANOW
%48 = call i64 @tcsetattr(i32 %29, i32 %47, %struct.termios* %4)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %55
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %52, i8* %53)
br label %69
55: ; preds = %46, %67
%56 = call i64 @read(i32 %29, i8* %5, i32 8192)
%57 = icmp sgt i64 %56, 0
br i1 %57, label %58, label %67
58: ; preds = %55
%59 = load i32, i32* @STDOUT_FILENO
%60 = call i64 @writen(i32 %59, i8* %5, i64 %56)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %58
%63 = load i32, i32* @stderr
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %64, i8* %65)
br label %67
67: ; preds = %58, %62, %55
%68 = call i32 @usleep(i32 100)
br label %55
69: ; preds = %50, %41, %31, %13
ret i32 -1
}