#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*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int /*<<< orphan*/ WUNTRACED;
int execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, scalar_t__);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int psh_run(char **args) {
  pid_t pid;
  int status;
  int i;
  int concurrent = 0;
  i = 0;
  while (args[i])
    i++;
  if (strcmp("&", args[i - 1]) == 0) {
    concurrent = 1;
    args[i - 1] = NULL;
  }
  pid = fork();
  if (pid < 0) {
  } else if (pid == 0) {
    if (execvp(args[0], args) == -1) {
      perror("psh");
    }
    exit(EXIT_FAILURE);
  } else {
    if (concurrent) {
      printf("[%d]\n", pid);
      return 0;
    }
    do {
      waitpid(pid, &status, WUNTRACED);
    } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  }
  return 0;
}


x86 assembly (clang, -O0):
<psh_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11cb <psh_run+0x3b>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11aa <psh_run+0x1a>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe1d(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    120e <psh_run+0x7e>
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1229 <psh_run+0x99>
jmp    12e8 <psh_run+0x158>
cmpq   $0x0,-0x18(%rbp)
jne    1267 <psh_run+0xd7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1259 <psh_run+0xc9>
lea    0xdae(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
je     128d <psh_run+0xfd>
mov    -0x18(%rbp),%rsi
lea    0xd8a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ef <psh_run+0x15f>
jmp    1292 <psh_run+0x102>
mov    -0x18(%rbp),%rdi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   1060 <waitpid@plt>
mov    -0x1c(%rbp),%edi
call   1090 <WIFEXITED@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
jne    12d3 <psh_run+0x143>
mov    -0x1c(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1292 <psh_run+0x102>
jmp    12e3 <psh_run+0x153>
jmp    12e8 <psh_run+0x158>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psh_run>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
movabs $0xfffffffe00000000,%rbx
ff ff ff
movabs $0x100000000,%rax
mov    %rdi,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %rax,%rbx
cmpq   $0x0,(%rcx)
lea    0x8(%rcx),%rcx
jne    11c0 <psh_run+0x30>
mov    %rbx,%rax
sar    $0x1d,%rax
mov    (%r14,%rax,1),%rsi
lea    0xe21(%rip),%rdi
call   1050 <strcmp@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11f8 <psh_run+0x68>
sar    $0x20,%rbx
movq   $0x0,(%r14,%rbx,8)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1259 <psh_run+0xc9>
je     1265 <psh_run+0xd5>
mov    %rax,%rbx
test   %r15,%r15
je     124a <psh_run+0xba>
mov    0x2dcb(%rip),%r15
lea    0xc(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
jne    1259 <psh_run+0xc9>
mov    0xc(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %eax,%eax
je     1220 <psh_run+0x90>
jmp    1259 <psh_run+0xc9>
lea    0xdb5(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%r14),%rdi
mov    %r14,%rsi
call   1080 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1281 <psh_run+0xf1>
lea    0xd86(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"&\00"
@.str.1 = [4 x i8] c"psh\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [6 x i8] c"[%d]\0A\00"
@WUNTRACED = global i32 0
define i32 @psh_run(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8** %0, i8*** %3
store i32 0, i32* %7
store i32 0, i32* %6
br label %8
8: ; preds = %15, %1
%9 = load i8**, i8*** %3
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8*, i8** %9, i64 %11
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
store i32 %17, i32* %6
br label %8
18: ; preds = %8
%19 = load i8**, i8*** %3
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %19, i64 %22
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %18
store i32 1, i32* %7
%28 = load i8**, i8*** %3
%29 = load i32, i32* %6
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
store i8* null, i8** %32
br label %33
33: ; preds = %27, %18
%34 = call i64 (...) @fork()
store i64 %34, i64* %4
%35 = load i64, i64* %4
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
br label %77
38: ; preds = %33
%39 = load i64, i64* %4
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %53
41: ; preds = %38
%42 = load i8**, i8*** %3
%43 = getelementptr inbounds i8*, i8** %42, i64 0
%44 = load i8*, i8** %43
%45 = load i8**, i8*** %3
%46 = call i32 @execvp(i8* %44, i8** %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %41
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %38
%54 = load i32, i32* %7
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i64, i64* %4
%58 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %57)
store i32 0, i32* %2
br label %78
59: ; preds = %53
br label %60
60: ; preds = %73, %59
%61 = load i64, i64* %4
%62 = load i32, i32* @WUNTRACED
%63 = call i32 @waitpid(i64 %61, i32* %5, i32 %62)
br label %64
64: ; preds = %60
%65 = load i32, i32* %5
%66 = call i32 @WIFEXITED(i32 %65)
%67 = icmp ne i32 %66, 0
br i1 %67, label %73, label %68
68: ; preds = %64
%69 = load i32, i32* %5
%70 = call i32 @WIFSIGNALED(i32 %69)
%71 = icmp ne i32 %70, 0
%72 = xor i1 %71, true
br label %73
73: ; preds = %68, %64
%74 = phi i1 [ false, %64 ], [ %72, %68 ]
br i1 %74, label %60, label %75
75: ; preds = %73
br label %76
76: ; preds = %75
br label %77
77: ; preds = %76, %37
store i32 0, i32* %2
br label %78
78: ; preds = %77, %56
%79 = load i32, i32* %2
ret i32 %79
}

LLVM IR (-O2):
@.str = [2 x i8] c"&\00"
@.str.1 = [4 x i8] c"psh\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [6 x i8] c"[%d]\0A\00"
@WUNTRACED = global i32 0
define i32 @psh_run(i8** %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %4, %1
%5 = phi i64 [ %9, %4 ], [ 0, %1 ]
%6 = getelementptr inbounds i8*, i8** %0, i64 %5
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
%9 = add nuw i64 %5, 1
br i1 %8, label %10, label %4
10: ; preds = %4
%11 = shl i64 %5, 32
%12 = add i64 %11, -4294967296
%13 = ashr exact i64 %12, 32
%14 = getelementptr inbounds i8*, i8** %0, i64 %13
%15 = load i8*, i8** %14
%16 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %10
store i8* null, i8** %14
br label %19
19: ; preds = %18, %10
%20 = tail call i64 (...) @fork()
%21 = icmp slt i64 %20, 0
br i1 %21, label %46, label %22
22: ; preds = %19
%23 = icmp eq i64 %20, 0
br i1 %23, label %24, label %33
24: ; preds = %22
%25 = load i8*, i8** %0
%26 = tail call i32 @execvp(i8* %25, i8** %0)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %22
br i1 %17, label %34, label %36
34: ; preds = %33
%35 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %20)
br label %46
36: ; preds = %33, %42
%37 = load i32, i32* @WUNTRACED
%38 = call i32 @waitpid(i64 %20, i32* %2, i32 %37)
%39 = load i32, i32* %2
%40 = call i32 @WIFEXITED(i32 %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = load i32, i32* %2
%44 = call i32 @WIFSIGNALED(i32 %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %36, label %46
46: ; preds = %42, %36, %19, %34
ret i32 0
}