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


x86 assembly (clang, -O0):
<programmer_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movb   $0x2,-0xa(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl -0xa(%rbp),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
je     11ae <programmer_echo+0x7e>
mov    -0xa(%rbp),%al
lea    0xe6a(%rip),%rdi
movzbl %al,%esi
movsbl -0x9(%rbp),%edx
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <programmer_echo+0x85>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<programmer_echo>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    %sil,0xf(%rsp)
movb   $0x2,0xe(%rsp)
xor    %ebx,%ebx
lea    0xe(%rsp),%r14
mov    %r14,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl 0xe(%rsp),%eax
movsbl 0xf(%rsp),%ecx
cmp    %ecx,%eax
je     11a1 <programmer_echo+0x71>
movzbl %al,%esi
movsbl %cl,%edx
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [27 x i8] c"DEBUG: Bad echo: %c != %c\0A\00"
define i32 @programmer_echo(i32 %0, i8 signext %1) {
%3 = alloca i8
%4 = alloca i8
store i8 %1, i8* %3
store i8 2, i8* %4
%5 = call i32 (i32, ...) @write(i32 %0, i8* %4, i32 1)
%6 = call i32 (i32, ...) @write(i32 %0, i8* %3, i32 1)
%7 = call i32 @read(i32 %0, i8* %4, i32 1)
%8 = load i8, i8* %4
%9 = zext i8 %8 to i32
%10 = load i8, i8* %3
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %9, %11
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8 zeroext %8, i8 signext %10)
br label %15
15: ; preds = %2, %13
%16 = phi i32 [ -1, %13 ], [ 0, %2 ]
ret i32 %16
}