#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int ioctl(int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  int fd = 0, i = 0, ret = 0;
  fd = open("/dev/bb_led", O_RDWR);
  printf("fd=%d\r\n", fd);
  if (0 > fd) {
    perror("open /dev/bb_led");
    exit(1);
  }
  for (i = 0; i < 10; i++) {
    ret = ioctl(fd, i % 2);
    printf("ioctl ret = %d\r\n", ret);
    sleep(1);
  }
  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)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe43(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jle    11e3 <main+0x73>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jge    1233 <main+0xc3>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe03(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ea <main+0x7a>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
lea    0xe6e(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
test   %ebx,%ebx
js     12ea <main+0x17a>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <ioctl@plt>
lea    0xe68(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <ioctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd17(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/bb_led\00"
@O_RDWR = global i32 0
@.str.1 = [8 x i8] c"fd=%d\0D\0A\00"
@.str.2 = [17 x i8] c"open /dev/bb_led\00"
@.str.3 = [17 x i8] c"ioctl ret = %d\0D\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %6
%14 = icmp sgt i32 0, %13
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
store i32 0, i32* %7
br label %19
19: ; preds = %30, %18
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 10
br i1 %21, label %22, label %33
22: ; preds = %19
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = srem i32 %24, 2
%26 = call i32 @ioctl(i32 %23, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = call i32 @sleep(i32 1)
br label %30
30: ; preds = %22
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %19
33: ; preds = %19
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/bb_led\00"
@O_RDWR = global i32 0
@.str.1 = [8 x i8] c"fd=%d\0D\0A\00"
@.str.2 = [17 x i8] c"open /dev/bb_led\00"
@.str.3 = [17 x i8] c"ioctl ret = %d\0D\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %4, 0
br i1 %6, label %39, label %7
7: ; preds = %2
%8 = tail call i32 @ioctl(i32 %4, i32 0)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 @ioctl(i32 %4, i32 1)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = tail call i32 @sleep(i32 1)
%14 = tail call i32 @ioctl(i32 %4, i32 0)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = tail call i32 @sleep(i32 1)
%17 = tail call i32 @ioctl(i32 %4, i32 1)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = tail call i32 @sleep(i32 1)
%20 = tail call i32 @ioctl(i32 %4, i32 0)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 @ioctl(i32 %4, i32 1)
%24 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = tail call i32 @sleep(i32 1)
%26 = tail call i32 @ioctl(i32 %4, i32 0)
%27 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = tail call i32 @sleep(i32 1)
%29 = tail call i32 @ioctl(i32 %4, i32 1)
%30 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = tail call i32 @sleep(i32 1)
%32 = tail call i32 @ioctl(i32 %4, i32 0)
%33 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = tail call i32 @sleep(i32 1)
%35 = tail call i32 @ioctl(i32 %4, i32 1)
%36 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = tail call i32 @sleep(i32 1)
%38 = tail call i32 @close(i32 %4)
ret i32 0
39: ; preds = %2
%40 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%41 = tail call i32 @exit(i32 1)
unreachable
}