#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 SL11Read(int);
int /*<<< orphan*/ SL11Write(int, int);
int /*<<< orphan*/ cfe_sleep(int);
int /*<<< orphan*/ xprintf(char *, ...);
int speed_detect(void) {
  int i = 0;
  int full_speed;
  full_speed = 0xffff;
  SL11Write(0x06, 0x63);
  SL11Write(0x0f, 0xae);
  SL11Write(0x05, 0x08);
  SL11Write(0x05, (unsigned char)i);
  SL11Write(0x0f, 0xae);
  SL11Write(0x05, 0x08);
  cfe_sleep(1);
  SL11Write(0x05, 0);
  cfe_sleep(1);
  SL11Write(0x0d, 0xff);
  i = SL11Read(0x0d);
  if (i & 0x40) {
    xprintf("Device is Removed\n");
    return 0;
  }
  if ((i & 0x80) == 0) {
    xprintf("Slow Speed is detected %x\n", i);
    SL11Write(0x0f, 0xee);
    SL11Write(0x0e, 0xe0);
    SL11Write(0x05, 0x21);
    full_speed = 0;
  } else {
    xprintf("Full Speed is detected %x\n", i);
    SL11Write(0x0f, 0xae);
    SL11Write(0x0e, 0xe0);
    SL11Write(0x05, 0x05);
  }
  SL11Write(0x03, 0x50);
  SL11Write(0x04, 0);
  SL11Write(0x00, 0x01);
  cfe_sleep(3);
  return 0;
}


