#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, 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    $0x30,%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,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1262 <spawn+0xa2>
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x2c(%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,-0x30(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x30(%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    $0x30,%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
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 99
br i1 %14, label %15, label %71
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 100
br i1 %20, label %21, label %71
21: ; preds = %15
store i32 3, i32* %8
store i32 0, i32* %7
br label %22
22: ; preds = %50, %21
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %53
26: ; preds = %22
store i32 0, i32* %9
br label %27
27: ; preds = %40, %26
%28 = load i8*, i8** %4
%29 = load i32, i32* %9
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = load i8, i8* %32
%34 = load i8*, i8** %4
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 %33, i8* %37
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
store i32 %39, i32* %9
br label %40
40: ; preds = %27
%41 = load i8*, i8** %4
%42 = load i32, i32* %9
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 0
br i1 %48, label %27, label %49
49: ; preds = %40
br label %50
50: ; preds = %49
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 1
store i32 %52, i32* %7
br label %22
53: ; preds = %22
%54 = call i32 @close(i32 1)
%55 = load i32, i32* %5
%56 = call i32 @dup(i32 %55)
%57 = load i8*, i8** %4
%58 = call i64 @chdir(i8* %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %70
60: ; preds = %53
%61 = load i32, i32* @LOG_ERR
%62 = load i32, i32* @errno
%63 = call i32 @strerror(i32 %62)
%64 = call i32 @syslog(i32 %61, i32 %63)
%65 = load i8*, i8** %4
%66 = call i32 @strcpy(i8* %65, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%67 = load i8*, i8** %4
%68 = load i32, i32* %5
%69 = call i32 @spawn(i8* %67, i32 %68)
br label %70
70: ; preds = %60, %53
br label %86
71: ; preds = %15, %2
%72 = call i64 (...) @fork()
store i64 %72, i64* %6
%73 = load i64, i64* %6
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %71
%76 = call i32 @close(i32 1)
%77 = call i32 @wait(i32 0)
store i32 0, i32* %3
br label %86
78: ; preds = %71
%79 = call i32 @close(i32 1)
%80 = load i32, i32* %5
%81 = call i32 @dup(i32 %80)
%82 = load i8*, i8** %4
%83 = 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* %82, i32* null)
%84 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%85 = call i32 (...) @abort()
unreachable
86: ; preds = %75, %70
%87 = load i32, i32* %3
ret i32 %87
}

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
}