#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 /*<<< orphan*/ write(int, char *, int);
int spaceball_cmd(int fd, char *c, char *d) {
  int i;
  for (i = 0; c[i]; i++)
    write(fd, c + i, 1);
  write(fd, "\r", 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,-0x4(%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     1182 <spaceball_cmd+0x52>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    114a <spaceball_cmd+0x1a>
mov    -0x4(%rbp),%edi
lea    0xe74(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x4(%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
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     1169 <spaceball_cmd+0x39>
mov    %r15,%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1150 <spaceball_cmd+0x20>
lea    0xe90(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <write@plt>
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>

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 i8*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %7
br label %8
8: ; preds = %22, %3
%9 = load i8*, i8** %5
%10 = load i32, i32* %7
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %8
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @write(i32 %16, i8* %20, i32 1)
br label %22
22: ; preds = %15
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
br label %8
25: ; preds = %8
%26 = load i32, i32* %4
%27 = call i32 @write(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%28 = load i32, i32* %4
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = call i32 @toupper(i8 signext %31)
%33 = load i8*, i8** %6
%34 = call i32 @spaceball_waitcmd(i32 %28, i32 %32, i8* %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
ret i32 %35
}

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 %14, label %6
6: ; preds = %3, %6
%7 = phi i64 [ %10, %6 ], [ 0, %3 ]
%8 = phi i8* [ %11, %6 ], [ %1, %3 ]
%9 = tail call i32 @write(i32 %0, i8* %8, i32 1)
%10 = add nuw i64 %7, 1
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %6
14: ; preds = %6, %3
%15 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%16 = load i8, i8* %1
%17 = tail call i32 @toupper(i8 signext %16)
%18 = tail call i32 @spaceball_waitcmd(i32 %0, i32 %17, i8* %2)
ret i32 %18
}