#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 */
scalar_t__ readchar(int, unsigned char *, int);
int /*<<< orphan*/ usleep(int);
int write(int, unsigned char *, int);
int fujitsu_init(int fd, unsigned long *id, unsigned long *extra) {
  unsigned char cmd, data;
  cmd = 0xff;
  ;
  if (write(fd, &cmd, 1) != 1)
    return -1;
  usleep(100 * 1000);
  cmd = 0x81;
  if (write(fd, &cmd, 1) != 1)
    return -1;
  if (readchar(fd, &data, 100) || (data & 0xbf) != 0x90)
    return -1;
  if (readchar(fd, &data, 100) || data != 0x00)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<fujitsu_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0xff,-0x19(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     116d <fujitsu_init+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <fujitsu_init+0xe7>
mov    $0x186a0,%edi
call   1050 <usleep@plt>
movb   $0x81,-0x19(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     11a1 <fujitsu_init+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <fujitsu_init+0xe7>
mov    -0x8(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x64,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
jne    11d0 <fujitsu_init+0xa0>
movzbl -0x1a(%rbp),%eax
and    $0xbf,%eax
cmp    $0x90,%eax
je     11dc <fujitsu_init+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <fujitsu_init+0xe7>
mov    -0x8(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x64,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
jne    1204 <fujitsu_init+0xd4>
movzbl -0x1a(%rbp),%eax
cmp    $0x0,%eax
je     1210 <fujitsu_init+0xe0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <fujitsu_init+0xe7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fujitsu_init>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movb   $0xff,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0xffffffff,%ebp
cmp    $0x1,%eax
jne    11be <fujitsu_init+0x8e>
mov    $0x186a0,%edi
call   1050 <usleep@plt>
movb   $0x81,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    11be <fujitsu_init+0x8e>
lea    0x6(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
jne    11be <fujitsu_init+0x8e>
mov    0x6(%rsp),%al
and    $0xbf,%al
cmp    $0x90,%al
jne    11be <fujitsu_init+0x8e>
lea    0x6(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
setne  %al
cmpb   $0x0,0x6(%rsp)
setne  %cl
or     %al,%cl
movzbl %cl,%ebp
neg    %ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fujitsu_init(i32 %0, i64* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i8
%9 = alloca i8
store i32 %0, i32* %5
store i64* %1, i64** %6
store i64* %2, i64** %7
store i8 -1, i8* %8
%10 = load i32, i32* %5
%11 = call i32 @write(i32 %10, i8* %8, i32 1)
%12 = icmp ne i32 %11, 1
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %40
14: ; preds = %3
%15 = call i32 @usleep(i32 100000)
store i8 -127, i8* %8
%16 = load i32, i32* %5
%17 = call i32 @write(i32 %16, i8* %8, i32 1)
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %4
br label %40
20: ; preds = %14
%21 = load i32, i32* %5
%22 = call i64 @readchar(i32 %21, i8* %9, i32 100)
%23 = icmp ne i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = load i8, i8* %9
%26 = zext i8 %25 to i32
%27 = and i32 %26, 191
%28 = icmp ne i32 %27, 144
br i1 %28, label %29, label %30
29: ; preds = %24, %20
store i32 -1, i32* %4
br label %40
30: ; preds = %24
%31 = load i32, i32* %5
%32 = call i64 @readchar(i32 %31, i8* %9, i32 100)
%33 = icmp ne i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i8, i8* %9
%36 = zext i8 %35 to i32
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34, %30
store i32 -1, i32* %4
br label %40
39: ; preds = %34
store i32 0, i32* %4
br label %40
40: ; preds = %39, %38, %29, %19, %13
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
define i32 @fujitsu_init(i32 %0, i64* nocapture readnone %1, i64* nocapture readnone %2) {
%4 = alloca i8
%5 = alloca i8
store i8 -1, i8* %4
%6 = call i32 @write(i32 %0, i8* %4, i32 1)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %26
8: ; preds = %3
%9 = tail call i32 @usleep(i32 100000)
store i8 -127, i8* %4
%10 = call i32 @write(i32 %0, i8* %4, i32 1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %26
12: ; preds = %8
%13 = call i64 @readchar(i32 %0, i8* %5, i32 100)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %12
%16 = load i8, i8* %5
%17 = and i8 %16, -65
%18 = icmp eq i8 %17, -112
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = call i64 @readchar(i32 %0, i8* %5, i32 100)
%21 = icmp ne i64 %20, 0
%22 = load i8, i8* %5
%23 = icmp ne i8 %22, 0
%24 = select i1 %21, i1 true, i1 %23
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %19, %12, %15, %8, %3
%27 = phi i32 [ -1, %3 ], [ -1, %8 ], [ -1, %15 ], [ -1, %12 ], [ %25, %19 ]
ret i32 %27
}