#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 */
typedef scalar_t__ __ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/);
int getpid();
char *index(char *, char);
int parseCmd(char **, char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ stdin;
size_t strlen(char *);
int getCmd(char *prompt, char **args, int *background, int *redirect) {
  char *ampLoc, *redirectLoc;
  size_t lineSize = strlen(prompt);
  char *line = NULL;
  __ssize_t length = 0;
  printf("%s(PID: %d) > ", prompt, getpid());
  length = getline(&line, &lineSize, stdin);
  if (length <= 0) {
    perror("A fatal error has occurred. Exiting.");
    exit(1);
  }
  if ((ampLoc = index(line, '&')) != NULL) {
    *background = 1;
    *ampLoc = ' ';
  } else {
    *background = 0;
  }
  if ((redirectLoc = index(line, '>')) != NULL) {
    *redirect = 1;
    *redirectLoc = ' ';
  } else {
    *redirect = 0;
  }
  char *line_copy = line;
  int argCount = parseCmd(args, &line_copy);
  return argCount;
}


x86 assembly (clang, -O0):
<getCmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
lea    0xe2f(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1090 <getline@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jg     1211 <getCmd+0x91>
lea    0xe0d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x40(%rbp),%rdi
mov    $0x26,%esi
call   1070 <index@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1243 <getCmd+0xc3>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x28(%rbp),%rax
movb   $0x20,(%rax)
jmp    124d <getCmd+0xcd>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x40(%rbp),%rdi
mov    $0x3e,%esi
call   1070 <index@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     127f <getCmd+0xff>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x30(%rbp),%rax
movb   $0x20,(%rax)
jmp    1289 <getCmd+0x109>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1030 <parseCmd@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getCmd>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    %rax,0x10(%rsp)
movq   $0x0,(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe49(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <printf@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   1090 <getline@plt>
test   %rax,%rax
jle    124c <getCmd+0xcc>
mov    (%rsp),%rdi
mov    $0x26,%esi
call   1070 <index@plt>
test   %rax,%rax
je     11fc <getCmd+0x7c>
movl   $0x1,(%r12)
movb   $0x20,(%rax)
jmp    1204 <getCmd+0x84>
movl   $0x0,(%r12)
mov    (%rsp),%rdi
mov    $0x3e,%esi
call   1070 <index@plt>
test   %rax,%rax
je     1223 <getCmd+0xa3>
movl   $0x1,(%r15)
movb   $0x20,(%rax)
jmp    122a <getCmd+0xaa>
movl   $0x0,(%r15)
mov    (%rsp),%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1030 <parseCmd@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdbc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"%s(PID: %d) > \00"
@stdin = global i32 0
@.str.1 = [37 x i8] c"A fatal error has occurred. Exiting.\00"
define i32 @getCmd(i8* %0, i8** %1, i32* %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %11
store i8* null, i8** %12
store i64 0, i64* %13
%18 = load i8*, i8** %5
%19 = call i32 (...) @getpid()
%20 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %19)
%21 = load i32, i32* @stdin
%22 = call i64 @getline(i8** %12, i64* %11, i32 %21)
store i64 %22, i64* %13
%23 = load i64, i64* %13
%24 = icmp sle i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %4
%26 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %4
%29 = load i8*, i8** %12
%30 = call i8* @index(i8* %29, i8 signext 38)
store i8* %30, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32*, i32** %7
store i32 1, i32* %33
%34 = load i8*, i8** %9
store i8 32, i8* %34
br label %37
35: ; preds = %28
%36 = load i32*, i32** %7
store i32 0, i32* %36
br label %37
37: ; preds = %35, %32
%38 = load i8*, i8** %12
%39 = call i8* @index(i8* %38, i8 signext 62)
store i8* %39, i8** %10
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32*, i32** %8
store i32 1, i32* %42
%43 = load i8*, i8** %10
store i8 32, i8* %43
br label %46
44: ; preds = %37
%45 = load i32*, i32** %8
store i32 0, i32* %45
br label %46
46: ; preds = %44, %41
%47 = load i8*, i8** %12
store i8* %47, i8** %14
%48 = load i8**, i8*** %6
%49 = call i32 @parseCmd(i8** %48, i8** %14)
store i32 %49, i32* %15
%50 = load i32, i32* %15
ret i32 %50
}

LLVM IR (-O2):
@.str = [15 x i8] c"%s(PID: %d) > \00"
@stdin = global i32 0
@.str.1 = [37 x i8] c"A fatal error has occurred. Exiting.\00"
define i32 @getCmd(i8* %0, i8** %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = bitcast i64* %5 to i8*
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
store i64 %9, i64* %5
%10 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %11)
%13 = load i32, i32* @stdin
%14 = call i64 @getline(i8** %6, i64* %5, i32 %13)
%15 = icmp slt i64 %14, 1
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = call i8* @index(i8* %20, i8 signext 38)
%22 = icmp eq i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %19
store i32 1, i32* %2
store i8 32, i8* %21
br label %25
24: ; preds = %19
store i32 0, i32* %2
br label %25
25: ; preds = %24, %23
%26 = load i8*, i8** %6
%27 = call i8* @index(i8* %26, i8 signext 62)
%28 = icmp eq i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %25
store i32 1, i32* %3
store i8 32, i8* %27
br label %31
30: ; preds = %25
store i32 0, i32* %3
br label %31
31: ; preds = %30, %29
%32 = bitcast i8** %7 to i8*
%33 = load i8*, i8** %6
store i8* %33, i8** %7
%34 = call i32 @parseCmd(i8** %1, i8** %7)
ret i32 %34
}