#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ioctl(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned char);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int main(void) {
  int fd;
  int i, j;
  unsigned char buf[2];
  fd = open("/dev/bct3288a", O_RDWR);
  if (fd < 0) {
    perror("dev bct3288a open:");
    return 1;
  }
  while (1) {
    buf[1] = 0;
    ioctl(fd, 0x102, 0);
    for (i = 4; i < 16; i++) {
      buf[0] = i;
      if (i < 10)
        buf[1] = buf[1] + 1;
      else
        buf[1] = buf[1] + 4;
      write(fd, buf, sizeof(buf));
      printf("buf[1]=%d\n", buf[1]);
      usleep(500 * 1000);
    }
    ioctl(fd, 0x101, 0);
    sleep(1);
  }
  ioctl(fd, 0x101, 0);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b8 <main+0x48>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret
jmp    11bd <main+0x4d>
movb   $0x0,-0x11(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x102,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
movl   $0x4,-0xc(%rbp)
cmpl   $0x10,-0xc(%rbp)
jge    1243 <main+0xd3>
mov    -0xc(%rbp),%eax
mov    %al,-0x12(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    1200 <main+0x90>
movzbl -0x11(%rbp),%eax
add    $0x1,%eax
mov    %al,-0x11(%rbp)
jmp    120a <main+0x9a>
movzbl -0x11(%rbp),%eax
add    $0x4,%eax
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
lea    0xdff(%rip),%rdi
movzbl -0x11(%rbp),%esi
call   1040 <printf@plt>
mov    $0x7a120,%edi
call   1090 <usleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11d7 <main+0x67>
mov    -0x8(%rbp),%edi
mov    $0x101,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11bd <main+0x4d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1236 <main+0xc6>
mov    %eax,%r12d
lea    0xe(%rsp),%r14
lea    0xe79(%rip),%r15
nopl   0x0(%rax,%rax,1)
xor    %ebp,%ebp
mov    %r12d,%edi
mov    $0x102,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    $0x4,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %bl,0xe(%rsp)
xor    %eax,%eax
cmp    $0xa,%ebx
setae  %al
lea    (%rax,%rax,2),%eax
add    $0x1,%eax
add    %al,%bpl
mov    %bpl,0xf(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
movzbl %bpl,%esi
mov    %r15,%rdi
call   1040 <printf@plt>
mov    $0x7a120,%edi
call   1090 <usleep@plt>
add    $0x1,%ebx
cmp    $0x10,%ebx
jne    11d0 <main+0x60>
mov    %r12d,%edi
mov    $0x101,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11b0 <main+0x40>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/bct3288a\00"
@O_RDWR = global i32 0
@.str.1 = [19 x i8] c"dev bct3288a open:\00"
@.str.2 = [11 x i8] c"buf[1]=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
ret i32 1
12: ; preds = %0
br label %13
13: ; preds = %12, %51
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 0, i8* %14
%15 = load i32, i32* %2
%16 = call i32 @ioctl(i32 %15, i32 258, i32 0)
store i32 4, i32* %3
br label %17
17: ; preds = %48, %13
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 16
br i1 %19, label %20, label %51
20: ; preds = %17
%21 = load i32, i32* %3
%22 = trunc i32 %21 to i8
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
store i8 %22, i8* %23
%24 = load i32, i32* %3
%25 = icmp slt i32 %24, 10
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = add nsw i32 %29, 1
%31 = trunc i32 %30 to i8
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 %31, i8* %32
br label %40
33: ; preds = %20
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = add nsw i32 %36, 4
%38 = trunc i32 %37 to i8
%39 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 %38, i8* %39
br label %40
40: ; preds = %33, %26
%41 = load i32, i32* %2
%42 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%43 = call i32 @write(i32 %41, i8* %42, i32 2)
%44 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%45 = load i8, i8* %44
%46 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %45)
%47 = call i32 @usleep(i32 500000)
br label %48
48: ; preds = %40
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
br label %17
51: ; preds = %17
%52 = load i32, i32* %2
%53 = call i32 @ioctl(i32 %52, i32 257, i32 0)
%54 = call i32 @sleep(i32 1)
br label %13
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/bct3288a\00"
@O_RDWR = global i32 0
@.str.1 = [19 x i8] c"dev bct3288a open:\00"
@.str.2 = [11 x i8] c"buf[1]=%d\0A\00"
define i32 @main() {
%1 = alloca [2 x i8]
%2 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
br label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
ret i32 1
10: ; preds = %6, %24
%11 = tail call i32 @ioctl(i32 %4, i32 258, i32 0)
br label %12
12: ; preds = %10, %12
%13 = phi i8 [ 0, %10 ], [ %18, %12 ]
%14 = phi i32 [ 4, %10 ], [ %22, %12 ]
%15 = trunc i32 %14 to i8
store i8 %15, i8* %2
%16 = icmp ult i32 %14, 10
%17 = select i1 %16, i8 1, i8 4
%18 = add i8 %13, %17
store i8 %18, i8* %7
%19 = call i32 @write(i32 %4, i8* %2, i32 2)
%20 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %18)
%21 = tail call i32 @usleep(i32 500000)
%22 = add nuw nsw i32 %14, 1
%23 = icmp eq i32 %22, 16
br i1 %23, label %24, label %12
24: ; preds = %12
%25 = tail call i32 @ioctl(i32 %4, i32 257, i32 0)
%26 = tail call i32 @sleep(i32 1)
br label %10
}