x86 assembly (clang, -O0):
<speed_detect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0xffff,-0xc(%rbp)
mov    $0x6,%edi
mov    $0x63,%esi
call   1030 <SL11Write@plt>
mov    $0xf,%edi
mov    $0xae,%esi
call   1030 <SL11Write@plt>
mov    $0x5,%edi
mov    $0x8,%esi
call   1030 <SL11Write@plt>
mov    -0x8(%rbp),%eax
movzbl %al,%esi
mov    $0x5,%edi
call   1030 <SL11Write@plt>
mov    $0xf,%edi
mov    $0xae,%esi
call   1030 <SL11Write@plt>
mov    $0x5,%edi
mov    $0x8,%esi
call   1030 <SL11Write@plt>
mov    $0x1,%edi
call   1060 <cfe_sleep@plt>
mov    $0x5,%edi
xor    %esi,%esi
call   1030 <SL11Write@plt>
mov    $0x1,%edi
call   1060 <cfe_sleep@plt>
mov    $0xd,%edi
mov    $0xff,%esi
call   1030 <SL11Write@plt>
mov    $0xd,%edi
call   1050 <SL11Read@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x40,%eax
cmp    $0x0,%eax
je     1216 <speed_detect+0xd6>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <xprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e4 <speed_detect+0x1a4>
mov    -0x8(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1271 <speed_detect+0x131>
mov    -0x8(%rbp),%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <xprintf@plt>
mov    $0xf,%edi
mov    $0xee,%esi
call   1030 <SL11Write@plt>
mov    $0xe,%edi
mov    $0xe0,%esi
call   1030 <SL11Write@plt>
mov    $0x5,%edi
mov    $0x21,%esi
call   1030 <SL11Write@plt>
movl   $0x0,-0xc(%rbp)
jmp    12ac <speed_detect+0x16c>
mov    -0x8(%rbp),%esi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1040 <xprintf@plt>
mov    $0xf,%edi
mov    $0xae,%esi
call   1030 <SL11Write@plt>
mov    $0xe,%edi
mov    $0xe0,%esi
call   1030 <SL11Write@plt>
mov    $0x5,%esi
mov    %esi,%edi
call   1030 <SL11Write@plt>
mov    $0x3,%edi
mov    $0x50,%esi
call   1030 <SL11Write@plt>
mov    $0x4,%edi
xor    %esi,%esi
call   1030 <SL11Write@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1030 <SL11Write@plt>
mov    $0x3,%edi
call   1060 <cfe_sleep@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<speed_detect>:
push   %rbp
push   %r14
push   %rbx
mov    $0x6,%edi
mov    $0x63,%esi
call   1030 <SL11Write@plt>
mov    $0xf,%edi
mov    $0xae,%esi
call   1030 <SL11Write@plt>
mov    $0x5,%r14d
mov    $0x5,%edi
mov    $0x8,%esi
call   1030 <SL11Write@plt>
mov    $0x5,%edi
xor    %esi,%esi
call   1030 <SL11Write@plt>
mov    $0xf,%edi
mov    $0xae,%esi
call   1030 <SL11Write@plt>
mov    $0x5,%edi
mov    $0x8,%esi
call   1030 <SL11Write@plt>
mov    $0x1,%edi
call   1060 <cfe_sleep@plt>
mov    $0x5,%edi
xor    %esi,%esi
call   1030 <SL11Write@plt>
mov    $0x1,%edi
call   1060 <cfe_sleep@plt>
mov    $0xd,%edi
mov    $0xff,%esi
call   1030 <SL11Write@plt>
mov    $0xd,%edi
call   1050 <SL11Read@plt>
test   $0x40,%al
jne    1270 <speed_detect+0x130>
xor    %ebx,%ebx
test   $0x80,%al
sete   %bl
lea    0xe23(%rip),%rcx
lea    0xe37(%rip),%rdi
cmove  %rcx,%rdi
mov    $0x21,%ebp
cmovne %r14d,%ebp
shl    $0x6,%ebx
or     $0xae,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <xprintf@plt>
mov    $0xf,%edi
mov    %ebx,%esi
call   1030 <SL11Write@plt>
mov    $0xe,%edi
mov    $0xe0,%esi
call   1030 <SL11Write@plt>
mov    $0x5,%edi
mov    %ebp,%esi
call   1030 <SL11Write@plt>
mov    $0x3,%edi
mov    $0x50,%esi
call   1030 <SL11Write@plt>
mov    $0x4,%edi
xor    %esi,%esi
call   1030 <SL11Write@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1030 <SL11Write@plt>
mov    $0x3,%edi
call   1060 <cfe_sleep@plt>
jmp    127e <speed_detect+0x13e>
lea    0xd89(%rip),%rdi
xor    %eax,%eax
call   1040 <xprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Device is Removed\0A\00"
@.str.1 = [27 x i8] c"Slow Speed is detected %x\0A\00"
@.str.2 = [27 x i8] c"Full Speed is detected %x\0A\00"
define i32 @speed_detect() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %2
store i32 65535, i32* %3
%4 = call i32 @SL11Write(i32 6, i32 99)
%5 = call i32 @SL11Write(i32 15, i32 174)
%6 = call i32 @SL11Write(i32 5, i32 8)
%7 = load i32, i32* %2
%8 = trunc i32 %7 to i8
%9 = zext i8 %8 to i32
%10 = call i32 @SL11Write(i32 5, i32 %9)
%11 = call i32 @SL11Write(i32 15, i32 174)
%12 = call i32 @SL11Write(i32 5, i32 8)
%13 = call i32 @cfe_sleep(i32 1)
%14 = call i32 @SL11Write(i32 5, i32 0)
%15 = call i32 @cfe_sleep(i32 1)
%16 = call i32 @SL11Write(i32 13, i32 255)
%17 = call i32 @SL11Read(i32 13)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = and i32 %18, 64
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i32 (i8*, ...) @xprintf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %44
23: ; preds = %0
%24 = load i32, i32* %2
%25 = and i32 %24, 128
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i32, i32* %2
%29 = call i32 (i8*, ...) @xprintf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = call i32 @SL11Write(i32 15, i32 238)
%31 = call i32 @SL11Write(i32 14, i32 224)
%32 = call i32 @SL11Write(i32 5, i32 33)
store i32 0, i32* %3
br label %39
33: ; preds = %23
%34 = load i32, i32* %2
%35 = call i32 (i8*, ...) @xprintf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = call i32 @SL11Write(i32 15, i32 174)
%37 = call i32 @SL11Write(i32 14, i32 224)
%38 = call i32 @SL11Write(i32 5, i32 5)
br label %39
39: ; preds = %33, %27
%40 = call i32 @SL11Write(i32 3, i32 80)
%41 = call i32 @SL11Write(i32 4, i32 0)
%42 = call i32 @SL11Write(i32 0, i32 1)
%43 = call i32 @cfe_sleep(i32 3)
store i32 0, i32* %1
br label %44
44: ; preds = %39, %21
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [19 x i8] c"Device is Removed\0A\00"
@.str.1 = [27 x i8] c"Slow Speed is detected %x\0A\00"
@.str.2 = [27 x i8] c"Full Speed is detected %x\0A\00"
define i32 @speed_detect() {
%1 = tail call i32 @SL11Write(i32 6, i32 99)
%2 = tail call i32 @SL11Write(i32 15, i32 174)
%3 = tail call i32 @SL11Write(i32 5, i32 8)
%4 = tail call i32 @SL11Write(i32 5, i32 0)
%5 = tail call i32 @SL11Write(i32 15, i32 174)
%6 = tail call i32 @SL11Write(i32 5, i32 8)
%7 = tail call i32 @cfe_sleep(i32 1)
%8 = tail call i32 @SL11Write(i32 5, i32 0)
%9 = tail call i32 @cfe_sleep(i32 1)
%10 = tail call i32 @SL11Write(i32 13, i32 255)
%11 = tail call i32 @SL11Read(i32 13)
%12 = and i32 %11, 64
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %0
%15 = tail call i32 (i8*, ...) @xprintf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %30
16: ; preds = %0
%17 = and i32 %11, 128
%18 = icmp eq i32 %17, 0
%19 = select i1 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0)
%20 = select i1 %18, i32 238, i32 174
%21 = select i1 %18, i32 33, i32 5
%22 = tail call i32 (i8*, ...) @xprintf(i8* %19, i32 %11)
%23 = tail call i32 @SL11Write(i32 15, i32 %20)
%24 = tail call i32 @SL11Write(i32 14, i32 224)
%25 = tail call i32 @SL11Write(i32 5, i32 %21)
%26 = tail call i32 @SL11Write(i32 3, i32 80)
%27 = tail call i32 @SL11Write(i32 4, i32 0)
%28 = tail call i32 @SL11Write(i32 0, i32 1)
%29 = tail call i32 @cfe_sleep(i32 3)
br label %30
30: ; preds = %16, %14
ret i32 0
}