#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*/ sleep(int);
int write(int, char *, int);
int spaceball_init(int fd) {
  unsigned char c;
  if (readchar(fd, &c, 4000) || c != 0x11 || readchar(fd, &c, 1000) ||
      c != 0x0d)
    return -1;
  sleep(2);
  if (write(fd, "YS\rM\r", 5) != 5)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<spaceball_init>:
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    118b <spaceball_init+0x5b>
movzbl -0x9(%rbp),%eax
cmp    $0x11,%eax
jne    118b <spaceball_init+0x5b>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x3e8,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
jne    118b <spaceball_init+0x5b>
movzbl -0x9(%rbp),%eax
cmp    $0xd,%eax
je     1197 <spaceball_init+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <spaceball_init+0xa1>
mov    $0x2,%edi
call   1050 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    0xe55(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
cmp    $0x5,%eax
je     11ca <spaceball_init+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <spaceball_init+0xa1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spaceball_init>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x7(%rsp),%rsi
mov    $0xfa0,%edx
call   1030 <readchar@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    119a <spaceball_init+0x6a>
cmpb   $0x11,0x7(%rsp)
jne    119a <spaceball_init+0x6a>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3e8,%edx
call   1030 <readchar@plt>
test   %rax,%rax
jne    119a <spaceball_init+0x6a>
cmpb   $0xd,0x7(%rsp)
jne    119a <spaceball_init+0x6a>
mov    $0x2,%edi
call   1050 <sleep@plt>
lea    0xe7d(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
xor    %ebp,%ebp
cmp    $0x5,%eax
setne  %bpl
neg    %ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"YS\0DM\0D\00"
define i32 @spaceball_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 %21
20: ; preds = %16, %12, %8, %1
store i32 -1, i32* %2
br label %28
21: ; preds = %16
%22 = call i32 @sleep(i32 2)
%23 = load i32, i32* %3
%24 = call i32 @write(i32 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%25 = icmp ne i32 %24, 5
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 -1, i32* %2
br label %28
27: ; preds = %21
store i32 0, i32* %2
br label %28
28: ; preds = %27, %26, %20
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [6 x i8] c"YS\0DM\0D\00"
define i32 @spaceball_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 %19, 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 %19, label %14
14: ; preds = %8
%15 = call i32 @sleep(i32 2)
%16 = call i32 @write(i32 %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%17 = icmp ne i32 %16, 5
%18 = sext i1 %17 to i32
br label %19
19: ; preds = %14, %1, %8
%20 = phi i32 [ -1, %8 ], [ -1, %1 ], [ %18, %14 ]
ret i32 %20
}