#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 errno;
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int write(int, char *, int);
void ps3_keydown(int ps3_ir, char command) {
  int i, res;
  char buf[16];
  memset(buf, 0x0, sizeof(buf));
  buf[0] = 0x00;
  buf[1] = 0xAA;
  buf[2] = 0x00;
  buf[3] = 0x55;
  buf[4] = 0xA5;
  buf[5] = 0x5A;
  buf[6] = 0x66;
  buf[7] = 0x99;
  buf[8] = 0x69;
  res = write(ps3_ir, buf, 9);
  if (res < 0) {
    printf("Error: %d\n", errno);
    perror("write");
  } else {
    printf("write() wrote %d bytes\n", res);
  }
  buf[0] = 0x00;
  buf[1] = 0x00;
  buf[2] = 0xA2;
  buf[3] = command;
  buf[4] = 0x00;
  buf[5] = 0x00;
  buf[6] = 0x00;
  buf[7] = 0x00;
  buf[8] = 0x00;
  res = write(ps3_ir, buf, 9);
  if (res < 0) {
    printf("Error: %d\n", errno);
    perror("write");
  } else {
    printf("write() wrote %d bytes\n", res);
  }
  res = read(ps3_ir, buf, 16);
  if (res < 0) {
    perror("read");
  } else {
    printf("read() read %d bytes:\n\t", res);
    for (i = 0; i < res; i++)
      printf("%hhx ", buf[i]);
    puts("\n");
  }
  return;
}


