#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*/ _IO(char, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int on;
  int fd;
  printf("BBB LED control app!\n");
  if ((argc != 2) || (sscanf(argv[1], "%d", &on) != 1) || (on < 0) ||
      (on > 1)) {
    fprintf(stderr, "Usage:%s [0|1]\n", argv[0]);
    exit(1);
  }
  fd = open("/dev/bbb_leds", 0);
  if (fd < 0) {
    perror("open device leds failed\n");
    exit(1);
  }
  if (on) {
    printf("turn on led!\n");
    ioctl(fd, _IO('k', 1), 0);
  } else {
    printf("turn off led!\n");
    ioctl(fd, _IO('k', 2), 0);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jne    11f1 <main+0x61>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4b(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    11f1 <main+0x61>
cmpl   $0x0,-0x14(%rbp)
jl     11f1 <main+0x61>
cmpl   $0x1,-0x14(%rbp)
jle    1217 <main+0x87>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe11(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe0b(%rip),%rdi
xor    %esi,%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1248 <main+0xb8>
lea    0xdfe(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
je     1284 <main+0xf4>
lea    0xdf7(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x6b,%edi
mov    $0x1,%esi
call   1040 <_IO@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
jmp    12b1 <main+0x121>
lea    0xdd3(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x6b,%edi
mov    $0x2,%esi
call   1040 <_IO@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x2,%ebp
jne    1234 <main+0xa4>
mov    0x8(%rbx),%rdi
lea    0xe48(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    1234 <main+0xa4>
mov    0x4(%rsp),%eax
test   %eax,%eax
js     1234 <main+0xa4>
cmp    $0x2,%eax
jge    1234 <main+0xa4>
lea    0xe38(%rip),%rdi
xor    %esi,%esi
call   1090 <open@plt>
test   %eax,%eax
js     1256 <main+0xc6>
mov    %eax,%ebx
xor    %ebp,%ebp
cmpl   $0x0,0x4(%rsp)
sete   %bpl
lea    0xe55(%rip),%rax
lea    0xe5c(%rip),%rdi
cmove  %rax,%rdi
add    $0x1,%ebp
call   1030 <puts@plt>
mov    $0x6b,%edi
mov    %ebp,%esi
call   1040 <_IO@plt>
mov    %ebx,%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdbc(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"BBB LED control app!\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"Usage:%s [0|1]\0A\00"
@.str.3 = [14 x i8] c"/dev/bbb_leds\00"
@.str.4 = [25 x i8] c"open device leds failed\0A\00"
@.str.5 = [14 x i8] c"turn on led!\0A\00"
@.str.6 = [15 x i8] c"turn off led!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %23, label %11
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%16 = icmp ne i32 %15, 1
br i1 %16, label %23, label %17
17: ; preds = %11
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 1
br i1 %22, label %23, label %30
23: ; preds = %20, %17, %11, %2
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 0)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%42 = load i32, i32* %7
%43 = call i32 @_IO(i8 signext 107, i32 1)
%44 = call i32 @ioctl(i32 %42, i32 %43, i32 0)
br label %50
45: ; preds = %37
%46 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%47 = load i32, i32* %7
%48 = call i32 @_IO(i8 signext 107, i32 2)
%49 = call i32 @ioctl(i32 %47, i32 %48, i32 0)
br label %50
50: ; preds = %45, %40
%51 = load i32, i32* %7
%52 = call i32 @close(i32 %51)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"Usage:%s [0|1]\0A\00"
@.str.3 = [14 x i8] c"/dev/bbb_leds\00"
@.str.4 = [25 x i8] c"open device leds failed\0A\00"
@str = [21 x i8] c"BBB LED control app!\00"
@str.7 = [14 x i8] c"turn off led!\00"
@str.8 = [13 x i8] c"turn on led!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%6 = icmp eq i32 %0, 2
br i1 %6, label %7, label %17
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%11 = icmp ne i32 %10, 1
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
%14 = select i1 %11, i1 true, i1 %13
%15 = icmp sgt i32 %12, 1
%16 = select i1 %14, i1 true, i1 %15
br i1 %16, label %17, label %22
17: ; preds = %7, %2
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %1
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %7
%23 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, 0
%31 = select i1 %30, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0)
%32 = select i1 %30, i32 2, i32 1
%33 = call i32 @puts(i8* dereferenceable(1) %31)
%34 = call i32 @_IO(i8 signext 107, i32 %32)
%35 = call i32 @ioctl(i32 %23, i32 %34, i32 0)
%36 = call i32 @close(i32 %23)
ret i32 0
}