#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 scalar_t__ uint8_t;
typedef int /*<<< orphan*/ tio;
struct termios {
  int c_cflag;
  int /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int B9600;
int CREAD;
int CS8;
int /*<<< orphan*/ ICANON;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ bzero(struct termios *, int);
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ servo_send(int, int, scalar_t__);
int sin(float);
int /*<<< orphan*/ usleep(int);
int main(void) {
  int fd = open("/dev/serial/by-id/"
                "usb-Pololu_Corporation_Pololu_Micro_Maestro_6-Servo_"
                "Controller_00001644-if00",
                O_RDWR | O_NOCTTY);
  if (fd < 0) {
    printf("%s ", "/dev/ttyACM0");
    perror("couldn't open");
    exit(-1);
  }
  struct termios tio;
  bzero(&tio, sizeof(tio));
  tio.c_cflag = B9600 | CS8 | CREAD;
  tio.c_lflag = ICANON;
  int ii;
  uint8_t servo_out;
  for (ii = 0;; ii++) {
    servo_out = 128 + (uint8_t)(40 * sin(((float)ii) / 10.f));
    servo_send(fd, 1, servo_out);
    servo_send(fd, 2, servo_out);
    servo_send(fd, 3, servo_out);
    usleep(20000);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11e3 <main+0x63>
lea    0xea2(%rip),%rdi
lea    0xe9f(%rip),%rsi
call   1030 <printf@plt>
lea    0xea0(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    0x2dcf(%rip),%rcx
or     (%rcx),%eax
mov    0x2dce(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
cvtsi2ssl -0x14(%rbp),%xmm0
movss  0xdd1(%rip),%xmm1
divss  %xmm1,%xmm0
call   1040 <sin@plt>
imul   $0x28,%eax,%eax
cltq
add    $0x80,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    $0x1,%esi
call   1080 <servo_send@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    $0x2,%esi
call   1080 <servo_send@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    $0x3,%esi
call   1080 <servo_send@plt>
mov    $0x4e20,%edi
call   10a0 <usleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1222 <main+0xa2>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e6d(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe77(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1200 <main+0x90>
mov    %eax,%r14d
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xorps  %xmm0,%xmm0
cvtsi2ss %ebp,%xmm0
divss  0xe51(%rip),%xmm0
call   1040 <sin@plt>
shl    $0x3,%eax
lea    (%rax,%rax,4),%eax
movslq %eax,%rbx
sub    $0xffffffffffffff80,%rbx
mov    %r14d,%edi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1070 <servo_send@plt>
mov    %r14d,%edi
mov    $0x2,%esi
mov    %rbx,%rdx
call   1070 <servo_send@plt>
mov    %r14d,%edi
mov    $0x3,%esi
mov    %rbx,%rdx
call   1070 <servo_send@plt>
mov    $0x4e20,%edi
call   1090 <usleep@plt>
add    $0x1,%ebp
jmp    11a0 <main+0x30>
lea    0xe5c(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1030 <printf@plt>
lea    0xe5a(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32 }
@.str = [95 x i8] c"/dev/serial/by-id/usb-Pololu_Corporation_Pololu_Micro_Maestro_6-Servo_Controller_00001644-if00\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [4 x i8] c"%s \00"
@.str.2 = [13 x i8] c"/dev/ttyACM0\00"
@.str.3 = [14 x i8] c"couldn't open\00"
@B9600 = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@ICANON = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.termios
%4 = alloca i32
%5 = alloca i64
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = call i32 @bzero(%struct.termios* %3, i32 8)
%18 = load i32, i32* @B9600
%19 = load i32, i32* @CS8
%20 = or i32 %18, %19
%21 = load i32, i32* @CREAD
%22 = or i32 %20, %21
%23 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* @ICANON
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
store i32 %24, i32* %25
store i32 0, i32* %4
br label %26
26: ; preds = %44, %16
%27 = load i32, i32* %4
%28 = sitofp i32 %27 to float
%29 = fdiv float %28, 1.000000e+01
%30 = call i32 @sin(float %29)
%31 = mul nsw i32 40, %30
%32 = sext i32 %31 to i64
%33 = add nsw i64 128, %32
store i64 %33, i64* %5
%34 = load i32, i32* %2
%35 = load i64, i64* %5
%36 = call i32 @servo_send(i32 %34, i32 1, i64 %35)
%37 = load i32, i32* %2
%38 = load i64, i64* %5
%39 = call i32 @servo_send(i32 %37, i32 2, i64 %38)
%40 = load i32, i32* %2
%41 = load i64, i64* %5
%42 = call i32 @servo_send(i32 %40, i32 3, i64 %41)
%43 = call i32 @usleep(i32 20000)
br label %44
44: ; preds = %26
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
br label %26
}

LLVM IR (-O2):
@.str = [95 x i8] c"/dev/serial/by-id/usb-Pololu_Corporation_Pololu_Micro_Maestro_6-Servo_Controller_00001644-if00\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [4 x i8] c"%s \00"
@.str.2 = [13 x i8] c"/dev/ttyACM0\00"
@.str.3 = [14 x i8] c"couldn't open\00"
@B9600 = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@ICANON = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_NOCTTY
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%8 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %0, %10
%11 = phi i32 [ %22, %10 ], [ 0, %0 ]
%12 = sitofp i32 %11 to float
%13 = fdiv float %12, 1.000000e+01
%14 = tail call i32 @sin(float %13)
%15 = mul nsw i32 %14, 40
%16 = sext i32 %15 to i64
%17 = add nsw i64 %16, 128
%18 = tail call i32 @servo_send(i32 %4, i32 1, i64 %17)
%19 = tail call i32 @servo_send(i32 %4, i32 2, i64 %17)
%20 = tail call i32 @servo_send(i32 %4, i32 3, i64 %17)
%21 = tail call i32 @usleep(i32 20000)
%22 = add nuw nsw i32 %11, 1
br label %10
}