#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*/ exit(int);
int getline(char **, size_t *, int /*<<< orphan*/);
char *index(char *, char);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
char *strsep(char **, char *);
int getcmd(char *prompt, char *args[], int *background) {
  int length;
  int i = 0;
  char *token;
  char *loc;
  char *line = NULL;
  size_t linecap = 0;
  printf("%s", prompt);
  length = getline(&line, &linecap, stdin);
  if (length <= 0)
    exit(-1);
  if ((loc = index(line, '&')) != NULL) {
    *background = 1;
    *loc = ' ';
  } else
    *background = 0;
  while ((token = strsep(&line, " \t\n")) != NULL) {
    for (int j = 0; j < strlen(token); j++)
      if (token[j] <= 32)
        token[j] = '\0';
    if (strlen(token) > 0)
      args[i++] = token;
  }
  return i;
}


x86 assembly (clang, -O0):
<getcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11c8 <getcmd+0x68>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    $0x26,%esi
call   1050 <index@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11fa <getcmd+0x9a>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x30(%rbp),%rax
movb   $0x20,(%rax)
jmp    1204 <getcmd+0xa4>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
jmp    1209 <getcmd+0xa9>
lea    -0x38(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1060 <strsep@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12af <getcmd+0x14f>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
cmp    %ecx,%eax
jge    127e <getcmd+0x11e>
mov    -0x28(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jg     126b <getcmd+0x10b>
mov    -0x28(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1270 <getcmd+0x110>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    122e <getcmd+0xce>
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jle    12aa <getcmd+0x14a>
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1209 <getcmd+0xa9>
mov    -0x20(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%rsi
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe70(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1070 <getline@plt>
test   %eax,%eax
jle    128e <getcmd+0x12e>
mov    0x8(%rsp),%rdi
mov    $0x26,%esi
call   1050 <index@plt>
test   %rax,%rax
je     11d4 <getcmd+0x74>
movl   $0x1,(%rbx)
movb   $0x20,(%rax)
jmp    11da <getcmd+0x7a>
movl   $0x0,(%rbx)
lea    0xe22(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <strsep@plt>
test   %rax,%rax
je     1279 <getcmd+0x119>
mov    %rax,%rbx
xor    %r13d,%r13d
lea    0xe02(%rip),%r15
lea    0x8(%rsp),%r12
jmp    1223 <getcmd+0xc3>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <strsep@plt>
mov    %rax,%rbx
test   %rax,%rax
je     127c <getcmd+0x11c>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1260 <getcmd+0x100>
xor    %ebp,%ebp
jmp    1253 <getcmd+0xf3>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbp
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbp
jge    1260 <getcmd+0x100>
cmpb   $0x20,(%rbx,%rbp,1)
jg     1240 <getcmd+0xe0>
movb   $0x0,(%rbx,%rbp,1)
jmp    1240 <getcmd+0xe0>
nop
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1210 <getcmd+0xb0>
movslq %r13d,%rax
add    $0x1,%r13d
mov    %rbx,(%r14,%rax,8)
jmp    1210 <getcmd+0xb0>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.1 = [4 x i8] c" \09\0A\00"
define i32 @getcmd(i8* %0, i8** %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* %2, i32** %6
store i32 0, i32* %8
store i8* null, i8** %11
store i64 0, i64* %12
%14 = load i8*, i8** %4
%15 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @stdin
%17 = call i32 @getline(i8** %11, i64* %12, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %3
%23 = load i8*, i8** %11
%24 = call i8* @index(i8* %23, i8 signext 38)
store i8* %24, i8** %10
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32*, i32** %6
store i32 1, i32* %27
%28 = load i8*, i8** %10
store i8 32, i8* %28
br label %31
29: ; preds = %22
%30 = load i32*, i32** %6
store i32 0, i32* %30
br label %31
31: ; preds = %29, %26
br label %32
32: ; preds = %69, %31
%33 = call i8* @strsep(i8** %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %33, i8** %9
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %70
35: ; preds = %32
store i32 0, i32* %13
br label %36
36: ; preds = %55, %35
%37 = load i32, i32* %13
%38 = load i8*, i8** %9
%39 = call i32 @strlen(i8* %38)
%40 = icmp slt i32 %37, %39
br i1 %40, label %41, label %58
41: ; preds = %36
%42 = load i8*, i8** %9
%43 = load i32, i32* %13
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp sle i32 %47, 32
br i1 %48, label %49, label %54
49: ; preds = %41
%50 = load i8*, i8** %9
%51 = load i32, i32* %13
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
br label %54
54: ; preds = %49, %41
br label %55
55: ; preds = %54
%56 = load i32, i32* %13
%57 = add nsw i32 %56, 1
store i32 %57, i32* %13
br label %36
58: ; preds = %36
%59 = load i8*, i8** %9
%60 = call i32 @strlen(i8* %59)
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %69
62: ; preds = %58
%63 = load i8*, i8** %9
%64 = load i8**, i8*** %5
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
%67 = sext i32 %65 to i64
%68 = getelementptr inbounds i8*, i8** %64, i64 %67
store i8* %63, i8** %68
br label %69
69: ; preds = %62, %58
br label %32
70: ; preds = %32
%71 = load i32, i32* %8
ret i32 %71
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.1 = [4 x i8] c" \09\0A\00"
define i32 @getcmd(i8* %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%7 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%8 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = load i32, i32* @stdin
%10 = call i32 @getline(i8** %4, i64* %5, i32 %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i8* @index(i8* %15, i8 signext 38)
%17 = icmp eq i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %14
store i32 1, i32* %2
store i8 32, i8* %16
br label %20
19: ; preds = %14
store i32 0, i32* %2
br label %20
20: ; preds = %19, %18
%21 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %50, label %23
23: ; preds = %20, %46
%24 = phi i8* [ %48, %46 ], [ %21, %20 ]
%25 = phi i32 [ %47, %46 ], [ 0, %20 ]
%26 = call i32 @strlen(i8* %24)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %37, %23
%29 = call i32 @strlen(i8* %24)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %42, label %46
31: ; preds = %23, %37
%32 = phi i64 [ %38, %37 ], [ 0, %23 ]
%33 = getelementptr inbounds i8, i8* %24, i64 %32
%34 = load i8, i8* %33
%35 = icmp slt i8 %34, 33
br i1 %35, label %36, label %37
36: ; preds = %31
store i8 0, i8* %33
br label %37
37: ; preds = %31, %36
%38 = add nuw nsw i64 %32, 1
%39 = call i32 @strlen(i8* %24)
%40 = sext i32 %39 to i64
%41 = icmp slt i64 %38, %40
br i1 %41, label %31, label %28
42: ; preds = %28
%43 = add nsw i32 %25, 1
%44 = sext i32 %25 to i64
%45 = getelementptr inbounds i8*, i8** %1, i64 %44
store i8* %24, i8** %45
br label %46
46: ; preds = %42, %28
%47 = phi i32 [ %43, %42 ], [ %25, %28 ]
%48 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %23
50: ; preds = %46, %20
%51 = phi i32 [ 0, %20 ], [ %47, %46 ]
ret i32 %51
}