#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 *, unsigned char);
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int programmer_program_mode(int port) {
  unsigned char receive;
  unsigned char command = 'X';
  command = 'P';
  write(port, &command, 1);
  read(port, &receive, 1);
  if (receive != 'P') {
    printf("DEBUG: Bad return: %c\n", receive);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<programmer_program_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x58,-0xa(%rbp)
movb   $0x50,-0xa(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl -0x9(%rbp),%eax
cmp    $0x50,%eax
je     118e <programmer_program_mode+0x5e>
lea    0xe87(%rip),%rdi
movzbl -0x9(%rbp),%esi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <programmer_program_mode+0x65>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<programmer_program_mode>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movb   $0x50,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
mov    0xf(%rsp),%cl
xor    %eax,%eax
cmp    $0x50,%cl
je     117b <programmer_program_mode+0x4b>
movzbl %cl,%esi
lea    0xe8f(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"DEBUG: Bad return: %c\0A\00"
define i32 @programmer_program_mode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
store i32 %0, i32* %3
store i8 88, i8* %5
store i8 80, i8* %5
%6 = load i32, i32* %3
%7 = call i32 @write(i32 %6, i8* %5, i32 1)
%8 = load i32, i32* %3
%9 = call i32 @read(i32 %8, i8* %4, i32 1)
%10 = load i8, i8* %4
%11 = zext i8 %10 to i32
%12 = icmp ne i32 %11, 80
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8, i8* %4
%15 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 zeroext %14)
store i32 -1, i32* %2
br label %17
16: ; preds = %1
store i32 0, i32* %2
br label %17
17: ; preds = %16, %13
%18 = load i32, i32* %2
ret i32 %18
}

LLVM IR (-O2):
@.str = [23 x i8] c"DEBUG: Bad return: %c\0A\00"
define i32 @programmer_program_mode(i32 %0) {
%2 = alloca i8
%3 = alloca i8
store i8 80, i8* %3
%4 = call i32 @write(i32 %0, i8* %3, i32 1)
%5 = call i32 @read(i32 %0, i8* %2, i32 1)
%6 = load i8, i8* %2
%7 = icmp eq i8 %6, 80
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 zeroext %6)
br label %10
10: ; preds = %1, %8
%11 = phi i32 [ -1, %8 ], [ 0, %1 ]
ret i32 %11
}