#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 /*<<< orphan*/ memcpy(char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int motor, fd, error;
  char *motor_dev;
  short velocity;
  int position;
  char outdata[6];
  if (argc < 4) {
    printf("Usage: %s <motor> <velocity> <position>\n", argv[0]);
    return 1;
  }
  motor = atoi(argv[1]);
  velocity = (short)atoi(argv[2]);
  position = atoi(argv[3]);
  if (motor < 0 || motor > 3) {
    printf("Invalid motor port: must be 0..3\n");
    return 1;
  }
  switch (motor) {
  case 0:
    motor_dev = "/dev/cbc/pid0";
    break;
  case 1:
    motor_dev = "/dev/cbc/pid1";
    break;
  case 2:
    motor_dev = "/dev/cbc/pid2";
    break;
  case 3:
    motor_dev = "/dev/cbc/pid3";
    break;
  }
  fd = open(motor_dev, O_WRONLY);
  if (fd < 0) {
    printf("Error opening motor %d: %s\n", motor, strerror(fd));
    return 1;
  }
  memcpy(outdata, &velocity, 2);
  memcpy(outdata + 2, &position, 4);
  printf("Setting moving motor %d to position %d at velocity %hd\n", motor,
         position, velocity);
  error = write(fd, outdata, 6);
  if (error < 0) {
    printf("Error writing to motor %d: %s\n", motor, strerror(error));
    return 1;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11b1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1355 <main+0x1e5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %ax,-0x2a(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11f6 <main+0x86>
cmpl   $0x3,-0x14(%rbp)
jle    1210 <main+0xa0>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1355 <main+0x1e5>
mov    -0x14(%rbp),%eax
mov    %rax,-0x40(%rbp)
sub    $0x3,%rax
ja     1270 <main+0x100>
mov    -0x40(%rbp),%rax
lea    0xdd4(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe1f(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1270 <main+0x100>
lea    0xe1d(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1270 <main+0x100>
lea    0xe1b(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1270 <main+0x100>
lea    0xe19(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12bd <main+0x14d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%esi
mov    %rax,%rdx
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1355 <main+0x1e5>
lea    -0x36(%rbp),%rdi
xor    %eax,%eax
lea    -0x2a(%rbp),%rsi
mov    $0x2,%edx
call   1060 <memcpy@plt>
lea    -0x36(%rbp),%rdi
add    $0x2,%rdi
xor    %eax,%eax
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%edx
movswl -0x2a(%rbp),%ecx
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x36(%rbp),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    134d <main+0x1dd>
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x1c(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x48(%rbp),%esi
mov    %rax,%rdx
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1355 <main+0x1e5>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
jg     11a9 <main+0x29>
mov    0x0(%rbp),%rsi
lea    0xe63(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12ae <main+0x12e>
mov    0x8(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
call   1090 <atoi@plt>
mov    %ax,0xa(%rsp)
mov    0x18(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,0xc(%rsp)
cmp    $0x4,%ebx
jb     11e5 <main+0x65>
lea    0xef9(%rip),%rdi
call   1030 <puts@plt>
jmp    12ae <main+0x12e>
lea    -0x1(%rbx),%eax
cmp    $0x2,%eax
ja     11ff <main+0x7f>
cltq
lea    0xf02(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    1206 <main+0x86>
lea    0xe23(%rip),%rdi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1283 <main+0x103>
xor    %ebp,%ebp
lea    0x12(%rsp),%r15
lea    0xa(%rsp),%rsi
mov    %r15,%rdi
mov    $0x2,%edx
xor    %eax,%eax
call   1070 <memcpy@plt>
lea    0x14(%rsp),%rdi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1070 <memcpy@plt>
mov    0xc(%rsp),%edx
movswl 0xa(%rsp),%ecx
lea    0xe21(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x6,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1294 <main+0x114>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    12b3 <main+0x133>
mov    %r14d,%edi
call   10a0 <strerror@plt>
lea    0xdcf(%rip),%rdi
jmp    12a2 <main+0x122>
mov    %eax,%edi
call   10a0 <strerror@plt>
lea    0xe13(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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