#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*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ readchar(int, unsigned char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int sball4000_init(int fd) {
  unsigned char c;
  if (readchar(fd, &c, 4000) || c != 0x11 || readchar(fd, &c, 1000) ||
      c != 0x0d) {
    fprintf(stderr, "This doesn't look like a Spaceball 4000 FLX.\n");
    return -1;
  }
  sleep(2);
  if (write(fd, "YS\rM\r", 5) != 5)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0xfa0,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
jne    119b <sball4000_init+0x5b>
movzbl -0x9(%rbp),%eax
cmp    $0x11,%eax
jne    119b <sball4000_init+0x5b>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x3e8,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
jne    119b <sball4000_init+0x5b>
movzbl -0x9(%rbp),%eax
cmp    $0xd,%eax
je     11bc <sball4000_init+0x7c>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <sball4000_init+0xb6>
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    0xe5e(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
cmp    $0x5,%eax
je     11ef <sball4000_init+0xaf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <sball4000_init+0xb6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xf(%rsp),%rsi
mov    $0xfa0,%edx
call   1030 <readchar@plt>
test   %rax,%rax
jne    11aa <sball4000_init+0x6a>
cmpb   $0x11,0xf(%rsp)
jne    11aa <sball4000_init+0x6a>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3e8,%edx
call   1030 <readchar@plt>
test   %rax,%rax
jne    11aa <sball4000_init+0x6a>
cmpb   $0xd,0xf(%rsp)
jne    11aa <sball4000_init+0x6a>
mov    $0x2,%edi
call   1060 <sleep@plt>
lea    0xe9e(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x5,%ecx
setne  %al
neg    %eax
jmp    11c4 <sball4000_init+0x84>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [46 x i8] c"This doesn't look like a Spaceball 4000 FLX.\0A\00"
@.str.1 = [6 x i8] c"YS\0DM\0D\00"
define i32 @sball4000_init(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i64 @readchar(i32 %5, i8* %4, i32 4000)
%7 = icmp ne i64 %6, 0
br i1 %7, label %20, label %8
8: ; preds = %1
%9 = load i8, i8* %4
%10 = zext i8 %9 to i32
%11 = icmp ne i32 %10, 17
br i1 %11, label %20, label %12
12: ; preds = %8
%13 = load i32, i32* %3
%14 = call i64 @readchar(i32 %13, i8* %4, i32 1000)
%15 = icmp ne i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = load i8, i8* %4
%18 = zext i8 %17 to i32
%19 = icmp ne i32 %18, 13
br i1 %19, label %20, label %23
20: ; preds = %16, %12, %8, %1
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
23: ; preds = %16
%24 = call i32 @sleep(i32 2)
%25 = load i32, i32* %3
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%27 = icmp ne i32 %26, 5
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %2
br label %30
29: ; preds = %23
store i32 0, i32* %2
br label %30
30: ; preds = %29, %28, %20
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [46 x i8] c"This doesn't look like a Spaceball 4000 FLX.\0A\00"
@.str.1 = [6 x i8] c"YS\0DM\0D\00"
define i32 @sball4000_init(i32 %0) {
%2 = alloca i8
%3 = call i64 @readchar(i32 %0, i8* %2, i32 4000)
%4 = icmp ne i64 %3, 0
%5 = load i8, i8* %2
%6 = icmp ne i8 %5, 17
%7 = select i1 %4, i1 true, i1 %6
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = call i64 @readchar(i32 %0, i8* %2, i32 1000)
%10 = icmp ne i64 %9, 0
%11 = load i8, i8* %2
%12 = icmp ne i8 %11, 13
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %17
14: ; preds = %8, %1
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %22
17: ; preds = %8
%18 = call i32 @sleep(i32 2)
%19 = call i32 @write(i32 %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%20 = icmp ne i32 %19, 5
%21 = sext i1 %20 to i32
br label %22
22: ; preds = %17, %14
%23 = phi i32 [ -1, %14 ], [ %21, %17 ]
ret i32 %23
}