#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*/ panic(char *);
int /*<<< orphan*/ printf(char *, ...);
int read_byte(int);
scalar_t__ write_byte(int, int);
void send_status_request(int file) {
  int i;
  int received;
  if (write_byte(file, 0xe9) < 0) {
    panic("write_byte failed.");
  }
  if (read_byte(file) != 0xfa) {
    panic("ack missing.");
  }
  printf("status:");
  for (i = 0; i < 3; i++) {
    received = read_byte(file);
    if (received < 0) {
      panic("read_byte failed.");
    }
    printf(" %02x", received);
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<send_status_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    $0xe9,%esi
call   1060 <write_byte@plt>
cmp    $0x0,%rax
jge    116e <send_status_request+0x2e>
lea    0xe97(%rip),%rdi
call   1050 <panic@plt>
mov    -0x4(%rbp),%edi
call   1030 <read_byte@plt>
cmp    $0xfa,%eax
je     118d <send_status_request+0x4d>
lea    0xe8b(%rip),%rdi
call   1050 <panic@plt>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ec <send_status_request+0xac>
mov    -0x4(%rbp),%edi
call   1030 <read_byte@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11cd <send_status_request+0x8d>
lea    0xe60(%rip),%rdi
call   1050 <panic@plt>
mov    -0xc(%rbp),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a2 <send_status_request+0x62>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_status_request>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0xe9,%esi
call   1070 <write_byte@plt>
test   %rax,%rax
jns    1170 <send_status_request+0x20>
lea    0xe95(%rip),%rdi
call   1060 <panic@plt>
mov    %ebx,%edi
call   1040 <read_byte@plt>
cmp    $0xfa,%eax
je     118a <send_status_request+0x3a>
lea    0xe8e(%rip),%rdi
call   1060 <panic@plt>
lea    0xe8f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1040 <read_byte@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11b1 <send_status_request+0x61>
lea    0xe7c(%rip),%rdi
call   1060 <panic@plt>
lea    0xe82(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1040 <read_byte@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11da <send_status_request+0x8a>
lea    0xe53(%rip),%rdi
call   1060 <panic@plt>
lea    0xe59(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1040 <read_byte@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1203 <send_status_request+0xb3>
lea    0xe2a(%rip),%rdi
call   1060 <panic@plt>
lea    0xe30(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"write_byte failed.\00"
@.str.1 = [13 x i8] c"ack missing.\00"
@.str.2 = [8 x i8] c"status:\00"
@.str.3 = [18 x i8] c"read_byte failed.\00"
@.str.4 = [6 x i8] c" %02x\00"
@.str.5 = [2 x i8] c"\0A\00"
define void @send_status_request(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i64 @write_byte(i32 %5, i32 233)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = load i32, i32* %2
%12 = call i32 @read_byte(i32 %11)
%13 = icmp ne i32 %12, 250
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @panic(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %18
18: ; preds = %31, %16
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = load i32, i32* %2
%23 = call i32 @read_byte(i32 %22)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = load i32, i32* %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %28
%32 = load i32, i32* %3
%33 = add nsw i32 %32, 1
store i32 %33, i32* %3
br label %18
34: ; preds = %18
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"write_byte failed.\00"
@.str.1 = [13 x i8] c"ack missing.\00"
@.str.2 = [8 x i8] c"status:\00"
@.str.3 = [18 x i8] c"read_byte failed.\00"
@.str.4 = [6 x i8] c" %02x\00"
define void @send_status_request(i32 %0) {
%2 = tail call i64 @write_byte(i32 %0, i32 233)
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @panic(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i32 @read_byte(i32 %0)
%8 = icmp eq i32 %7, 250
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @panic(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @read_byte(i32 %0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%19 = tail call i32 @read_byte(i32 %0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%25 = tail call i32 @read_byte(i32 %0)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = tail call i32 @panic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%31 = tail call i32 @putchar(i32 10)
ret void
}