#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 /*<<< orphan*/ _IO(int, int /*<<< orphan*/);
int atoi(char *);
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int main(int argc, char **argv) {
  int motor, fd, error;
  char *motor_dev;
  short velocity;
  if (argc < 2) {
    printf("Usage: %s <motor> \n", argv[0]);
    return 1;
  }
  motor = atoi(argv[1]);
  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;
  }
  error = ioctl(fd, _IO(224, 0));
  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   $0x2,-0x8(%rbp)
jge    11b1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fb <main+0x18b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11d5 <main+0x65>
cmpl   $0x3,-0x14(%rbp)
jle    11ef <main+0x7f>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fb <main+0x18b>
mov    -0x14(%rbp),%eax
mov    %rax,-0x38(%rbp)
sub    $0x3,%rax
ja     124f <main+0xdf>
mov    -0x38(%rbp),%rax
lea    0xdf5(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe2b(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    124f <main+0xdf>
lea    0xe29(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    124f <main+0xdf>
lea    0xe27(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    124f <main+0xdf>
lea    0xe25(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    129c <main+0x12c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    %rax,%rdx
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fb <main+0x18b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    $0xe0,%edi
xor    %esi,%esi
call   1040 <_IO@plt>
mov    -0x40(%rbp),%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12f3 <main+0x183>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%esi
mov    %rax,%rdx
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fb <main+0x18b>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     119f <main+0x1f>
mov    (%rsi),%rsi
lea    0xe6d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1246 <main+0xc6>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
cmp    $0x4,%eax
jb     11be <main+0x3e>
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
jmp    1246 <main+0xc6>
mov    %eax,%r14d
lea    -0x1(%r14),%eax
cmp    $0x2,%eax
ja     11dc <main+0x5c>
cltq
lea    0xed5(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    11e3 <main+0x63>
lea    0xe31(%rip),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     121b <main+0x9b>
xor    %ebp,%ebp
mov    $0xe0,%edi
xor    %esi,%esi
call   1050 <_IO@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <ioctl@plt>
test   %eax,%eax
js     122b <main+0xab>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    124b <main+0xcb>
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xe23(%rip),%rdi
jmp    1239 <main+0xb9>
mov    %eax,%edi
call   10a0 <strerror@plt>
lea    0xe2f(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Usage: %s <motor> \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 = [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, 2
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 ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %61
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 i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %18
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 3
br i1 %27, label %28, label %30
28: ; preds = %25, %18
%29 = 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 %61
30: ; preds = %25
%31 = load i32, i32* %6
switch i32 %31, label %36 [
i32 0, label %32
i32 1, label %33
i32 2, label %34
i32 3, label %35
]
32: ; preds = %30
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8** %9
br label %36
33: ; preds = %30
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8** %9
br label %36
34: ; preds = %30
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8** %9
br label %36
35: ; preds = %30
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8** %9
br label %36
36: ; preds = %30, %35, %34, %33, %32
%37 = load i8*, i8** %9
%38 = load i32, i32* @O_WRONLY
%39 = call i32 @open(i8* %37, i32 %38)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %36
%43 = load i32, i32* %6
%44 = load i32, i32* %7
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %43, i8* %45)
store i32 1, i32* %3
br label %61
47: ; preds = %36
%48 = load i32, i32* %7
%49 = call i32 @_IO(i32 224, i32 0)
%50 = call i32 @ioctl(i32 %48, i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%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* %8
%56 = call i8* @strerror(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %54, i8* %56)
store i32 1, i32* %3
br label %61
58: ; preds = %47
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
br label %61
61: ; preds = %58, %53, %42, %28, %13
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [20 x i8] c"Usage: %s <motor> \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 = [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 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %38
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = icmp ugt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %38
14: ; preds = %7
%15 = add i32 %10, -1
%16 = icmp ult i32 %15, 3
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = sext i32 %15 to i64
%19 = shl i64 %18, 2
%20 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.main to i8*), i64 %19)
br label %21
21: ; preds = %17, %14
%22 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %14 ], [ %20, %17 ]
%23 = load i32, i32* @O_WRONLY
%24 = tail call i32 @open(i8* %22, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = tail call i8* @strerror(i32 %24)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %10, i8* %27)
br label %38
29: ; preds = %21
%30 = tail call i32 @_IO(i32 224, i32 0)
%31 = tail call i32 @ioctl(i32 %24, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = tail call i8* @strerror(i32 %31)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i32 %10, i8* %34)
br label %38
36: ; preds = %29
%37 = tail call i32 @close(i32 %24)
br label %38
38: ; preds = %36, %33, %26, %12, %4
%39 = phi i32 [ 1, %4 ], [ 1, %12 ], [ 1, %26 ], [ 1, %33 ], [ 0, %36 ]
ret i32 %39
}