#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 spaceball_waitcmd(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ toupper(char);
int write(int, char *, int);
int spaceball_cmd(int fd, char *c, char *d) {
  int i;
  for (i = 0; c[i]; i++)
    if (write(fd, c + i, 1) != 1)
      return -1;
  if (write(fd, "\r", 1) != 1)
    return -1;
  i = spaceball_waitcmd(fd, toupper(c[0]), d);
  return i;
}


x86 assembly (clang, -O0):
<spaceball_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     119c <spaceball_cmd+0x6c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     1189 <spaceball_cmd+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <spaceball_cmd+0xbe>
jmp    118e <spaceball_cmd+0x5e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    114a <spaceball_cmd+0x1a>
mov    -0x8(%rbp),%edi
lea    0xe5a(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     11c5 <spaceball_cmd+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <spaceball_cmd+0xbe>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
call   1050 <spaceball_waitcmd@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spaceball_cmd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
cmpb   $0x0,(%rsi)
je     116e <spaceball_cmd+0x3e>
mov    %r15,%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    11a5 <spaceball_cmd+0x75>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1150 <spaceball_cmd+0x20>
lea    0xe8b(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    11a5 <spaceball_cmd+0x75>
movsbl (%r15),%edi
call   1030 <toupper@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <spaceball_waitcmd@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0D\00"
define i32 @spaceball_cmd(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %8
br label %9
9: ; preds = %26, %3
%10 = load i8*, i8** %6
%11 = load i32, i32* %8
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %9
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = call i32 @write(i32 %17, i8* %21, i32 1)
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %25
24: ; preds = %16
store i32 -1, i32* %4
br label %43
25: ; preds = %16
br label %26
26: ; preds = %25
%27 = load i32, i32* %8
%28 = add nsw i32 %27, 1
store i32 %28, i32* %8
br label %9
29: ; preds = %9
%30 = load i32, i32* %5
%31 = call i32 @write(i32 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -1, i32* %4
br label %43
34: ; preds = %29
%35 = load i32, i32* %5
%36 = load i8*, i8** %6
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = call i32 @toupper(i8 signext %38)
%40 = load i8*, i8** %7
%41 = call i32 @spaceball_waitcmd(i32 %35, i32 %39, i8* %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
store i32 %42, i32* %4
br label %43
43: ; preds = %34, %33, %24
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@.str = [2 x i8] c"\0D\00"
define i32 @spaceball_cmd(i32 %0, i8* nocapture %1, i8* %2) {
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %16, label %10
6: ; preds = %10
%7 = getelementptr inbounds i8, i8* %1, i64 %15
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %3, %6
%11 = phi i64 [ %15, %6 ], [ 0, %3 ]
%12 = phi i8* [ %7, %6 ], [ %1, %3 ]
%13 = tail call i32 @write(i32 %0, i8* %12, i32 1)
%14 = icmp eq i32 %13, 1
%15 = add nuw i64 %11, 1
br i1 %14, label %6, label %23
16: ; preds = %6, %3
%17 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i8, i8* %1
%21 = tail call i32 @toupper(i8 signext %20)
%22 = tail call i32 @spaceball_waitcmd(i32 %0, i32 %21, i8* %2)
br label %23
23: ; preds = %10, %16, %19
%24 = phi i32 [ %22, %19 ], [ -1, %16 ], [ -1, %10 ]
ret i32 %24
}