#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__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ abort();
scalar_t__ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int spawn(char *arguments, int fd) {
  pid_t child_pid;
  int i, j, n;
  if (arguments[0] == 'c' && arguments[1] == 'd') {
    for (n = 3, i = 0; i < n; i++) {
      int j = 0;
      do {
        arguments[j] = arguments[j + 1];
        j++;
      } while (arguments[j - 1] != '\0');
    }
    close(1);
    dup(fd);
    if ((chdir(arguments)) != 0) {
      syslog(LOG_ERR, strerror(errno));
      strcpy(arguments, "echo No such directory or file");
      spawn(arguments, fd);
    }
  } else {
    child_pid = fork();
    if (child_pid != 0) {
      close(1);
      wait(0);
      return 0;
    } else {
      close(1);
      dup(fd);
      execlp("/bin/sh", "bash", "-c", arguments, NULL);
      perror("Exec\n");
      abort();
    }
  }
}


x86 assembly (clang, -O0):
<spawn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x63,%eax
jne    12cc <spawn+0x10c>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x64,%eax
jne    12cc <spawn+0x10c>
movl   $0x3,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1262 <spawn+0xa2>
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1211 <spawn+0x51>
jmp    1254 <spawn+0x94>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11fe <spawn+0x3e>
mov    $0x1,%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <dup@plt>
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     12c7 <spawn+0x107>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%esi
call   1080 <syslog@plt>
mov    -0x10(%rbp),%rdi
lea    0xd4a(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <spawn@plt>
jmp    1347 <spawn+0x187>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12ff <spawn+0x13f>
mov    $0x1,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
movl   $0x0,-0x4(%rbp)
jmp    1347 <spawn+0x187>
mov    $0x1,%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <dup@plt>
mov    -0x10(%rbp),%rcx
lea    0xd03(%rip),%rdi
lea    0xd04(%rip),%rsi
lea    0xd02(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   10b0 <execlp@plt>
lea    0xcf4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
cmpb   $0x63,(%rdi)
jne    128c <spawn+0xdc>
lea    0x1(%rbx),%r12
mov    0x2e0b(%rip),%r13
lea    0xe24(%rip),%r14
nopl   0x0(%rax)
cmpb   $0x64,0x1(%rbx)
jne    128c <spawn+0xdc>
mov    %r12,%rax
nopl   (%rax)
movzbl (%rax),%ecx
mov    %cl,-0x1(%rax)
add    $0x1,%rax
test   %cl,%cl
jne    11f0 <spawn+0x40>
mov    %r12,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rax),%ecx
mov    %cl,-0x1(%rax)
add    $0x1,%rax
test   %cl,%cl
jne    1210 <spawn+0x60>
mov    %r12,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rax),%ecx
mov    %cl,-0x1(%rax)
add    $0x1,%rax
test   %cl,%cl
jne    1230 <spawn+0x80>
mov    $0x1,%edi
call   1070 <close@plt>
mov    %r15d,%edi
call   1060 <dup@plt>
mov    %rbx,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
je     12ac <spawn+0xfc>
mov    0x2d6c(%rip),%rax
mov    (%rax),%ebp
mov    0x0(%r13),%edi
call   10b0 <strerror@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1080 <syslog@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
cmpb   $0x63,(%rbx)
je     11e0 <spawn+0x30>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %rax,%rbp
mov    $0x1,%edi
call   1070 <close@plt>
test   %rbp,%rbp
je     12bd <spawn+0x10d>
xor    %edi,%edi
call   10c0 <wait@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
call   1060 <dup@plt>
lea    0xd53(%rip),%rdi
lea    0xd54(%rip),%rsi
lea    0xd52(%rip),%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   10a0 <execlp@plt>
lea    0xd43(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@errno = global i32 0
@.str = [31 x i8] c"echo No such directory or file\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [5 x i8] c"bash\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [6 x i8] c"Exec\0A\00"
define i32 @spawn(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 99
br i1 %15, label %16, label %72
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 100
br i1 %21, label %22, label %72
22: ; preds = %16
store i32 3, i32* %9
store i32 0, i32* %7
br label %23
23: ; preds = %51, %22
%24 = load i32, i32* %7
%25 = load i32, i32* %9
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %54
27: ; preds = %23
store i32 0, i32* %10
br label %28
28: ; preds = %41, %27
%29 = load i8*, i8** %4
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i8, i8* %33
%35 = load i8*, i8** %4
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 %34, i8* %38
%39 = load i32, i32* %10
%40 = add nsw i32 %39, 1
store i32 %40, i32* %10
br label %41
41: ; preds = %28
%42 = load i8*, i8** %4
%43 = load i32, i32* %10
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 0
br i1 %49, label %28, label %50
50: ; preds = %41
br label %51
51: ; preds = %50
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %23
54: ; preds = %23
%55 = call i32 @close(i32 1)
%56 = load i32, i32* %5
%57 = call i32 @dup(i32 %56)
%58 = load i8*, i8** %4
%59 = call i64 @chdir(i8* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %71
61: ; preds = %54
%62 = load i32, i32* @LOG_ERR
%63 = load i32, i32* @errno
%64 = call i32 @strerror(i32 %63)
%65 = call i32 @syslog(i32 %62, i32 %64)
%66 = load i8*, i8** %4
%67 = call i32 @strcpy(i8* %66, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%68 = load i8*, i8** %4
%69 = load i32, i32* %5
%70 = call i32 @spawn(i8* %68, i32 %69)
br label %71
71: ; preds = %61, %54
br label %87
72: ; preds = %16, %2
%73 = call i64 (...) @fork()
store i64 %73, i64* %6
%74 = load i64, i64* %6
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %72
%77 = call i32 @close(i32 1)
%78 = call i32 @wait(i32 0)
store i32 0, i32* %3
br label %87
79: ; preds = %72
%80 = call i32 @close(i32 1)
%81 = load i32, i32* %5
%82 = call i32 @dup(i32 %81)
%83 = load i8*, i8** %4
%84 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %83, i32* null)
%85 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%86 = call i32 (...) @abort()
unreachable
87: ; preds = %76, %71
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@errno = global i32 0
@.str = [31 x i8] c"echo No such directory or file\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [5 x i8] c"bash\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [6 x i8] c"Exec\0A\00"
define i32 @spawn(i8* %0, i32 %1) {
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 99
br i1 %4, label %5, label %44
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 1
br label %7
7: ; preds = %5, %36
%8 = load i8, i8* %6
%9 = icmp eq i8 %8, 100
br i1 %9, label %10, label %44
10: ; preds = %7, %10
%11 = phi i64 [ %12, %10 ], [ 0, %7 ]
%12 = add nuw nsw i64 %11, 1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 %14, i8* %15
%16 = icmp eq i8 %14, 0
br i1 %16, label %17, label %10
17: ; preds = %10, %17
%18 = phi i64 [ %19, %17 ], [ 0, %10 ]
%19 = add nuw nsw i64 %18, 1
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = load i8, i8* %20
%22 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 %21, i8* %22
%23 = icmp eq i8 %21, 0
br i1 %23, label %24, label %17
24: ; preds = %17, %24
%25 = phi i64 [ %26, %24 ], [ 0, %17 ]
%26 = add nuw nsw i64 %25, 1
%27 = getelementptr inbounds i8, i8* %0, i64 %26
%28 = load i8, i8* %27
%29 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 %28, i8* %29
%30 = icmp eq i8 %28, 0
br i1 %30, label %31, label %24
31: ; preds = %24
%32 = tail call i32 @close(i32 1)
%33 = tail call i32 @dup(i32 %1)
%34 = tail call i64 @chdir(i8* %0)
%35 = icmp eq i64 %34, 0
br i1 %35, label %55, label %36
36: ; preds = %31
%37 = load i32, i32* @LOG_ERR
%38 = load i32, i32* @errno
%39 = tail call i32 @strerror(i32 %38)
%40 = tail call i32 @syslog(i32 %37, i32 %39)
%41 = tail call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%42 = load i8, i8* %0
%43 = icmp eq i8 %42, 99
br i1 %43, label %7, label %44
44: ; preds = %36, %7, %2
%45 = tail call i64 (...) @fork()
%46 = icmp eq i64 %45, 0
%47 = tail call i32 @close(i32 1)
br i1 %46, label %50, label %48
48: ; preds = %44
%49 = tail call i32 @wait(i32 0)
br label %55
50: ; preds = %44
%51 = tail call i32 @dup(i32 %1)
%52 = tail call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32* null)
%53 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%54 = tail call i32 (...) @abort()
unreachable
55: ; preds = %31, %48
ret i32 0
}