#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);
scalar_t__ read(int, unsigned char *, int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int programmer_chip_in_socket(int port) {
  unsigned char command;
  unsigned char receive = 'W';
  command = 18;
  write(port, &command, 1);
  read(port, &receive, 1);
  if (receive != 'A') {
    printf("Got %c\n", receive);
    return -1;
  }
  if (read(port, &receive, 1) == 0) {
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<programmer_chip_in_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x57,-0xa(%rbp)
movb   $0x12,-0x9(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl -0xa(%rbp),%eax
cmp    $0x41,%eax
je     118e <programmer_chip_in_socket+0x5e>
lea    0xe87(%rip),%rdi
movzbl -0xa(%rbp),%esi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <programmer_chip_in_socket+0x8c>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jne    11b5 <programmer_chip_in_socket+0x85>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11bc <programmer_chip_in_socket+0x8c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<programmer_chip_in_socket>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movb   $0x57,0xe(%rsp)
movb   $0x12,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
mov    0xe(%rsp),%al
cmp    $0x41,%al
jne    118a <programmer_chip_in_socket+0x5a>
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
lea    (%rcx,%rcx,1),%eax
add    $0xfffffffe,%eax
jmp    119e <programmer_chip_in_socket+0x6e>
movzbl %al,%esi
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c"Got %c\0A\00"
define i32 @programmer_chip_in_socket(i32 %0) {
%2 = alloca i8
%3 = alloca i8
store i8 87, i8* %3
store i8 18, i8* %2
%4 = call i32 @write(i32 %0, i8* %2, i32 1)
%5 = call i64 @read(i32 %0, i8* %3, i32 1)
%6 = load i8, i8* %3
%7 = icmp eq i8 %6, 65
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8 zeroext %6)
br label %14
10: ; preds = %1
%11 = call i64 @read(i32 %0, i8* %3, i32 1)
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i32 -2, i32 0
br label %14
14: ; preds = %10, %8
%15 = phi i32 [ -1, %8 ], [ %13, %10 ]
ret i32 %15
}