#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 read(int, unsigned char *, int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int programmer_read_chipId(int port) {
  unsigned char command;
  unsigned char buffer[256];
  int len;
  command = 13;
  write(port, &command, 1);
  read(port, buffer, 1);
  if (buffer[0] != 'C') {
    ;
    return -1;
  }
  len = 0;
  while ((len += read(port, buffer + len, 26)) != 26) {
    ;
  }
  return ((int)(buffer[1]) << 8 | buffer[0]) & 0xFFE0;
}


x86 assembly (clang, -O0):
<programmer_read_chipId>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
movb   $0xd,-0x9(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movzbl -0x110(%rbp),%eax
cmp    $0x43,%eax
je     1173 <programmer_read_chipId+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <programmer_read_chipId+0xb0>
movl   $0x0,-0x114(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x110(%rbp),%rsi
movslq -0x114(%rbp),%rax
add    %rax,%rsi
mov    $0x1a,%edx
call   1040 <read@plt>
add    -0x114(%rbp),%eax
mov    %eax,-0x114(%rbp)
cmp    $0x1a,%eax
je     11b5 <programmer_read_chipId+0x95>
jmp    117d <programmer_read_chipId+0x5d>
movzbl -0x10f(%rbp),%eax
shl    $0x8,%eax
movzbl -0x110(%rbp),%ecx
or     %ecx,%eax
and    $0xffe0,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<programmer_read_chipId>:
push   %rbp
push   %rbx
sub    $0x118,%rsp
mov    %edi,%ebp
movb   $0xd,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    $0xffffffff,%eax
cmpb   $0x43,0x10(%rsp)
jne    1190 <programmer_read_chipId+0x70>
xor    %ebx,%ebx
xchg   %ax,%ax
movslq %ebx,%rbx
lea    (%rsp,%rbx,1),%rsi
add    $0x10,%rsi
mov    %ebp,%edi
mov    $0x1a,%edx
call   1040 <read@plt>
add    %eax,%ebx
cmp    $0x1a,%ebx
jne    1160 <programmer_read_chipId+0x40>
movzbl 0x11(%rsp),%ecx
shl    $0x8,%ecx
movzbl 0x10(%rsp),%eax
and    $0xffffffe0,%eax
or     %ecx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @programmer_read_chipId(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca [256 x i8]
%6 = alloca i32
store i32 %0, i32* %3
store i8 13, i8* %4
%7 = load i32, i32* %3
%8 = call i32 @write(i32 %7, i8* %4, i32 1)
%9 = load i32, i32* %3
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 1)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = icmp ne i32 %14, 67
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %39
17: ; preds = %1
store i32 0, i32* %6
br label %18
18: ; preds = %28, %17
%19 = load i32, i32* %3
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = call i32 @read(i32 %19, i8* %23, i32 26)
%25 = load i32, i32* %6
%26 = add nsw i32 %25, %24
store i32 %26, i32* %6
%27 = icmp ne i32 %26, 26
br i1 %27, label %28, label %29
28: ; preds = %18
br label %18
29: ; preds = %18
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 1
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = shl i32 %32, 8
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = or i32 %33, %36
%38 = and i32 %37, 65504
store i32 %38, i32* %2
br label %39
39: ; preds = %29, %16
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
define i32 @programmer_read_chipId(i32 %0) {
%2 = alloca i8
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
store i8 13, i8* %2
%5 = call i32 @write(i32 %0, i8* %2, i32 1)
%6 = call i32 @read(i32 %0, i8* %4, i32 1)
%7 = load i8, i8* %4
%8 = icmp eq i8 %7, 67
br i1 %8, label %9, label %25
9: ; preds = %1, %9
%10 = phi i32 [ %14, %9 ], [ 0, %1 ]
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %11
%13 = call i32 @read(i32 %0, i8* %12, i32 26)
%14 = add nsw i32 %13, %10
%15 = icmp eq i32 %14, 26
br i1 %15, label %16, label %9
16: ; preds = %9
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 1
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = shl nuw nsw i32 %19, 8
%21 = load i8, i8* %4
%22 = and i8 %21, -32
%23 = zext i8 %22 to i32
%24 = or i32 %20, %23
br label %25
25: ; preds = %1, %16
%26 = phi i32 [ %24, %16 ], [ -1, %1 ]
ret i32 %26
}