#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*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execcmd(char *, int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
char *strtok(char *, char *);
char *trimspace(char *);
void proccmd(char *line, int *fd, int mode) {
  char *tok;
  pid_t pid;
  int status;
  size_t nl = strlen(line) - 1;
  if (line[nl] == '\n')
    line[nl] = '\0';
  if (mode == 1)
    printf(">> %s\n", line);
  tok = strtok(line, ";");
  while (tok) {
    tok = trimspace(tok);
    if ((pid = fork()) < 0) {
      fprintf(stderr, "Error: Failed to fork\n%s\n", strerror(errno));
      exit(EXIT_FAILURE);
    } else if (pid == 0) {
      close(fd[0]);
      execcmd(tok, fd[1], mode);
      fprintf(stderr, "Error: Exec returned\n%s\n", strerror(errno));
      exit(EXIT_FAILURE);
    } else {
    }
    tok = strtok(NULL, ";");
  }
}


x86 assembly (clang, -O0):
<proccmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11e6 <proccmd+0x46>
mov    -0x8(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x1,-0x14(%rbp)
jne    1200 <proccmd+0x60>
mov    -0x8(%rbp),%rsi
lea    0xe05(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1301 <proccmd+0x161>
mov    -0x20(%rbp),%rdi
call   1030 <trimspace@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    127b <proccmd+0xdb>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xda1(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12de <proccmd+0x13e>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x14(%rbp),%edx
call   1040 <execcmd@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd58(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    12e3 <proccmd+0x143>
jmp    12e8 <proccmd+0x148>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd14(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    1214 <proccmd+0x74>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proccmd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11c3 <proccmd+0x23>
movb   $0x0,-0x1(%rbx,%rax,1)
cmp    $0x1,%r15d
jne    11d8 <proccmd+0x38>
lea    0xe30(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
lea    0xe28(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strtok@plt>
test   %rax,%rax
je     1228 <proccmd+0x88>
lea    0xe14(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rax,%rdi
call   1030 <trimspace@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1233 <proccmd+0x93>
je     1253 <proccmd+0xb3>
xor    %edi,%edi
mov    %rbx,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
jne    1200 <proccmd+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2da6(%rip),%rax
mov    (%rax),%ebx
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdb8(%rip),%rsi
jmp    1288 <proccmd+0xe8>
mov    (%r14),%edi
call   1070 <close@plt>
mov    0x4(%r14),%esi
mov    %rbp,%rdi
mov    %r15d,%edx
call   1040 <execcmd@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd9b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str = [7 x i8] c">> %s\0A\00"
@.str.1 = [2 x i8] c";\00"
@stderr = global i32 0
@.str.2 = [26 x i8] c"Error: Failed to fork\0A%s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [25 x i8] c"Error: Exec returned\0A%s\0A\00"
define void @proccmd(i8* %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, -1
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 10
br i1 %9, label %10, label %11
10: ; preds = %3
store i8 0, i8* %7
br label %11
11: ; preds = %10, %3
%12 = icmp eq i32 %2, 1
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %13, %11
%16 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %47, label %18
18: ; preds = %15, %44
%19 = phi i8* [ %45, %44 ], [ %16, %15 ]
%20 = tail call i8* @trimspace(i8* %19)
%21 = tail call i64 (...) @fork()
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %18
%31 = icmp eq i64 %21, 0
br i1 %31, label %32, label %44
32: ; preds = %30
%33 = load i32, i32* %1
%34 = tail call i32 @close(i32 %33)
%35 = getelementptr inbounds i32, i32* %1, i64 1
%36 = load i32, i32* %35
%37 = tail call i32 @execcmd(i8* %20, i32 %36, i32 %2)
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = tail call i8* @strerror(i32 %39)
%41 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %40)
%42 = load i32, i32* @EXIT_FAILURE
%43 = tail call i32 @exit(i32 %42)
unreachable
44: ; preds = %30
%45 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %18
47: ; preds = %44, %15
ret void
}