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

/* Variables and functions */
int ECHO;
int ECHONL;
int ICANON;
int IEXTEN;
int ISIG;
int OCRNL;
int ONLCR;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ close(int);
int maestroGetPosition(int, int /*<<< orphan*/);
int open(char const *, int);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main() {
  const char *device = "/dev/ttyACM0";
  int fd = open(device, O_RDWR | O_NOCTTY);
  if (fd == -1) {
    perror(device);
    return 1;
  }
  struct termios options;
  tcgetattr(fd, &options);
  options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  options.c_oflag &= ~(ONLCR | OCRNL);
  tcsetattr(fd, TCSANOW, &options);
  int position = maestroGetPosition(fd, 0);
  printf("%i\n", position);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c7 <main+0x57>
mov    -0x10(%rbp),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1264 <main+0xf4>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    0x2dc5(%rip),%rcx
or     (%rcx),%eax
mov    0x2dac(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd3(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc2(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    0x2dd7(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1090 <maestroGetPosition@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdbd(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e61(%rip),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1239 <main+0xc9>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e19(%rip),%rax
mov    0x2dea(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2dcf(%rip),%rax
or     (%rax),%ecx
mov    0x2df6(%rip),%rax
or     (%rax),%ecx
mov    0x2de5(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x2db6(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0xc(%rsp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1090 <maestroGetPosition@plt>
lea    0xded(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    122e <main+0xbe>

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@ONLCR = global i32 0
@OCRNL = global i32 0
@TCSANOW = global i32 0
@.str.1 = [4 x i8] c"%i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.termios
%5 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = load i8*, i8** %2
%15 = call i32 @perror(i8* %14)
store i32 1, i32* %1
br label %48
16: ; preds = %0
%17 = load i32, i32* %3
%18 = call i32 @tcgetattr(i32 %17, %struct.termios* %4)
%19 = load i32, i32* @ECHO
%20 = load i32, i32* @ECHONL
%21 = or i32 %19, %20
%22 = load i32, i32* @ICANON
%23 = or i32 %21, %22
%24 = load i32, i32* @ISIG
%25 = or i32 %23, %24
%26 = load i32, i32* @IEXTEN
%27 = or i32 %25, %26
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%30 = load i32, i32* %29
%31 = and i32 %30, %28
store i32 %31, i32* %29
%32 = load i32, i32* @ONLCR
%33 = load i32, i32* @OCRNL
%34 = or i32 %32, %33
%35 = xor i32 %34, -1
%36 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%37 = load i32, i32* %36
%38 = and i32 %37, %35
store i32 %38, i32* %36
%39 = load i32, i32* %3
%40 = load i32, i32* @TCSANOW
%41 = call i32 @tcsetattr(i32 %39, i32 %40, %struct.termios* %4)
%42 = load i32, i32* %3
%43 = call i32 @maestroGetPosition(i32 %42, i32 0)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %3
%47 = call i32 @close(i32 %46)
store i32 0, i32* %1
br label %48
48: ; preds = %16, %13
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
%struct.termios = type { i32, i32 }
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@IEXTEN = global i32 0
@ONLCR = global i32 0
@OCRNL = global i32 0
@TCSANOW = global i32 0
@.str.1 = [4 x i8] c"%i\0A\00"
define i32 @main() {
%1 = alloca %struct.termios
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %37
9: ; preds = %0
%10 = bitcast %struct.termios* %1 to i8*
%11 = call i32 @tcgetattr(i32 %5, %struct.termios* %1)
%12 = load i32, i32* @ECHO
%13 = load i32, i32* @ECHONL
%14 = or i32 %13, %12
%15 = load i32, i32* @ICANON
%16 = or i32 %14, %15
%17 = load i32, i32* @ISIG
%18 = or i32 %16, %17
%19 = load i32, i32* @IEXTEN
%20 = or i32 %18, %19
%21 = xor i32 %20, -1
%22 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%23 = load i32, i32* %22
%24 = and i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* @ONLCR
%26 = load i32, i32* @OCRNL
%27 = or i32 %26, %25
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%30 = load i32, i32* %29
%31 = and i32 %30, %28
store i32 %31, i32* %29
%32 = load i32, i32* @TCSANOW
%33 = call i32 @tcsetattr(i32 %5, i32 %32, %struct.termios* %1)
%34 = call i32 @maestroGetPosition(i32 %5, i32 0)
%35 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = call i32 @close(i32 %5)
br label %37
37: ; preds = %9, %7
%38 = phi i32 [ 1, %7 ], [ 0, %9 ]
ret i32 %38
}