#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 ece453_reg_read(char *);
int ece453_reg_write(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  int button_vals = 0;
  int curr_led = 0;
  int status = 0;
  bool changed = false;
  status = ece453_reg_read("/sys/kernel/ece453/device_id");
  if (status == -1) {
    printf("Error Reading DEV_ID\n\r");
    return -1;
  }
  printf("DEV_ID: 0x%08X\n\r", status);
  status = ece453_reg_write("/sys/kernel/ece453/gpio_out", 1);
  if (status == -1) {
    printf("Error Writing GPIO OUT\n\r");
    return -1;
  }
  printf("Press KEY[0] to move right or KEY[1] to move left.\n\r");
  printf("Press KEY[2] to exit.\n\r");
  while (true) {
    sleep(1);
    button_vals = ece453_reg_read("/sys/kernel/ece453/gpio_in");
    if (button_vals == -1) {
      printf("Error Reading GPIO IN\n\r");
      status = -1;
      break;
    }
    if ((button_vals & (1 << (10 + 2))) == 0) {
      status = 0;
      break;
    }
    if ((button_vals & (1 << (10 + 1))) == 0) {
      curr_led = (curr_led == 9) ? 0 : curr_led + 1;
      changed = true;
    } else if ((button_vals & (1 << (10 + 0))) == 0) {
      curr_led = (curr_led == 0) ? 9 : curr_led - 1;
      changed = true;
    }
    if (changed) {
      changed = false;
      status = ece453_reg_write("/sys/kernel/ece453/gpio_out", 1 << curr_led);
      if (status == -1) {
        printf("Error Writing GPIO OUT\n\r");
        break;
      }
    }
  }
  if (ece453_reg_write("/sys/kernel/ece453/gpio_out", 0) == -1) {
    printf("Error Writing GPIO OUT\n\r");
    status = -1;
  }
  return status;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
movl   $0x0,-0x20(%rbp)
lea    0xe87(%rip),%rdi
call   1040 <ece453_reg_read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a5 <main+0x65>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <main+0x22a>
mov    -0x1c(%rbp),%esi
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe88(%rip),%rdi
mov    $0x1,%esi
call   1050 <ece453_reg_write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ee <main+0xae>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <main+0x22a>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xeac(%rip),%rdi
call   1040 <ece453_reg_read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1247 <main+0x107>
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1338 <main+0x1f8>
mov    -0x14(%rbp),%eax
and    $0x1000,%eax
cmp    $0x0,%eax
jne    1264 <main+0x124>
movl   $0x0,-0x1c(%rbp)
jmp    1338 <main+0x1f8>
mov    -0x14(%rbp),%eax
and    $0x800,%eax
cmp    $0x0,%eax
jne    12a4 <main+0x164>
cmpl   $0x9,-0x18(%rbp)
jne    1289 <main+0x149>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    1292 <main+0x152>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    12e7 <main+0x1a7>
mov    -0x14(%rbp),%eax
and    $0x400,%eax
cmp    $0x0,%eax
jne    12e2 <main+0x1a2>
cmpl   $0x0,-0x18(%rbp)
jne    12cc <main+0x18c>
mov    $0x9,%eax
mov    %eax,-0x28(%rbp)
jmp    12d5 <main+0x195>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    12e7 <main+0x1a7>
cmpl   $0x0,-0x20(%rbp)
je     1333 <main+0x1f3>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
lea    0xd3c(%rip),%rdi
call   1050 <ece453_reg_write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    132e <main+0x1ee>
lea    0xd3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1338 <main+0x1f8>
jmp    1333 <main+0x1f3>
jmp    120a <main+0xca>
lea    0xd06(%rip),%rdi
xor    %esi,%esi
call   1050 <ece453_reg_write@plt>
cmp    $0xffffffff,%eax
jne    1364 <main+0x224>
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeae(%rip),%rdi
call   1040 <ece453_reg_read@plt>
cmp    $0xffffffff,%eax
je     1255 <main+0x115>
lea    0xecd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xece(%rip),%rdi
mov    $0x1,%esi
call   1050 <ece453_reg_write@plt>
lea    0xed9(%rip),%r15
cmp    $0xffffffff,%eax
je     128b <main+0x14b>
lea    0xee2(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf07(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xf07(%rip),%rdi
call   1040 <ece453_reg_read@plt>
lea    0xf16(%rip),%r14
cmp    $0xffffffff,%eax
je     1269 <main+0x129>
lea    0xe69(%rip),%r12
lea    0xee4(%rip),%r13
jmp    1232 <main+0xf2>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
mov    $0x1,%ebp
mov    $0x9,%ecx
add    %ebx,%ebp
cmp    %ecx,%ebx
cmove  %eax,%ebp
mov    $0x1,%esi
mov    %ebp,%ecx
shl    %cl,%esi
mov    %r12,%rdi
call   1050 <ece453_reg_write@plt>
mov    %ebp,%ebx
cmp    $0xffffffff,%eax
je     1262 <main+0x122>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %r13,%rdi
call   1040 <ece453_reg_read@plt>
cmp    $0xffffffff,%eax
je     1269 <main+0x129>
test   $0x1000,%eax
je     125e <main+0x11e>
test   $0x800,%eax
je     11f0 <main+0xb0>
test   $0x400,%eax
jne    121b <main+0xdb>
mov    $0x9,%eax
mov    $0xffffffff,%ebp
xor    %ecx,%ecx
jmp    11fc <main+0xbc>
lea    0xdc1(%rip),%r15
jmp    128b <main+0x14b>
xor    %ebx,%ebx
jmp    1278 <main+0x138>
lea    0xdf8(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%ebx
lea    0xdc6(%rip),%rdi
xor    %esi,%esi
call   1050 <ece453_reg_write@plt>
cmp    $0xffffffff,%eax
jne    129a <main+0x15a>
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/sys/kernel/ece453/device_id\00"
@.str.1 = [23 x i8] c"Error Reading DEV_ID\0A\0D\00"
@.str.2 = [17 x i8] c"DEV_ID: 0x%08X\0A\0D\00"
@.str.3 = [28 x i8] c"/sys/kernel/ece453/gpio_out\00"
@.str.4 = [25 x i8] c"Error Writing GPIO OUT\0A\0D\00"
@.str.5 = [53 x i8] c"Press KEY[0] to move right or KEY[1] to move left.\0A\0D\00"
@.str.6 = [24 x i8] c"Press KEY[2] to exit.\0A\0D\00"
@.str.7 = [27 x i8] c"/sys/kernel/ece453/gpio_in\00"
@.str.8 = [24 x i8] c"Error Reading GPIO IN\0A\0D\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 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
store i32 0, i32* %9
%10 = call i32 @ece453_reg_read(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %85
15: ; preds = %2
%16 = load i32, i32* %8
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @ece453_reg_write(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 1)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %85
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %26
26: ; preds = %23, %77
%27 = call i32 @sleep(i32 1)
%28 = call i32 @ece453_reg_read(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %8
br label %78
33: ; preds = %26
%34 = load i32, i32* %6
%35 = and i32 %34, 4096
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 0, i32* %8
br label %78
38: ; preds = %33
%39 = load i32, i32* %6
%40 = and i32 %39, 2048
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, 9
br i1 %44, label %45, label %46
45: ; preds = %42
br label %49
46: ; preds = %42
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
br label %49
49: ; preds = %46, %45
%50 = phi i32 [ 0, %45 ], [ %48, %46 ]
store i32 %50, i32* %7
store i32 1, i32* %9
br label %65
51: ; preds = %38
%52 = load i32, i32* %6
%53 = and i32 %52, 1024
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %64
55: ; preds = %51
%56 = load i32, i32* %7
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %55
br label %62
59: ; preds = %55
%60 = load i32, i32* %7
%61 = sub nsw i32 %60, 1
br label %62
62: ; preds = %59, %58
%63 = phi i32 [ 9, %58 ], [ %61, %59 ]
store i32 %63, i32* %7
store i32 1, i32* %9
br label %64
64: ; preds = %62, %51
br label %65
65: ; preds = %64, %49
%66 = load i32, i32* %9
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %77
68: ; preds = %65
store i32 0, i32* %9
%69 = load i32, i32* %7
%70 = shl i32 1, %69
%71 = call i32 @ece453_reg_write(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %70)
store i32 %71, i32* %8
%72 = load i32, i32* %8
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %68
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %78
76: ; preds = %68
br label %77
77: ; preds = %76, %65
br label %26
78: ; preds = %74, %37, %31
%79 = call i32 @ece453_reg_write(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 0)
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %83
81: ; preds = %78
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %8
br label %83
83: ; preds = %81, %78
%84 = load i32, i32* %8
store i32 %84, i32* %3
br label %85
85: ; preds = %83, %21, %13
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
@.str = [29 x i8] c"/sys/kernel/ece453/device_id\00"
@.str.1 = [23 x i8] c"Error Reading DEV_ID\0A\0D\00"
@.str.2 = [17 x i8] c"DEV_ID: 0x%08X\0A\0D\00"
@.str.3 = [28 x i8] c"/sys/kernel/ece453/gpio_out\00"
@.str.4 = [25 x i8] c"Error Writing GPIO OUT\0A\0D\00"
@.str.5 = [53 x i8] c"Press KEY[0] to move right or KEY[1] to move left.\0A\0D\00"
@.str.6 = [24 x i8] c"Press KEY[2] to exit.\0A\0D\00"
@.str.7 = [27 x i8] c"/sys/kernel/ece453/gpio_in\00"
@.str.8 = [24 x i8] c"Error Reading GPIO IN\0A\0D\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @ece453_reg_read(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32 %3, -1
br i1 %4, label %48, label %5
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%7 = tail call i32 @ece453_reg_write(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %48, label %9
9: ; preds = %5
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 @ece453_reg_read(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%14 = icmp eq i32 %13, -1
br i1 %14, label %41, label %15
15: ; preds = %9, %36
%16 = phi i32 [ %39, %36 ], [ %13, %9 ]
%17 = phi i32 [ %37, %36 ], [ 0, %9 ]
%18 = and i32 %16, 4096
%19 = icmp eq i32 %18, 0
br i1 %19, label %44, label %20
20: ; preds = %15
%21 = and i32 %16, 2048
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = and i32 %16, 1024
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %23, %20
%27 = phi i32 [ 9, %20 ], [ 0, %23 ]
%28 = phi i32 [ 1, %20 ], [ -1, %23 ]
%29 = phi i32 [ 0, %20 ], [ 9, %23 ]
%30 = icmp eq i32 %17, %27
%31 = add nsw i32 %17, %28
%32 = select i1 %30, i32 %29, i32 %31
%33 = shl nuw i32 1, %32
%34 = tail call i32 @ece453_reg_write(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %41, label %36
36: ; preds = %23, %26
%37 = phi i32 [ %32, %26 ], [ %17, %23 ]
%38 = tail call i32 @sleep(i32 1)
%39 = tail call i32 @ece453_reg_read(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %15
41: ; preds = %26, %36, %9
%42 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), %9 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), %36 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), %26 ]
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %42)
br label %44
44: ; preds = %15, %41
%45 = phi i32 [ -1, %41 ], [ 0, %15 ]
%46 = tail call i32 @ece453_reg_write(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 0)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %44, %5, %2
%49 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), %5 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), %44 ]
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %49)
br label %51
51: ; preds = %48, %44
%52 = phi i32 [ %45, %44 ], [ -1, %48 ]
ret i32 %52
}