#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*/ printf(char *, int);
int read_byte(int);
scalar_t__ write_byte(int, int);
int serio_reset(int file) {
  int received;
  int device_id;
  if (write_byte(file, 0xff) < 0) {
    return -1;
  }
  do {
    received = read_byte(file);
    if (received < 0) {
      return -1;
    }
  } while (received != 0xaa);
  device_id = read_byte(file);
  if (device_id < 0) {
    return -1;
  }
  printf("reset, device id = %02x\n", device_id);
  return 0;
}


x86 assembly (clang, -O0):
<serio_reset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    $0xff,%esi
call   1050 <write_byte@plt>
cmp    $0x0,%rax
jge    115e <serio_reset+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <serio_reset+0x9d>
jmp    1163 <serio_reset+0x33>
mov    -0x8(%rbp),%edi
call   1030 <read_byte@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1184 <serio_reset+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <serio_reset+0x9d>
jmp    1189 <serio_reset+0x59>
cmpl   $0xaa,-0xc(%rbp)
jne    1163 <serio_reset+0x33>
mov    -0x8(%rbp),%edi
call   1030 <read_byte@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11b7 <serio_reset+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <serio_reset+0x9d>
mov    -0x10(%rbp),%esi
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serio_reset>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0xff,%esi
call   1050 <write_byte@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     117d <serio_reset+0x4d>
nopl   0x0(%rax)
mov    %ebp,%edi
call   1030 <read_byte@plt>
test   %eax,%eax
js     117d <serio_reset+0x4d>
cmp    $0xaa,%eax
jne    1150 <serio_reset+0x20>
mov    %ebp,%edi
call   1030 <read_byte@plt>
test   %eax,%eax
js     117d <serio_reset+0x4d>
lea    0xe8c(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"reset, device id = %02x\0A\00"
define i32 @serio_reset(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i64 @write_byte(i32 %6, i32 255)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %30
10: ; preds = %1
br label %11
11: ; preds = %18, %10
%12 = load i32, i32* %3
%13 = call i32 @read_byte(i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 -1, i32* %2
br label %30
17: ; preds = %11
br label %18
18: ; preds = %17
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 170
br i1 %20, label %11, label %21
21: ; preds = %18
%22 = load i32, i32* %3
%23 = call i32 @read_byte(i32 %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 -1, i32* %2
br label %30
27: ; preds = %21
%28 = load i32, i32* %5
%29 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %28)
store i32 0, i32* %2
br label %30
30: ; preds = %27, %26, %16, %9
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [25 x i8] c"reset, device id = %02x\0A\00"
define i32 @serio_reset(i32 %0) {
%2 = tail call i64 @write_byte(i32 %0, i32 255)
%3 = icmp slt i64 %2, 0
br i1 %3, label %14, label %4
4: ; preds = %1, %7
%5 = tail call i32 @read_byte(i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %4
%8 = icmp eq i32 %5, 170
br i1 %8, label %9, label %4
9: ; preds = %7
%10 = tail call i32 @read_byte(i32 %0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %14
14: ; preds = %4, %9, %1, %12
%15 = phi i32 [ 0, %12 ], [ -1, %1 ], [ -1, %9 ], [ -1, %4 ]
ret i32 %15
}