#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 *, int);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int write(int, char *, int);
void ps3_keyup(int ps3_ir) {
  int i, res;
  char buf[16];
  memset(buf, 0x0, sizeof(buf));
  buf[0] = 0x00;
  buf[1] = 0x00;
  buf[2] = 0xA3;
  buf[3] = 0x00;
  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");
  }
  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);
  }
  return;
}


x86 assembly (clang, -O0):
<ps3_keyup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
movb   $0x0,-0x20(%rbp)
movb   $0x0,-0x1f(%rbp)
movb   $0xa3,-0x1e(%rbp)
movb   $0x0,-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,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e3 <ps3_keyup+0x83>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    0xe33(%rip),%rdi
call   1050 <printf@plt>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
jmp    11f2 <ps3_keyup+0x92>
mov    -0xc(%rbp),%esi
lea    0xe24(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1221 <ps3_keyup+0xc1>
lea    0xe12(%rip),%rdi
call   1080 <perror@plt>
jmp    1272 <ps3_keyup+0x112>
mov    -0xc(%rbp),%esi
lea    0xe03(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1266 <ps3_keyup+0x106>
movslq -0x8(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%esi
lea    0xdf3(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1237 <ps3_keyup+0xd7>
lea    0xd9c(%rip),%rdi
call   1030 <puts@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,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12da <ps3_keyup+0x17a>
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
lea    0xd3c(%rip),%rdi
call   1050 <printf@plt>
lea    0xd3b(%rip),%rdi
call   1080 <perror@plt>
jmp    12e9 <ps3_keyup+0x189>
mov    -0xc(%rbp),%esi
lea    0xd2d(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ps3_keyup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
movw   $0x0,(%rsp)
movb   $0xa3,0x2(%rsp)
movl   $0x0,0x3(%rsp)
movw   $0x0,0x7(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x9,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1261 <ps3_keyup+0x101>
lea    0xe59(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x10,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1287 <ps3_keyup+0x127>
mov    %eax,%ebp
lea    0xe4e(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
test   %ebp,%ebp
je     1215 <ps3_keyup+0xb5>
mov    %ebp,%ebx
xor    %ebp,%ebp
lea    0xe50(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
movsbl (%rsp,%rbp,1),%esi
mov    %r15,%rdi
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1200 <ps3_keyup+0xa0>
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
movabs $0x99665aa55500aa00,%rax
5a 66 99
mov    %rax,(%rsp)
movb   $0x69,0x8(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x9,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1295 <ps3_keyup+0x135>
lea    0xdc2(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
lea    0xd8f(%rip),%rdi
call   1050 <printf@plt>
lea    0xd8e(%rip),%rdi
call   1080 <perror@plt>
jmp    11bf <ps3_keyup+0x5f>
lea    0xd9b(%rip),%rdi
call   1080 <perror@plt>
jmp    1221 <ps3_keyup+0xc1>
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
lea    0xd5b(%rip),%rdi
call   1050 <printf@plt>
lea    0xd5a(%rip),%rdi
call   1080 <perror@plt>
jmp    1256 <ps3_keyup+0xf6>

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_keyup(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [16 x i8]
store i32 %0, i32* %2
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 16)
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
store i8 0, i8* %8
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 1
store i8 0, i8* %9
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 2
store i8 -93, i8* %10
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 3
store i8 0, i8* %11
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 4
store i8 0, i8* %12
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 5
store i8 0, i8* %13
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 6
store i8 0, i8* %14
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 7
store i8 0, i8* %15
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 8
store i8 0, i8* %16
%17 = load i32, i32* %2
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%19 = call i32 @write(i32 %17, i8* %18, i32 9)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %1
%23 = load i32, i32* @errno
%24 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %29
26: ; preds = %1
%27 = load i32, i32* %4
%28 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %26, %22
%30 = load i32, i32* %2
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 16)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %56
37: ; preds = %29
%38 = load i32, i32* %4
%39 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %38)
store i32 0, i32* %3
br label %40
40: ; preds = %51, %37
%41 = load i32, i32* %3
%42 = load i32, i32* %4
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %54
44: ; preds = %40
%45 = load i32, i32* %3
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %44
%52 = load i32, i32* %3
%53 = add nsw i32 %52, 1
store i32 %53, i32* %3
br label %40
54: ; preds = %40
%55 = call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %56
56: ; preds = %54, %35
%57 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
store i8 0, i8* %57
%58 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 1
store i8 -86, i8* %58
%59 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 2
store i8 0, i8* %59
%60 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 3
store i8 85, i8* %60
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 4
store i8 -91, i8* %61
%62 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 5
store i8 90, i8* %62
%63 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 6
store i8 102, i8* %63
%64 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 7
store i8 -103, i8* %64
%65 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 8
store i8 105, i8* %65
%66 = load i32, i32* %2
%67 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%68 = call i32 @write(i32 %66, i8* %67, i32 9)
store i32 %68, i32* %4
%69 = load i32, i32* %4
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %56
%72 = load i32, i32* @errno
%73 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %72)
%74 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %78
75: ; preds = %56
%76 = load i32, i32* %4
%77 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %76)
br label %78
78: ; preds = %75, %71
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_keyup(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 16)
store i8 0, i8* %3
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 1
store i8 0, i8* %5
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 2
store i8 -93, i8* %6
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 3
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 4
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 5
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 6
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 7
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 8
%13 = call i32 @write(i32 %0, i8* %3, i32 9)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %21
19: ; preds = %1
%20 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %21
21: ; preds = %19, %15
%22 = call i32 @read(i32 %0, i8* %3, i32 16)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %41
26: ; preds = %21
%27 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%28 = icmp eq i32 %22, 0
br i1 %28, label %39, label %29
29: ; preds = %26
%30 = zext i32 %22 to i64
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ 0, %29 ], [ %37, %31 ]
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = add nuw nsw i64 %32, 1
%38 = icmp eq i64 %37, %30
br i1 %38, label %39, label %31
39: ; preds = %31, %26
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %39, %24
store i8 0, i8* %3
store i8 -86, i8* %5
store i8 0, i8* %6
store i8 85, i8* %7
store i8 -91, i8* %8
store i8 90, i8* %9
store i8 102, i8* %10
store i8 -103, i8* %11
store i8 105, i8* %12
%42 = call i32 @write(i32 %0, i8* %3, i32 9)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* @errno
%46 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %50
48: ; preds = %41
%49 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %42)
br label %50
50: ; preds = %48, %44
ret void
}