#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 motor, fd, error;
  char *motor_dev;
  short velocity;
  if (argc < 3) {
    printf("Usage: %s <motor> <velocity>\n", argv[0]);
    return 1;
  }
  motor = atoi(argv[1]);
  velocity = (short)atoi(argv[2]);
  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;
  }
  printf("Setting motor %d to velocity: %hd\n", motor, velocity);
  error = write(fd, &velocity, 2);
  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    $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    1306 <main+0x1a6>
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    1306 <main+0x1a6>
mov    -0x14(%rbp),%eax
mov    %rax,-0x38(%rbp)
sub    $0x3,%rax
ja     1250 <main+0xf0>
mov    -0x38(%rbp),%rax
lea    0xdf4(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe34(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1250 <main+0xf0>
lea    0xe32(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1250 <main+0xf0>
lea    0xe30(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1250 <main+0xf0>
lea    0xe2e(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    129d <main+0x13d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    %rax,%rdx
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1306 <main+0x1a6>
mov    -0x14(%rbp),%esi
movswl -0x2a(%rbp),%edx
lea    0xdf9(%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    12fe <main+0x19e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%esi
mov    %rax,%rdx
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1306 <main+0x1a6>
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   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     1196 <main+0x26>
mov    (%rbx),%rsi
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    125e <main+0xee>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    %bp,0xe(%rsp)
cmp    $0x4,%r14d
jb     11c9 <main+0x59>
lea    0xef5(%rip),%rdi
call   1030 <puts@plt>
jmp    125e <main+0xee>
lea    -0x1(%r14),%eax
cmp    $0x2,%eax
ja     11e4 <main+0x74>
cltq
lea    0xefd(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    11eb <main+0x7b>
lea    0xe33(%rip),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1233 <main+0xc3>
movswl %bp,%edx
lea    0xe69(%rip),%rdi
xor    %ebp,%ebp
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1243 <main+0xd3>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1263 <main+0xf3>
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xe15(%rip),%rdi
jmp    1251 <main+0xe1>
mov    %eax,%edi
call   1090 <strerror@plt>
lea    0xe44(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Usage: %s <motor> <velocity>\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 = [35 x i8] c"Setting motor %d to 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
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 %69
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 %69
35: ; preds = %30
%36 = load i32, i32* %6
switch i32 %36, label %41 [
i32 0, label %37
i32 1, label %38
i32 2, label %39
i32 3, label %40
]
37: ; preds = %35
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8** %9
br label %41
38: ; preds = %35
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8** %9
br label %41
39: ; preds = %35
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8** %9
br label %41
40: ; preds = %35
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8** %9
br label %41
41: ; preds = %35, %40, %39, %38, %37
%42 = load i8*, i8** %9
%43 = load i32, i32* @O_WRONLY
%44 = call i32 @open(i8* %42, i32 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = load i32, i32* %6
%49 = load i32, i32* %7
%50 = call i8* @strerror(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %48, i8* %50)
store i32 1, i32* %3
br label %69
52: ; preds = %41
%53 = load i32, i32* %6
%54 = load i16, i16* %10
%55 = sext i16 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %53, i32 %55)
%57 = load i32, i32* %7
%58 = call i32 @write(i32 %57, i16* %10, i32 2)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %52
%62 = load i32, i32* %6
%63 = load i32, i32* %8
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i32 %62, i8* %64)
store i32 1, i32* %3
br label %69
66: ; preds = %52
%67 = load i32, i32* %7
%68 = call i32 @close(i32 %67)
br label %69
69: ; preds = %66, %61, %47, %33, %13
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@.str = [30 x i8] c"Usage: %s <motor> <velocity>\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 = [35 x i8] c"Setting motor %d to 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 = 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 %46
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, i64 0, i64 0))
br label %46
20: ; preds = %9
%21 = add i32 %12, -1
%22 = icmp ult i32 %21, 3
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = sext i32 %21 to i64
%25 = shl i64 %24, 2
%26 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.main to i8*), i64 %25)
br label %27
27: ; preds = %23, %20
%28 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %20 ], [ %26, %23 ]
%29 = load i32, i32* @O_WRONLY
%30 = tail call i32 @open(i8* %28, i32 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = tail call i8* @strerror(i32 %30)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %12, i8* %33)
br label %46
35: ; preds = %27
%36 = shl i32 %15, 16
%37 = ashr exact i32 %36, 16
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %12, i32 %37)
%39 = call i32 @write(i32 %30, i16* %3, i32 2)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = tail call i8* @strerror(i32 %39)
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i32 %12, i8* %42)
br label %46
44: ; preds = %35
%45 = tail call i32 @close(i32 %30)
br label %46
46: ; preds = %44, %41, %32, %18, %6
%47 = phi i32 [ 1, %6 ], [ 1, %18 ], [ 1, %32 ], [ 1, %41 ], [ 0, %44 ]
ret i32 %47
}