#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*/ readchar(int, unsigned char *, int);
int write(int, char **, int);
int t213_init(int fd, unsigned long *id, unsigned long *extra) {
  char cmd[] = {0x0a, 1, 'A'};
  int count = 10;
  int state = 0;
  unsigned char data;
  while (count > 0) {
    if (write(fd, &cmd, 3) != 3)
      return -1;
    while (!readchar(fd, &data, 100)) {
      switch (state) {
      case 0:
        if (data == 0x0a) {
          state = 1;
        }
        break;
      case 1:
        if (data == 1) {
          state = 2;
        } else if (data != 0x0a) {
          state = 0;
        }
        break;
      case 2:
        if (data == 'A') {
          return 0;
        } else if (data == 0x0a) {
          state = 1;
        } else {
          state = 0;
        }
        break;
      }
    }
    count--;
  }
  return -1;
}


x86 assembly (clang, -O0):
<t213_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0xec6(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xebe(%rip),%al
mov    %al,-0x19(%rbp)
movl   $0xa,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    128b <t213_init+0x16b>
mov    -0x8(%rbp),%edi
lea    -0x1b(%rbp),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
je     1185 <t213_init+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <t213_init+0x172>
jmp    118a <t213_init+0x6a>
mov    -0x8(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x64,%edx
call   1030 <readchar@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b0 <t213_init+0x90>
jmp    127d <t213_init+0x15d>
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
test   %eax,%eax
je     11e5 <t213_init+0xc5>
jmp    11c3 <t213_init+0xa3>
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
je     11fe <t213_init+0xde>
jmp    11d4 <t213_init+0xb4>
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
je     1235 <t213_init+0x115>
jmp    1278 <t213_init+0x158>
movzbl -0x25(%rbp),%eax
cmp    $0xa,%eax
jne    11f9 <t213_init+0xd9>
movl   $0x1,-0x24(%rbp)
jmp    1278 <t213_init+0x158>
movzbl -0x25(%rbp),%eax
cmp    $0x1,%eax
jne    1217 <t213_init+0xf7>
movl   $0x2,-0x24(%rbp)
jmp    1230 <t213_init+0x110>
movzbl -0x25(%rbp),%eax
cmp    $0xa,%eax
je     122b <t213_init+0x10b>
movl   $0x0,-0x24(%rbp)
jmp    1230 <t213_init+0x110>
jmp    1278 <t213_init+0x158>
movzbl -0x25(%rbp),%eax
cmp    $0x41,%eax
jne    124e <t213_init+0x12e>
movl   $0x0,-0x4(%rbp)
jmp    1292 <t213_init+0x172>
movzbl -0x25(%rbp),%eax
cmp    $0xa,%eax
jne    1267 <t213_init+0x147>
movl   $0x1,-0x24(%rbp)
jmp    126e <t213_init+0x14e>
movl   $0x0,-0x24(%rbp)
jmp    1273 <t213_init+0x153>
jmp    1278 <t213_init+0x158>
jmp    118a <t213_init+0x6a>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    1155 <t213_init+0x35>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<t213_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
movb   $0x41,0x6(%rsp)
movw   $0x10a,0x4(%rsp)
mov    $0xa,%ebp
xor    %r14d,%r14d
lea    0x3(%rsp),%r15
mov    $0x2,%r13d
mov    $0xffffffff,%r12d
jmp    116e <t213_init+0x4e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    -0x1(%rbp),%eax
cmp    $0x1,%ebp
mov    %eax,%ebp
jbe    11fb <t213_init+0xdb>
mov    %ebx,%edi
lea    0x4(%rsp),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
je     11a4 <t213_init+0x84>
jmp    11fb <t213_init+0xdb>
cs nopw 0x0(%rax,%rax,1)
movzbl 0x3(%rsp),%eax
xor    %r14d,%r14d
cmp    $0xa,%al
sete   %r14b
cmp    $0x1,%al
cmove  %r13d,%r14d
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x64,%edx
call   1030 <readchar@plt>
test   %eax,%eax
jne    1160 <t213_init+0x40>
cmp    $0x2,%r14d
je     11e0 <t213_init+0xc0>
cmp    $0x1,%r14d
je     1190 <t213_init+0x70>
test   %r14d,%r14d
jne    11a4 <t213_init+0x84>
xor    %r14d,%r14d
cmpb   $0xa,0x3(%rsp)
sete   %r14b
jmp    11a4 <t213_init+0x84>
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%r14d
movzbl 0x3(%rsp),%eax
cmp    $0xa,%al
je     11a4 <t213_init+0x84>
cmp    $0x41,%al
je     11f8 <t213_init+0xd8>
xor    %r14d,%r14d
jmp    11a4 <t213_init+0x84>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.t213_init.cmd = [3 x i8] c"\0A\01A"
define i32 @t213_init(i32 %0, i64* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca [3 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
store i32 %0, i32* %5
store i64* %1, i64** %6
store i64* %2, i64** %7
%12 = bitcast [3 x i8]* %8 to i8*
store i32 10, i32* %9
store i32 0, i32* %10
br label %13
13: ; preds = %62, %3
%14 = load i32, i32* %9
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %65
16: ; preds = %13
%17 = load i32, i32* %5
%18 = bitcast [3 x i8]* %8 to i8**
%19 = call i32 @write(i32 %17, i8** %18, i32 3)
%20 = icmp ne i32 %19, 3
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %4
br label %66
22: ; preds = %16
br label %23
23: ; preds = %61, %22
%24 = load i32, i32* %5
%25 = call i32 @readchar(i32 %24, i8* %11, i32 100)
%26 = icmp ne i32 %25, 0
%27 = xor i1 %26, true
br i1 %27, label %28, label %62
28: ; preds = %23
%29 = load i32, i32* %10
switch i32 %29, label %61 [
i32 0, label %30
i32 1, label %36
i32 2, label %48
]
30: ; preds = %28
%31 = load i8, i8* %11
%32 = zext i8 %31 to i32
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 1, i32* %10
br label %35
35: ; preds = %34, %30
br label %61
36: ; preds = %28
%37 = load i8, i8* %11
%38 = zext i8 %37 to i32
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 2, i32* %10
br label %47
41: ; preds = %36
%42 = load i8, i8* %11
%43 = zext i8 %42 to i32
%44 = icmp ne i32 %43, 10
br i1 %44, label %45, label %46
45: ; preds = %41
store i32 0, i32* %10
br label %46
46: ; preds = %45, %41
br label %47
47: ; preds = %46, %40
br label %61
48: ; preds = %28
%49 = load i8, i8* %11
%50 = zext i8 %49 to i32
%51 = icmp eq i32 %50, 65
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 0, i32* %4
br label %66
53: ; preds = %48
%54 = load i8, i8* %11
%55 = zext i8 %54 to i32
%56 = icmp eq i32 %55, 10
br i1 %56, label %57, label %58
57: ; preds = %53
store i32 1, i32* %10
br label %59
58: ; preds = %53
store i32 0, i32* %10
br label %59
59: ; preds = %58, %57
br label %60
60: ; preds = %59
br label %61
61: ; preds = %28, %60, %47, %35
br label %23
62: ; preds = %23
%63 = load i32, i32* %9
%64 = add nsw i32 %63, -1
store i32 %64, i32* %9
br label %13
65: ; preds = %13
store i32 -1, i32* %4
br label %66
66: ; preds = %65, %52, %21
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
@__const.t213_init.cmd = [3 x i8] c"\0A\01A"
define i32 @t213_init(i32 %0, i64* nocapture readnone %1, i64* nocapture readnone %2) {
%4 = alloca [3 x i8]
%5 = alloca i8
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%7 = bitcast [3 x i8]* %4 to i8**
br label %8
8: ; preds = %3, %35
%9 = phi i32 [ 0, %3 ], [ %36, %35 ]
%10 = phi i32 [ 10, %3 ], [ %37, %35 ]
%11 = call i32 @write(i32 %0, i8** %7, i32 3)
%12 = icmp eq i32 %11, 3
br i1 %12, label %13, label %39
13: ; preds = %8
%14 = call i32 @readchar(i32 %0, i8* %5, i32 100)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %35
16: ; preds = %13, %31
%17 = phi i32 [ %32, %31 ], [ %9, %13 ]
switch i32 %17, label %31 [
i32 0, label %18
i32 1, label %22
i32 2, label %28
]
18: ; preds = %16
%19 = load i8, i8* %5
%20 = icmp eq i8 %19, 10
%21 = zext i1 %20 to i32
br label %31
22: ; preds = %16
%23 = load i8, i8* %5
%24 = icmp eq i8 %23, 10
%25 = zext i1 %24 to i32
%26 = icmp eq i8 %23, 1
%27 = select i1 %26, i32 2, i32 %25
br label %31
28: ; preds = %16
%29 = load i8, i8* %5
switch i8 %29, label %30 [
i8 65, label %39
i8 10, label %31
]
30: ; preds = %28
br label %31
31: ; preds = %22, %18, %28, %30, %16
%32 = phi i32 [ %17, %16 ], [ 0, %30 ], [ %21, %18 ], [ 1, %28 ], [ %27, %22 ]
%33 = call i32 @readchar(i32 %0, i8* %5, i32 100)
%34 = icmp eq i32 %33, 0
br i1 %34, label %16, label %35
35: ; preds = %31, %13
%36 = phi i32 [ %9, %13 ], [ %32, %31 ]
%37 = add nsw i32 %10, -1
%38 = icmp ugt i32 %10, 1
br i1 %38, label %8, label %39
39: ; preds = %35, %8, %28
%40 = phi i32 [ 0, %28 ], [ -1, %8 ], [ -1, %35 ]
ret i32 %40
}