#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 tsc40_init(int fd, unsigned long *id, unsigned long *extra) {
  unsigned char cmd[2], data;
  unsigned int eeprom;
  cmd[0] = 0x55;
  if (write(fd, cmd, 1) != 1)
    return -1;
  usleep(15 * 1000);
  cmd[0] = 0x15;
  if (write(fd, cmd, 1) != 1)
    return -1;
  if (readchar(fd, &data, 100))
    return -1;
  eeprom = !((data & 0x80) >> 7);
  if (readchar(fd, &data, 100))
    return -1;
  cmd[0] = 0x05;
  cmd[1] = 0x45;
  if (write(fd, cmd, 2) != 2)
    return -1;
  if (readchar(fd, &data, 100))
    return -1;
  if ((data == 0x15) && (eeprom == 1)) {
    if (readchar(fd, &data, 100))
      return -1;
    switch (data) {
    case 0x02:
    case 0x04:
    case 0x08:
      return -1;
      break;
    default:
      break;
    }
  }
  cmd[0] = 0x01;
  if (write(fd, cmd, 1) != 1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<tsc40_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x55,-0x1a(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     116d <tsc40_init+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <tsc40_init+0x1ec>
mov    $0x3a98,%edi
call   1050 <usleep@plt>
movb   $0x15,-0x1a(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     11a1 <tsc40_init+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <tsc40_init+0x1ec>
mov    -0x8(%rbp),%edi
lea    -0x1b(%rbp),%rsi
mov    $0x64,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
je     11c8 <tsc40_init+0x98>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <tsc40_init+0x1ec>
movzbl -0x1b(%rbp),%eax
and    $0x80,%eax
sar    $0x7,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1b(%rbp),%rsi
mov    $0x64,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
je     120b <tsc40_init+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <tsc40_init+0x1ec>
movb   $0x5,-0x1a(%rbp)
movb   $0x45,-0x19(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
je     1239 <tsc40_init+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <tsc40_init+0x1ec>
mov    -0x8(%rbp),%edi
lea    -0x1b(%rbp),%rsi
mov    $0x64,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
je     1260 <tsc40_init+0x130>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <tsc40_init+0x1ec>
movzbl -0x1b(%rbp),%eax
cmp    $0x15,%eax
jne    12eb <tsc40_init+0x1bb>
cmpl   $0x1,-0x20(%rbp)
jne    12eb <tsc40_init+0x1bb>
mov    -0x8(%rbp),%edi
lea    -0x1b(%rbp),%rsi
mov    $0x64,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
je     129e <tsc40_init+0x16e>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <tsc40_init+0x1ec>
movzbl -0x1b(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x2,%eax
je     12d5 <tsc40_init+0x1a5>
jmp    12b3 <tsc40_init+0x183>
mov    -0x24(%rbp),%eax
sub    $0x4,%eax
je     12d5 <tsc40_init+0x1a5>
jmp    12c4 <tsc40_init+0x194>
mov    -0x24(%rbp),%eax
sub    $0x8,%eax
jne    12e1 <tsc40_init+0x1b1>
jmp    12d5 <tsc40_init+0x1a5>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <tsc40_init+0x1ec>
jmp    12e6 <tsc40_init+0x1b6>
jmp    12eb <tsc40_init+0x1bb>
movb   $0x1,-0x1a(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     1315 <tsc40_init+0x1e5>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <tsc40_init+0x1ec>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tsc40_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
movb   $0x55,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0xffffffff,%ebx
cmp    $0x1,%eax
jne    1249 <tsc40_init+0x119>
mov    $0x3a98,%edi
call   1050 <usleep@plt>
movb   $0x15,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    1249 <tsc40_init+0x119>
lea    0xd(%rsp),%rsi
mov    %ebp,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
jne    1249 <tsc40_init+0x119>
mov    0xd(%rsp),%r14b
lea    0xd(%rsp),%rsi
mov    %ebp,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
jne    1249 <tsc40_init+0x119>
movw   $0x4505,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1040 <write@plt>
cmp    $0x2,%eax
jne    1249 <tsc40_init+0x119>
lea    0xd(%rsp),%rsi
mov    %ebp,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
jne    1249 <tsc40_init+0x119>
cmpb   $0x15,0xd(%rsp)
jne    1229 <tsc40_init+0xf9>
test   %r14b,%r14b
js     1229 <tsc40_init+0xf9>
lea    0xd(%rsp),%rsi
mov    %ebp,%edi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %rax,%rax
jne    1249 <tsc40_init+0x119>
movzbl 0xd(%rsp),%eax
cmp    $0x8,%rax
ja     1229 <tsc40_init+0xf9>
mov    $0x114,%ecx
bt     %rax,%rcx
jb     1249 <tsc40_init+0x119>
movb   $0x1,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
setne  %bl
neg    %ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @tsc40_init(i32 %0, i64* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca [2 x i8]
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %5
store i64* %1, i64** %6
store i64* %2, i64** %7
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
store i8 85, i8* %11
%12 = load i32, i32* %5
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%14 = call i32 @write(i32 %12, i8* %13, i32 1)
%15 = icmp ne i32 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %81
17: ; preds = %3
%18 = call i32 @usleep(i32 15000)
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
store i8 21, i8* %19
%20 = load i32, i32* %5
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%22 = call i32 @write(i32 %20, i8* %21, i32 1)
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %4
br label %81
25: ; preds = %17
%26 = load i32, i32* %5
%27 = call i64 @readchar(i32 %26, i8* %9, i32 100)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %4
br label %81
30: ; preds = %25
%31 = load i8, i8* %9
%32 = zext i8 %31 to i32
%33 = and i32 %32, 128
%34 = ashr i32 %33, 7
%35 = icmp ne i32 %34, 0
%36 = xor i1 %35, true
%37 = zext i1 %36 to i32
store i32 %37, i32* %10
%38 = load i32, i32* %5
%39 = call i64 @readchar(i32 %38, i8* %9, i32 100)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %30
store i32 -1, i32* %4
br label %81
42: ; preds = %30
%43 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
store i8 5, i8* %43
%44 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 1
store i8 69, i8* %44
%45 = load i32, i32* %5
%46 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%47 = call i32 @write(i32 %45, i8* %46, i32 2)
%48 = icmp ne i32 %47, 2
br i1 %48, label %49, label %50
49: ; preds = %42
store i32 -1, i32* %4
br label %81
50: ; preds = %42
%51 = load i32, i32* %5
%52 = call i64 @readchar(i32 %51, i8* %9, i32 100)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
store i32 -1, i32* %4
br label %81
55: ; preds = %50
%56 = load i8, i8* %9
%57 = zext i8 %56 to i32
%58 = icmp eq i32 %57, 21
br i1 %58, label %59, label %73
59: ; preds = %55
%60 = load i32, i32* %10
%61 = icmp eq i32 %60, 1
br i1 %61, label %62, label %73
62: ; preds = %59
%63 = load i32, i32* %5
%64 = call i64 @readchar(i32 %63, i8* %9, i32 100)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %62
store i32 -1, i32* %4
br label %81
67: ; preds = %62
%68 = load i8, i8* %9
%69 = zext i8 %68 to i32
switch i32 %69, label %71 [
i32 2, label %70
i32 4, label %70
i32 8, label %70
]
70: ; preds = %67, %67, %67
store i32 -1, i32* %4
br label %81
71: ; preds = %67
br label %72
72: ; preds = %71
br label %73
73: ; preds = %72, %59, %55
%74 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
store i8 1, i8* %74
%75 = load i32, i32* %5
%76 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%77 = call i32 @write(i32 %75, i8* %76, i32 1)
%78 = icmp ne i32 %77, 1
br i1 %78, label %79, label %80
79: ; preds = %73
store i32 -1, i32* %4
br label %81
80: ; preds = %73
store i32 0, i32* %4
br label %81
81: ; preds = %80, %79, %70, %66, %54, %49, %41, %29, %24, %16
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
define i32 @tsc40_init(i32 %0, i64* nocapture readnone %1, i64* nocapture readnone %2) {
%4 = alloca [2 x i8]
%5 = alloca i8
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
store i8 85, i8* %6
%7 = call i32 @write(i32 %0, i8* %6, i32 1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %41
9: ; preds = %3
%10 = tail call i32 @usleep(i32 15000)
store i8 21, i8* %6
%11 = call i32 @write(i32 %0, i8* %6, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %41
13: ; preds = %9
%14 = call i64 @readchar(i32 %0, i8* %5, i32 100)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %41
16: ; preds = %13
%17 = load i8, i8* %5
%18 = icmp sgt i8 %17, -1
%19 = call i64 @readchar(i32 %0, i8* %5, i32 100)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %41
21: ; preds = %16
store i8 5, i8* %6
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 69, i8* %22
%23 = call i32 @write(i32 %0, i8* %6, i32 2)
%24 = icmp eq i32 %23, 2
br i1 %24, label %25, label %41
25: ; preds = %21
%26 = call i64 @readchar(i32 %0, i8* %5, i32 100)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %41
28: ; preds = %25
%29 = load i8, i8* %5
%30 = icmp eq i8 %29, 21
%31 = select i1 %30, i1 %18, i1 false
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = call i64 @readchar(i32 %0, i8* %5, i32 100)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i8, i8* %5
switch i8 %36, label %37 [
i8 2, label %41
i8 4, label %41
i8 8, label %41
]
37: ; preds = %35, %28
store i8 1, i8* %6
%38 = call i32 @write(i32 %0, i8* %6, i32 1)
%39 = icmp ne i32 %38, 1
%40 = sext i1 %39 to i32
br label %41
41: ; preds = %37, %35, %35, %35, %32, %25, %21, %16, %13, %9, %3
%42 = phi i32 [ -1, %3 ], [ -1, %9 ], [ -1, %13 ], [ -1, %16 ], [ -1, %21 ], [ -1, %25 ], [ -1, %32 ], [ -1, %35 ], [ -1, %35 ], [ -1, %35 ], [ %40, %37 ]
ret i32 %42
}