#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 open(char const *, int);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ smcExitSafeStart(int);
int smcGetErrorStatus(int);
int smcGetTargetSpeed(int);
int /*<<< orphan*/ smcSetTargetSpeed(int, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main() {
  printf("Starting the motor controller example\n");
  const char *device = "/dev/ttyO4";
  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);
  smcExitSafeStart(fd);
  printf("Error status: 0x%04x\n", smcGetErrorStatus(fd));
  int speed = smcGetTargetSpeed(fd);
  printf("Current Target Speed is %d.\n", speed);
  int newSpeed = (speed <= 0) ? 3200 : -3200;
  printf("Setting Target Speed to %d.\n", newSpeed);
  smcSetTargetSpeed(fd, newSpeed);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe63(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1205 <main+0x65>
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f4 <main+0x154>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    0x2d87(%rip),%rcx
or     (%rcx),%eax
mov    0x2d6e(%rip),%rcx
or     (%rcx),%eax
mov    0x2d95(%rip),%rcx
or     (%rcx),%eax
mov    0x2d84(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    0x2d99(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0x14(%rbp),%edi
call   1090 <smcExitSafeStart@plt>
mov    -0x14(%rbp),%edi
call   1060 <smcGetErrorStatus@plt>
mov    %eax,%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1030 <smcGetTargetSpeed@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%edx
mov    $0xfffff380,%eax
mov    $0xc80,%ecx
cmp    $0x0,%edx
cmovle %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%esi
call   10c0 <smcSetTargetSpeed@plt>
mov    -0x14(%rbp),%edi
call   1050 <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
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e15(%rip),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe23(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     12cf <main+0x11f>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %eax,%edi
mov    %rbp,%rsi
call   1080 <tcgetattr@plt>
mov    0x2dcd(%rip),%rax
mov    0x2d9e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d83(%rip),%rax
or     (%rax),%ecx
mov    0x2daa(%rip),%rax
or     (%rax),%ecx
mov    0x2d99(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x2d6a(%rip),%rax
mov    0x2db3(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0xc(%rsp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1090 <tcsetattr@plt>
mov    %ebx,%edi
call   10a0 <smcExitSafeStart@plt>
mov    %ebx,%edi
call   1070 <smcGetErrorStatus@plt>
lea    0xd9c(%rip),%rdi
xor    %r14d,%r14d
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1040 <smcGetTargetSpeed@plt>
mov    %eax,%ebp
lea    0xd96(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
mov    $0xc80,%eax
mov    $0xfffff380,%ebp
cmovle %eax,%ebp
lea    0xd94(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   10d0 <smcSetTargetSpeed@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd2a(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%r14d
jmp    12c3 <main+0x113>

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@.str = [39 x i8] c"Starting the motor controller example\0A\00"
@.str.1 = [11 x i8] c"/dev/ttyO4\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.2 = [22 x i8] c"Error status: 0x%04x\0A\00"
@.str.3 = [29 x i8] c"Current Target Speed is %d.\0A\00"
@.str.4 = [29 x i8] c"Setting Target Speed to %d.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.termios
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8** %2
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = load i8*, i8** %2
%17 = call i32 @perror(i8* %16)
store i32 1, i32* %1
br label %64
18: ; preds = %0
%19 = load i32, i32* %3
%20 = call i32 @tcgetattr(i32 %19, %struct.termios* %4)
%21 = load i32, i32* @ECHO
%22 = load i32, i32* @ECHONL
%23 = or i32 %21, %22
%24 = load i32, i32* @ICANON
%25 = or i32 %23, %24
%26 = load i32, i32* @ISIG
%27 = or i32 %25, %26
%28 = load i32, i32* @IEXTEN
%29 = or i32 %27, %28
%30 = xor i32 %29, -1
%31 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%32 = load i32, i32* %31
%33 = and i32 %32, %30
store i32 %33, i32* %31
%34 = load i32, i32* @ONLCR
%35 = load i32, i32* @OCRNL
%36 = or i32 %34, %35
%37 = xor i32 %36, -1
%38 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%39 = load i32, i32* %38
%40 = and i32 %39, %37
store i32 %40, i32* %38
%41 = load i32, i32* %3
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %41, i32 %42, %struct.termios* %4)
%44 = load i32, i32* %3
%45 = call i32 @smcExitSafeStart(i32 %44)
%46 = load i32, i32* %3
%47 = call i32 @smcGetErrorStatus(i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %3
%50 = call i32 @smcGetTargetSpeed(i32 %49)
store i32 %50, i32* %5
%51 = load i32, i32* %5
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %5
%54 = icmp sle i32 %53, 0
%55 = zext i1 %54 to i64
%56 = select i1 %54, i32 3200, i32 -3200
store i32 %56, i32* %6
%57 = load i32, i32* %6
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %3
%60 = load i32, i32* %6
%61 = call i32 @smcSetTargetSpeed(i32 %59, i32 %60)
%62 = load i32, i32* %3
%63 = call i32 @close(i32 %62)
store i32 0, i32* %1
br label %64
64: ; preds = %18, %15
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
%struct.termios = type { i32, i32 }
@.str.1 = [11 x i8] c"/dev/ttyO4\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.2 = [22 x i8] c"Error status: 0x%04x\0A\00"
@.str.3 = [29 x i8] c"Current Target Speed is %d.\0A\00"
@.str.4 = [29 x i8] c"Setting Target Speed to %d.\0A\00"
@str = [38 x i8] c"Starting the motor controller example\00"
define i32 @main() {
%1 = alloca %struct.termios
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %45
10: ; preds = %0
%11 = bitcast %struct.termios* %1 to i8*
%12 = call i32 @tcgetattr(i32 %6, %struct.termios* %1)
%13 = load i32, i32* @ECHO
%14 = load i32, i32* @ECHONL
%15 = or i32 %14, %13
%16 = load i32, i32* @ICANON
%17 = or i32 %15, %16
%18 = load i32, i32* @ISIG
%19 = or i32 %17, %18
%20 = load i32, i32* @IEXTEN
%21 = or i32 %19, %20
%22 = xor i32 %21, -1
%23 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, %22
store i32 %25, i32* %23
%26 = load i32, i32* @ONLCR
%27 = load i32, i32* @OCRNL
%28 = or i32 %27, %26
%29 = xor i32 %28, -1
%30 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%31 = load i32, i32* %30
%32 = and i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %6, i32 %33, %struct.termios* %1)
%35 = call i32 @smcExitSafeStart(i32 %6)
%36 = call i32 @smcGetErrorStatus(i32 %6)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = call i32 @smcGetTargetSpeed(i32 %6)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = icmp slt i32 %38, 1
%41 = select i1 %40, i32 3200, i32 -3200
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = call i32 @smcSetTargetSpeed(i32 %6, i32 %41)
%44 = call i32 @close(i32 %6)
br label %45
45: ; preds = %10, %8
%46 = phi i32 [ 1, %8 ], [ 0, %10 ]
ret i32 %46
}