#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 */
typedef int /*<<< orphan*/ data;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
scalar_t__ readchar(int, unsigned char *, int);
int write(int, unsigned char *, int);
int pm6k_init(int fd, unsigned long *id, unsigned long *extra) {
  int i = 0;
  unsigned char cmd[6] = {0xF1, 0x00, 0x00, 0x00, 0x00, 0x0E};
  unsigned char data[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd))
    return -1;
  for (i = 0; i < sizeof(data); i++)
    if (readchar(fd, &data[i], 100) < 0)
      break;
  return 0;
}


x86 assembly (clang, -O0):
<pm6k_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0xeb0(%rip),%eax
mov    %eax,-0x22(%rbp)
mov    0xeaa(%rip),%ax
mov    %ax,-0x1e(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x6,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
cltq
cmp    $0x6,%rax
je     1197 <pm6k_init+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <pm6k_init+0xbd>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x6,%rax
jae    11e6 <pm6k_init+0xb6>
mov    -0x8(%rbp),%edi
movslq -0x1c(%rbp),%rax
lea    -0x28(%rbp),%rsi
add    %rax,%rsi
mov    $0x64,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
jge    11d3 <pm6k_init+0xa3>
jmp    11e6 <pm6k_init+0xb6>
jmp    11d8 <pm6k_init+0xa8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119e <pm6k_init+0x6e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pm6k_init>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movw   $0xe00,0x14(%rsp)
movl   $0xf1,0x10(%rsp)
movw   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    $0xffffffff,%ebp
cmp    $0x6,%eax
jne    11e4 <pm6k_init+0xc4>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
xor    %ebp,%ebp
test   %rax,%rax
js     11e4 <pm6k_init+0xc4>
lea    0x9(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
js     11e4 <pm6k_init+0xc4>
lea    0xa(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
js     11e4 <pm6k_init+0xc4>
lea    0xb(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
js     11e4 <pm6k_init+0xc4>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
js     11e4 <pm6k_init+0xc4>
lea    0xd(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.pm6k_init.cmd = [6 x i8] c"\F1\00\00\00\00\0E"
define i32 @pm6k_init(i32 %0, i64* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i32
%9 = alloca [6 x i8]
%10 = alloca [6 x i8]
store i32 %0, i32* %5
store i64* %1, i64** %6
store i64* %2, i64** %7
store i32 0, i32* %8
%11 = bitcast [6 x i8]* %9 to i8*
%12 = bitcast [6 x i8]* %10 to i8*
%13 = load i32, i32* %5
%14 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%15 = call i32 @write(i32 %13, i8* %14, i32 6)
%16 = sext i32 %15 to i64
%17 = icmp ne i64 %16, 6
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %37
19: ; preds = %3
store i32 0, i32* %8
br label %20
20: ; preds = %33, %19
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = icmp ult i64 %22, 6
br i1 %23, label %24, label %36
24: ; preds = %20
%25 = load i32, i32* %5
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [6 x i8], [6 x i8]* %10, i64 0, i64 %27
%29 = call i64 @readchar(i32 %25, i8* %28, i32 100)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
br label %36
32: ; preds = %24
br label %33
33: ; preds = %32
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %20
36: ; preds = %31, %20
store i32 0, i32* %4
br label %37
37: ; preds = %36, %18
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@__const.pm6k_init.cmd = [6 x i8] c"\F1\00\00\00\00\0E"
define i32 @pm6k_init(i32 %0, i64* nocapture readnone %1, i64* nocapture readnone %2) {
%4 = alloca [6 x i8]
%5 = alloca [6 x i8]
%6 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%8 = call i32 @write(i32 %0, i8* %6, i32 6)
%9 = icmp eq i32 %8, 6
br i1 %9, label %10, label %32
10: ; preds = %3
%11 = call i64 @readchar(i32 %0, i8* %7, i32 100)
%12 = icmp slt i64 %11, 0
br i1 %12, label %32, label %13
13: ; preds = %10
%14 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 1
%15 = call i64 @readchar(i32 %0, i8* %14, i32 100)
%16 = icmp slt i64 %15, 0
br i1 %16, label %32, label %17
17: ; preds = %13
%18 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 2
%19 = call i64 @readchar(i32 %0, i8* %18, i32 100)
%20 = icmp slt i64 %19, 0
br i1 %20, label %32, label %21
21: ; preds = %17
%22 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 3
%23 = call i64 @readchar(i32 %0, i8* %22, i32 100)
%24 = icmp slt i64 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %21
%26 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 4
%27 = call i64 @readchar(i32 %0, i8* %26, i32 100)
%28 = icmp slt i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 5
%31 = call i64 @readchar(i32 %0, i8* %30, i32 100)
br label %32
32: ; preds = %29, %10, %13, %17, %21, %25, %3
%33 = phi i32 [ -1, %3 ], [ 0, %29 ], [ 0, %25 ], [ 0, %21 ], [ 0, %17 ], [ 0, %13 ], [ 0, %10 ]
ret i32 %33
}