x86 assembly (clang, -O0):
<ps3_keydown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
movb   $0x0,-0x20(%rbp)
movb   $0xaa,-0x1f(%rbp)
movb   $0x0,-0x1e(%rbp)
movb   $0x55,-0x1d(%rbp)
movb   $0xa5,-0x1c(%rbp)
movb   $0x5a,-0x1b(%rbp)
movb   $0x66,-0x1a(%rbp)
movb   $0x99,-0x19(%rbp)
movb   $0x69,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11ec <ps3_keydown+0x8c>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
lea    0xe2d(%rip),%rdi
movsbl %al,%esi
call   1050 <printf@plt>
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
jmp    11fe <ps3_keydown+0x9e>
mov    -0x10(%rbp),%eax
lea    0xe1b(%rip),%rdi
movsbl %al,%esi
call   1050 <printf@plt>
movb   $0x0,-0x20(%rbp)
movb   $0x0,-0x1f(%rbp)
movb   $0xa2,-0x1e(%rbp)
mov    -0x5(%rbp),%al
mov    %al,-0x1d(%rbp)
movb   $0x0,-0x1c(%rbp)
movb   $0x0,-0x1b(%rbp)
movb   $0x0,-0x1a(%rbp)
movb   $0x0,-0x19(%rbp)
movb   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    126b <ps3_keydown+0x10b>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
lea    0xdae(%rip),%rdi
movsbl %al,%esi
call   1050 <printf@plt>
lea    0xdaa(%rip),%rdi
call   1080 <perror@plt>
jmp    127d <ps3_keydown+0x11d>
mov    -0x10(%rbp),%eax
lea    0xd9c(%rip),%rdi
movsbl %al,%esi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12ac <ps3_keydown+0x14c>
lea    0xd87(%rip),%rdi
call   1080 <perror@plt>
jmp    1300 <ps3_keydown+0x1a0>
mov    -0x10(%rbp),%eax
lea    0xd78(%rip),%rdi
movsbl %al,%esi
call   1050 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12f4 <ps3_keydown+0x194>
movslq -0xc(%rbp),%rax
lea    0xd6a(%rip),%rdi
movsbl -0x20(%rbp,%rax,1),%esi
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12c5 <ps3_keydown+0x165>
lea    0xd0e(%rip),%rdi
call   1030 <puts@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ps3_keydown>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
movabs $0x99665aa55500aa00,%rax
5a 66 99
mov    %rax,(%rsp)
movb   $0x69,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x9,%edx
call   1040 <write@plt>
test   %eax,%eax
js     125a <ps3_keydown+0xfa>
movsbl %al,%esi
lea    0xe5e(%rip),%rdi
call   1050 <printf@plt>
movw   $0x0,(%rsp)
movb   $0xa2,0x2(%rsp)
mov    %r14b,0x3(%rsp)
movb   $0x0,0x8(%rbp)
movl   $0x0,0x4(%rbp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x9,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1281 <ps3_keydown+0x121>
movsbl %al,%esi
lea    0xe1d(%rip),%rdi
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1070 <read@plt>
test   %eax,%eax
js     12a8 <ps3_keydown+0x148>
mov    %eax,%ebx
movsbl %bl,%esi
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
test   %ebx,%ebx
je     1245 <ps3_keydown+0xe5>
mov    %ebx,%ebp
xor    %ebx,%ebx
lea    0xe16(%rip),%r14
movsbl (%rsp,%rbx,1),%esi
mov    %r14,%rdi
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1230 <ps3_keydown+0xd0>
lea    0xdbd(%rip),%rdi
call   1030 <puts@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d7f(%rip),%rax
movsbl (%rax),%esi
lea    0xd95(%rip),%rdi
call   1050 <printf@plt>
lea    0xd94(%rip),%rdi
call   1080 <perror@plt>
jmp    11b8 <ps3_keydown+0x58>
mov    0x2d58(%rip),%rax
movsbl (%rax),%esi
lea    0xd6e(%rip),%rdi
call   1050 <printf@plt>
lea    0xd6d(%rip),%rdi
call   1080 <perror@plt>
jmp    11f9 <ps3_keydown+0x99>
lea    0xd7a(%rip),%rdi
call   1080 <perror@plt>
jmp    1251 <ps3_keydown+0xf1>

LLVM IR (-O0):
@.str = [11 x i8] c"Error: %d\0A\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [24 x i8] c"write() wrote %d bytes\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [24 x i8] c"read() read %d bytes:\0A\09\00"
@.str.5 = [6 x i8] c"%hhx \00"
@.str.6 = [2 x i8] c"\0A\00"
define void @ps3_keydown(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca [16 x i8]
store i32 %0, i32* %3
store i8 %1, i8* %4
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 16)
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
store i8 0, i8* %10
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 1
store i8 -86, i8* %11
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 2
store i8 0, i8* %12
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 3
store i8 85, i8* %13
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 4
store i8 -91, i8* %14
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 5
store i8 90, i8* %15
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 6
store i8 102, i8* %16
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 7
store i8 -103, i8* %17
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 8
store i8 105, i8* %18
%19 = load i32, i32* %3
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%21 = call i32 @write(i32 %19, i8* %20, i32 9)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %2
%25 = load i32, i32* @errno
%26 = trunc i32 %25 to i8
%27 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %26)
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %33
29: ; preds = %2
%30 = load i32, i32* %6
%31 = trunc i32 %30 to i8
%32 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8 signext %31)
br label %33
33: ; preds = %29, %24
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
store i8 0, i8* %34
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 1
store i8 0, i8* %35
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 2
store i8 -94, i8* %36
%37 = load i8, i8* %4
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 3
store i8 %37, i8* %38
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 4
store i8 0, i8* %39
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 5
store i8 0, i8* %40
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 6
store i8 0, i8* %41
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 7
store i8 0, i8* %42
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 8
store i8 0, i8* %43
%44 = load i32, i32* %3
%45 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%46 = call i32 @write(i32 %44, i8* %45, i32 9)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %33
%50 = load i32, i32* @errno
%51 = trunc i32 %50 to i8
%52 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %51)
%53 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %58
54: ; preds = %33
%55 = load i32, i32* %6
%56 = trunc i32 %55 to i8
%57 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8 signext %56)
br label %58
58: ; preds = %54, %49
%59 = load i32, i32* %3
%60 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%61 = call i32 @read(i32 %59, i8* %60, i32 16)
store i32 %61, i32* %6
%62 = load i32, i32* %6
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %58
%65 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %85
66: ; preds = %58
%67 = load i32, i32* %6
%68 = trunc i32 %67 to i8
%69 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8 signext %68)
store i32 0, i32* %5
br label %70
70: ; preds = %80, %66
%71 = load i32, i32* %5
%72 = load i32, i32* %6
%73 = icmp slt i32 %71, %72
br i1 %73, label %74, label %83
74: ; preds = %70
%75 = load i32, i32* %5
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 %76
%78 = load i8, i8* %77
%79 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8 signext %78)
br label %80
80: ; preds = %74
%81 = load i32, i32* %5
%82 = add nsw i32 %81, 1
store i32 %82, i32* %5
br label %70
83: ; preds = %70
%84 = call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %85
85: ; preds = %83, %64
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"Error: %d\0A\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [24 x i8] c"write() wrote %d bytes\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [24 x i8] c"read() read %d bytes:\0A\09\00"
@.str.5 = [6 x i8] c"%hhx \00"
@.str.6 = [2 x i8] c"\0A\00"
define void @ps3_keydown(i32 %0, i8 signext %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 16)
store i8 0, i8* %4
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 1
store i8 -86, i8* %6
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 2
store i8 0, i8* %7
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 3
store i8 85, i8* %8
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 4
store i8 -91, i8* %9
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 5
store i8 90, i8* %10
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 6
store i8 102, i8* %11
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 7
store i8 -103, i8* %12
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 8
store i8 105, i8* %13
%14 = call i32 @write(i32 %0, i8* %4, i32 9)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = trunc i32 %17 to i8
%19 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %18)
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %24
21: ; preds = %2
%22 = trunc i32 %14 to i8
%23 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8 signext %22)
br label %24
24: ; preds = %21, %16
store i8 0, i8* %4
store i8 0, i8* %6
store i8 -94, i8* %7
store i8 %1, i8* %8
%25 = call i32 @write(i32 %0, i8* %4, i32 9)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = trunc i32 %28 to i8
%30 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %29)
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %35
32: ; preds = %24
%33 = trunc i32 %25 to i8
%34 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8 signext %33)
br label %35
35: ; preds = %32, %27
%36 = call i32 @read(i32 %0, i8* %4, i32 16)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %55
40: ; preds = %35
%41 = trunc i32 %36 to i8
%42 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8 signext %41)
%43 = icmp eq i32 %36, 0
br i1 %43, label %53, label %44
44: ; preds = %40
%45 = zext i32 %36 to i64
br label %46
46: ; preds = %44, %46
%47 = phi i64 [ 0, %44 ], [ %51, %46 ]
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8 signext %49)
%51 = add nuw nsw i64 %47, 1
%52 = icmp eq i64 %51, %45
br i1 %52, label %53, label %46
53: ; preds = %46, %40
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %38
ret void
}