#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 */

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int write(int, short *, int);
int main(int argc, char **argv) {
  int servo, fd, error;
  char *servo_dev;
  short position;
  if (argc < 3) {
    printf("Usage: %s <servo> <position>\n", argv[0]);
    return 1;
  }
  servo = atoi(argv[1]);
  position = (short)atoi(argv[2]);
  if (servo < 0 || servo > 3) {
    printf("Invalid servo port: must be 0..3\n");
    return 1;
  }
  if (position < -1 || position > 2048) {
    printf("Invalid servo value: must be from -1..2048\n");
    return 1;
  }
  switch (servo) {
  case 0:
    servo_dev = "/dev/cbc/servo0";
    break;
  case 1:
    servo_dev = "/dev/cbc/servo1";
    break;
  case 2:
    servo_dev = "/dev/cbc/servo2";
    break;
  case 3:
    servo_dev = "/dev/cbc/servo3";
    break;
  }
  fd = open(servo_dev, O_WRONLY);
  if (fd < 0) {
    printf("Error opening servo %d: %s\n", servo, strerror(fd));
    return 1;
  }
  printf("Setting servo %d to position: %hd\n", servo, position);
  error = write(fd, &position, 2);
  if (error < 0) {
    printf("Error writing to servo %d: %s\n", servo, strerror(error));
    return 1;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11a1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x1dc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %ax,-0x2a(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11d6 <main+0x76>
cmpl   $0x3,-0x14(%rbp)
jle    11f0 <main+0x90>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x1dc>
movswl -0x2a(%rbp),%eax
cmp    $0xffffffff,%eax
jl     120c <main+0xac>
movswl -0x2a(%rbp),%eax
cmp    $0x800,%eax
jle    1226 <main+0xc6>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x1dc>
mov    -0x14(%rbp),%eax
mov    %rax,-0x38(%rbp)
sub    $0x3,%rax
ja     1286 <main+0x126>
mov    -0x38(%rbp),%rax
lea    0xdbe(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe2a(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1286 <main+0x126>
lea    0xe2a(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1286 <main+0x126>
lea    0xe2a(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1286 <main+0x126>
lea    0xe2a(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12d3 <main+0x173>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    %rax,%rdx
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x1dc>
mov    -0x14(%rbp),%esi
movswl -0x2a(%rbp),%edx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x2a(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1334 <main+0x1d4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%esi
mov    %rax,%rdx
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <main+0x1dc>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     1192 <main+0x22>
mov    (%rbx),%rsi
lea    0xe77(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11d9 <main+0x69>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
mov    %bx,0x6(%rsp)
cmp    $0x4,%r14d
jb     11bd <main+0x4d>
lea    0xf2c(%rip),%rdi
jmp    11d4 <main+0x64>
shl    $0x10,%ebx
lea    -0x8000001(%rbx),%eax
cmp    $0xf7fefffe,%eax
ja     11eb <main+0x7b>
lea    0xee8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    -0x1(%r14),%eax
cmp    $0x2,%eax
ja     1206 <main+0x96>
cltq
lea    0xf0b(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    120d <main+0x9d>
lea    0xe11(%rip),%rdi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     125a <main+0xea>
sar    $0x10,%ebx
lea    0xe4e(%rip),%rdi
xor    %ebp,%ebp
mov    %r14d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x6(%rsp),%rsi
mov    %r15d,%edi
mov    $0x2,%edx
call   1040 <write@plt>
test   %eax,%eax
js     126b <main+0xfb>
mov    %r15d,%edi
call   1060 <close@plt>
jmp    11de <main+0x6e>
mov    %r15d,%edi
call   1090 <strerror@plt>
lea    0xdf5(%rip),%rdi
jmp    1279 <main+0x109>
mov    %eax,%edi
call   1090 <strerror@plt>
lea    0xe24(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11d9 <main+0x69>

LLVM IR (-O0):
@.str = [30 x i8] c"Usage: %s <servo> <position>\0A\00"
@.str.1 = [34 x i8] c"Invalid servo port: must be 0..3\0A\00"
@.str.2 = [44 x i8] c"Invalid servo value: must be from -1..2048\0A\00"
@.str.3 = [16 x i8] c"/dev/cbc/servo0\00"
@.str.4 = [16 x i8] c"/dev/cbc/servo1\00"
@.str.5 = [16 x i8] c"/dev/cbc/servo2\00"
@.str.6 = [16 x i8] c"/dev/cbc/servo3\00"
@O_WRONLY = global i32 0
@.str.7 = [28 x i8] c"Error opening servo %d: %s\0A\00"
@.str.8 = [35 x i8] c"Setting servo %d to position: %hd\0A\00"
@.str.9 = [31 x i8] c"Error writing to servo %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 i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i16
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %79
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
%27 = trunc i32 %26 to i16
store i16 %27, i16* %10
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %18
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 3
br i1 %32, label %33, label %35
33: ; preds = %30, %18
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %79
35: ; preds = %30
%36 = load i16, i16* %10
%37 = sext i16 %36 to i32
%38 = icmp slt i32 %37, -1
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i16, i16* %10
%41 = sext i16 %40 to i32
%42 = icmp sgt i32 %41, 2048
br i1 %42, label %43, label %45
43: ; preds = %39, %35
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %79
45: ; preds = %39
%46 = load i32, i32* %6
switch i32 %46, label %51 [
i32 0, label %47
i32 1, label %48
i32 2, label %49
i32 3, label %50
]
47: ; preds = %45
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8** %9
br label %51
48: ; preds = %45
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8** %9
br label %51
49: ; preds = %45
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8** %9
br label %51
50: ; preds = %45
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8** %9
br label %51
51: ; preds = %45, %50, %49, %48, %47
%52 = load i8*, i8** %9
%53 = load i32, i32* @O_WRONLY
%54 = call i32 @open(i8* %52, i32 %53)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %51
%58 = load i32, i32* %6
%59 = load i32, i32* %7
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %58, i8* %60)
store i32 1, i32* %3
br label %79
62: ; preds = %51
%63 = load i32, i32* %6
%64 = load i16, i16* %10
%65 = sext i16 %64 to i32
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0), i32 %63, i32 %65)
%67 = load i32, i32* %7
%68 = call i32 @write(i32 %67, i16* %10, i32 2)
store i32 %68, i32* %8
%69 = load i32, i32* %8
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %62
%72 = load i32, i32* %6
%73 = load i32, i32* %8
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i32 %72, i8* %74)
store i32 1, i32* %3
br label %79
76: ; preds = %62
%77 = load i32, i32* %7
%78 = call i32 @close(i32 %77)
br label %79
79: ; preds = %76, %71, %57, %43, %33, %13
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@.str = [30 x i8] c"Usage: %s <servo> <position>\0A\00"
@.str.3 = [16 x i8] c"/dev/cbc/servo0\00"
@.str.4 = [16 x i8] c"/dev/cbc/servo1\00"
@.str.5 = [16 x i8] c"/dev/cbc/servo2\00"
@.str.6 = [16 x i8] c"/dev/cbc/servo3\00"
@O_WRONLY = global i32 0
@.str.7 = [28 x i8] c"Error opening servo %d: %s\0A\00"
@.str.8 = [35 x i8] c"Setting servo %d to position: %hd\0A\00"
@.str.9 = [31 x i8] c"Error writing to servo %d: %s\0A\00"
@str = [43 x i8] c"Invalid servo value: must be from -1..2048\00"
@str.10 = [33 x i8] c"Invalid servo port: must be 0..3\00"
@reltable.main = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([16 x i8]* @.str.4 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([16 x i8]* @.str.5 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([16 x i8]* @.str.6 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32)]
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = bitcast i16* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %51
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = trunc i32 %15 to i16
store i16 %16, i16* %3
%17 = icmp ugt i32 %12, 3
br i1 %17, label %18, label %20
18: ; preds = %9
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.10, i64 0, i64 0))
br label %51
20: ; preds = %9
%21 = shl i32 %15, 16
%22 = add i32 %21, -134217729
%23 = icmp ult i32 %22, -134283265
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
br label %51
26: ; preds = %20
%27 = add i32 %12, -1
%28 = icmp ult i32 %27, 3
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = sext i32 %27 to i64
%31 = shl i64 %30, 2
%32 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.main to i8*), i64 %31)
br label %33
33: ; preds = %29, %26
%34 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), %26 ], [ %32, %29 ]
%35 = load i32, i32* @O_WRONLY
%36 = tail call i32 @open(i8* %34, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = tail call i8* @strerror(i32 %36)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %12, i8* %39)
br label %51
41: ; preds = %33
%42 = ashr exact i32 %21, 16
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0), i32 %12, i32 %42)
%44 = call i32 @write(i32 %36, i16* %3, i32 2)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = tail call i8* @strerror(i32 %44)
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i32 %12, i8* %47)
br label %51
49: ; preds = %41
%50 = tail call i32 @close(i32 %36)
br label %51
51: ; preds = %49, %46, %38, %24, %18, %6
%52 = phi i32 [ 1, %6 ], [ 1, %18 ], [ 1, %24 ], [ 1, %38 ], [ 1, %46 ], [ 0, %49 ]
ret i32 %52
}