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

/* Variables and functions */
int /*<<< orphan*/ B9600;
int CLOCAL;
int CREAD;
int CS8;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int write(int, char *, int /*<<< orphan*/);
int termios_test(void) {
  struct termios tio;
  int tty_fd;
  const char *device = "/dev/ttyACM0";
  int i;
  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(device, O_RDWR | O_NONBLOCK);
  if (tty_fd < 0) {
    perror("open");
    return 1;
  }
  cfsetospeed(&tio, B9600);
  tcsetattr(tty_fd, TCSANOW, &tio);
  for (i = 0; i < 3; ++i) {
    printf("wrote %d\n", write(tty_fd, "~out9=1~", strlen("~out9=1~")));
    sleep(1);
    printf("wrote %d\n", write(tty_fd, "~out9=0~", strlen("~out9=0~")));
    sleep(1);
  }
  close(tty_fd);
  return 0;
}


x86 assembly (clang, -O0):
<termios_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    0xe51(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    0x2de5(%rip),%rcx
or     (%rcx),%eax
mov    0x2dac(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2db4(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x40(%rbp),%rdi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1263 <termios_test+0xc3>
lea    0xdbb(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    132e <termios_test+0x18e>
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1040 <cfsetospeed@plt>
mov    -0x34(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1090 <tcsetattr@plt>
movl   $0x0,-0x44(%rbp)
cmpl   $0x3,-0x44(%rbp)
jge    131f <termios_test+0x17f>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    0xd74(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    %eax,%edx
lea    0xd63(%rip),%rsi
call   1030 <write@plt>
mov    %eax,%esi
lea    0xd4b(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x48(%rbp)
lea    0xd42(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%edi
mov    %eax,%edx
lea    0xd31(%rip),%rsi
call   1030 <write@plt>
mov    %eax,%esi
lea    0xd10(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1291 <termios_test+0xf1>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<termios_test>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
lea    0x8(%rsp),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x28,%edx
call   1070 <memset@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
mov    0x2df1(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2db7(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2dbc(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d9b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2dca(%rip),%rax
mov    0x2dbb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdd0(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1384 <termios_test+0x1e4>
mov    %eax,%ebp
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <cfsetospeed@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1090 <tcsetattr@plt>
lea    0xdad(%rip),%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xd88(%rip),%r15
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
lea    0xd80(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc82(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r14d
jmp    1374 <termios_test+0x1d4>

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i64, i64 }
@.str = [13 x i8] c"/dev/ttyACM0\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [5 x i8] c"open\00"
@B9600 = global i32 0
@TCSANOW = global i32 0
@.str.2 = [10 x i8] c"wrote %d\0A\00"
@.str.3 = [9 x i8] c"~out9=1~\00"
@.str.4 = [9 x i8] c"~out9=0~\00"
define i32 @termios_test() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %4
%6 = call i32 @memset(%struct.termios* %2, i32 0, i32 40)
%7 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 4
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 3
store i64 0, i64* %8
%9 = load i32, i32* @CS8
%10 = load i32, i32* @CREAD
%11 = or i32 %9, %10
%12 = load i32, i32* @CLOCAL
%13 = or i32 %11, %12
%14 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 2
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%17 = load i32*, i32** %16
%18 = load i64, i64* @VMIN
%19 = getelementptr inbounds i32, i32* %17, i64 %18
store i32 1, i32* %19
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%21 = load i32*, i32** %20
%22 = load i64, i64* @VTIME
%23 = getelementptr inbounds i32, i32* %21, i64 %22
store i32 5, i32* %23
%24 = load i8*, i8** %4
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_NONBLOCK
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %24, i32 %27)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %0
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %59
33: ; preds = %0
%34 = load i32, i32* @B9600
%35 = call i32 @cfsetospeed(%struct.termios* %2, i32 %34)
%36 = load i32, i32* %3
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 %36, i32 %37, %struct.termios* %2)
store i32 0, i32* %5
br label %39
39: ; preds = %53, %33
%40 = load i32, i32* %5
%41 = icmp slt i32 %40, 3
br i1 %41, label %42, label %56
42: ; preds = %39
%43 = load i32, i32* %3
%44 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @write(i32 %43, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%46 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = call i32 @sleep(i32 1)
%48 = load i32, i32* %3
%49 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @write(i32 %48, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = call i32 @sleep(i32 1)
br label %53
53: ; preds = %42
%54 = load i32, i32* %5
%55 = add nsw i32 %54, 1
store i32 %55, i32* %5
br label %39
56: ; preds = %39
%57 = load i32, i32* %3
%58 = call i32 @close(i32 %57)
store i32 0, i32* %1
br label %59
59: ; preds = %56, %31
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i64, i64 }
@.str = [13 x i8] c"/dev/ttyACM0\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [5 x i8] c"open\00"
@B9600 = global i32 0
@TCSANOW = global i32 0
@.str.2 = [10 x i8] c"wrote %d\0A\00"
@.str.3 = [9 x i8] c"~out9=1~\00"
@.str.4 = [9 x i8] c"~out9=0~\00"
define i32 @termios_test() {
%1 = alloca %struct.termios
%2 = bitcast %struct.termios* %1 to i8*
%3 = call i32 @memset(%struct.termios* %1, i32 0, i32 40)
%4 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 3
%5 = bitcast i64* %4 to i8*
%6 = load i32, i32* @CS8
%7 = load i32, i32* @CREAD
%8 = or i32 %7, %6
%9 = load i32, i32* @CLOCAL
%10 = or i32 %8, %9
%11 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 2
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%14 = load i32*, i32** %13
%15 = load i64, i64* @VMIN
%16 = getelementptr inbounds i32, i32* %14, i64 %15
store i32 1, i32* %16
%17 = load i64, i64* @VTIME
%18 = getelementptr inbounds i32, i32* %14, i64 %17
store i32 5, i32* %18
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %20, %19
%22 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %0
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %56
26: ; preds = %0
%27 = load i32, i32* @B9600
%28 = call i32 @cfsetospeed(%struct.termios* %1, i32 %27)
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %22, i32 %29, %struct.termios* %1)
%31 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @write(i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i32 @sleep(i32 1)
%35 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @write(i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = call i32 @sleep(i32 1)
%39 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @write(i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = call i32 @sleep(i32 1)
%43 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @write(i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%45 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = call i32 @sleep(i32 1)
%47 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @write(i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %48)
%50 = call i32 @sleep(i32 1)
%51 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%52 = call i32 @write(i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %51)
%53 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %52)
%54 = call i32 @sleep(i32 1)
%55 = call i32 @close(i32 %22)
br label %56
56: ; preds = %26, %24
%57 = phi i32 [ 1, %24 ], [ 0, %26 ]
ret i32 %57
}