#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 int /*<<< orphan*/ shell_name;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ basename(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ctrlc_exit();
int /*<<< orphan*/ execl(char *, char *, char *, ...);
int fork();
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pr_error(char *);
int /*<<< orphan*/ printc_err(char *, char *);
char *strdup(char *);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int cmd_shellcmd(char **arg) {
  int fd;
  pid_t pid;
  char *sh, *p;
  static char *shell = NULL;
  static char shell_name[16];
  if (shell == NULL) {
    sh = getenv("SHELL");
    if (sh == NULL) {
      sh = "/bin/sh";
    }
    shell = strdup(sh);
    p = strdup(sh);
    if (shell == NULL || p == NULL) {
      pr_error("!: error: strdup");
      return -1;
    }
    memset(shell_name, 0, sizeof(shell_name));
    *shell_name = '-';
    strncpy(shell_name + 1, basename(p), sizeof(shell_name) - 2);
    shell_name[sizeof(shell_name) - 1] = '\0';
    free(p);
  }
  pid = fork();
  if (pid == -1) {
    pr_error("!: error: fork");
    return -1;
  }
  if (pid == 0) {
    ctrlc_exit();
    for (fd = 3; fd < 1024; fd++) {
      (void)close(fd);
    }
    if (arg && *arg[0]) {
      execl(shell, shell_name, "-c", *arg, NULL);
    } else {
      execl(shell, shell_name, NULL);
    }
    printc_err("!: error: execl(\"%s\")", shell);
    pr_error("");
    _exit(1);
  }
  while (waitpid(-1, NULL, 0) != pid) {
    ;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cmd_shellcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,0x2eac(%rip)
jne    12b8 <cmd_shellcmd+0xd8>
lea    0xdff(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1220 <cmd_shellcmd+0x40>
lea    0xdea(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10e0 <strdup@plt>
mov    %rax,0x2e70(%rip)
mov    -0x20(%rbp),%rdi
call   10e0 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,0x2e5b(%rip)
je     1256 <cmd_shellcmd+0x76>
cmpq   $0x0,-0x28(%rbp)
jne    126e <cmd_shellcmd+0x8e>
lea    0xdb1(%rip),%rdi
call   1050 <pr_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d6 <cmd_shellcmd+0x1f6>
lea    0x2e3b(%rip),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <memset@plt>
movb   $0x2d,0x2e28(%rip)
mov    -0x28(%rbp),%rdi
call   10b0 <basename@plt>
mov    %eax,%esi
lea    0x2e16(%rip),%rdi
add    $0x1,%rdi
mov    $0xe,%edx
call   1060 <strncpy@plt>
movb   $0x0,0x2e10(%rip)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12e4 <cmd_shellcmd+0x104>
lea    0xd4c(%rip),%rdi
call   1050 <pr_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d6 <cmd_shellcmd+0x1f6>
cmpl   $0x0,-0x18(%rbp)
jne    13ac <cmd_shellcmd+0x1cc>
mov    $0x0,%al
call   10d0 <ctrlc_exit@plt>
movl   $0x3,-0x14(%rbp)
cmpl   $0x400,-0x14(%rbp)
jge    131f <cmd_shellcmd+0x13f>
mov    -0x14(%rbp),%edi
call   10a0 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12fc <cmd_shellcmd+0x11c>
cmpq   $0x0,-0x10(%rbp)
je     136a <cmd_shellcmd+0x18a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     136a <cmd_shellcmd+0x18a>
mov    0x2d5c(%rip),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0x2d5e(%rip),%rsi
lea    0xcd5(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    $0x0,%al
call   10f0 <execl@plt>
jmp    1383 <cmd_shellcmd+0x1a3>
mov    0x2d2f(%rip),%rdi
lea    0x2d38(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   10f0 <execl@plt>
mov    0x2d16(%rip),%rsi
lea    0xca0(%rip),%rdi
call   1080 <printc_err@plt>
lea    0xca9(%rip),%rdi
call   1050 <pr_error@plt>
mov    $0x1,%edi
call   1070 <_exit@plt>
jmp    13b1 <cmd_shellcmd+0x1d1>
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <waitpid@plt>
cmp    -0x18(%rbp),%eax
je     13cf <cmd_shellcmd+0x1ef>
jmp    13b1 <cmd_shellcmd+0x1d1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_shellcmd>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
cmpq   $0x0,0x2eb1(%rip)
jne    1289 <cmd_shellcmd+0xa9>
lea    0xe04(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xdfb(%rip),%rbx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   10e0 <strdup@plt>
mov    %rax,%rbp
mov    %rax,0x2e7f(%rip)
mov    %rbx,%rdi
call   10e0 <strdup@plt>
lea    0xdde(%rip),%rdi
test   %rbp,%rbp
je     12bd <cmd_shellcmd+0xdd>
mov    %rax,%rbx
test   %rax,%rax
je     12bd <cmd_shellcmd+0xdd>
lea    0x2e64(%rip),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <memset@plt>
movb   $0x2d,0x2e51(%rip)
mov    %rbx,%rdi
call   10b0 <basename@plt>
lea    0x2e43(%rip),%rdi
mov    %eax,%esi
mov    $0xe,%edx
call   1060 <strncpy@plt>
movb   $0x0,0x2e3e(%rip)
mov    %rbx,%rdi
call   1040 <free@plt>
xor    %eax,%eax
call   1100 <fork@plt>
cmp    $0xffffffff,%eax
je     12b6 <cmd_shellcmd+0xd6>
mov    %eax,%ebp
test   %eax,%eax
je     12ce <cmd_shellcmd+0xee>
nopl   0x0(%rax,%rax,1)
xor    %ebx,%ebx
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <waitpid@plt>
cmp    %ebp,%eax
jne    12a0 <cmd_shellcmd+0xc0>
jmp    12c7 <cmd_shellcmd+0xe7>
lea    0xd62(%rip),%rdi
call   1050 <pr_error@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   10d0 <ctrlc_exit@plt>
mov    $0x3,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   10a0 <close@plt>
add    $0x1,%ebx
cmp    $0x400,%ebx
jne    12e0 <cmd_shellcmd+0x100>
test   %r14,%r14
je     12ff <cmd_shellcmd+0x11f>
mov    (%r14),%rcx
cmpb   $0x0,(%rcx)
jne    1318 <cmd_shellcmd+0x138>
mov    0x2d9a(%rip),%rdi
lea    0x2da3(%rip),%rsi
xor    %edx,%edx
xor    %eax,%eax
call   10f0 <execl@plt>
jmp    1337 <cmd_shellcmd+0x157>
mov    0x2d81(%rip),%rdi
lea    0x2d8a(%rip),%rsi
lea    0xd01(%rip),%rdx
xor    %r8d,%r8d
xor    %eax,%eax
call   10f0 <execl@plt>
mov    0x2d62(%rip),%rsi
lea    0xcec(%rip),%rdi
call   1080 <printc_err@plt>
lea    0xcf5(%rip),%rdi
call   1050 <pr_error@plt>
mov    $0x1,%edi
call   1070 <_exit@plt>

LLVM IR (-O0):
@cmd_shellcmd.shell = internal global i8* null
@cmd_shellcmd.shell_name = internal global [16 x i8] zeroinitializer
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [17 x i8] c"!: error: strdup\00"
@.str.3 = [15 x i8] c"!: error: fork\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [22 x i8] c"!: error: execl(\22%s\22)\00"
@.str.6 = [1 x i8] zeroinitializer
define i32 @cmd_shellcmd(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8** %0, i8*** %3
%8 = load i8*, i8** @cmd_shellcmd.shell
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %34
10: ; preds = %1
%11 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %10
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %15
15: ; preds = %14, %10
%16 = load i8*, i8** %6
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** @cmd_shellcmd.shell
%18 = load i8*, i8** %6
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %7
%20 = load i8*, i8** @cmd_shellcmd.shell
%21 = icmp eq i8* %20, null
br i1 %21, label %25, label %22
22: ; preds = %15
%23 = load i8*, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %22, %15
%26 = call i32 @pr_error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %84
27: ; preds = %22
%28 = call i32 @memset(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 0), i32 0, i32 16)
store i8 45, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 0)
%29 = load i8*, i8** %7
%30 = call i32 @basename(i8* %29)
%31 = call i32 @strncpy(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 1), i32 %30, i32 14)
store i8 0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 15)
%32 = load i8*, i8** %7
%33 = call i32 @free(i8* %32)
br label %34
34: ; preds = %27, %1
%35 = call i32 (...) @fork()
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @pr_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %84
40: ; preds = %34
%41 = load i32, i32* %5
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %77
43: ; preds = %40
%44 = call i32 (...) @ctrlc_exit()
store i32 3, i32* %4
br label %45
45: ; preds = %51, %43
%46 = load i32, i32* %4
%47 = icmp slt i32 %46, 1024
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48
%52 = load i32, i32* %4
%53 = add nsw i32 %52, 1
store i32 %53, i32* %4
br label %45
54: ; preds = %45
%55 = load i8**, i8*** %3
%56 = icmp ne i8** %55, null
br i1 %56, label %57, label %69
57: ; preds = %54
%58 = load i8**, i8*** %3
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %57
%65 = load i8*, i8** @cmd_shellcmd.shell
%66 = load i8**, i8*** %3
%67 = load i8*, i8** %66
%68 = call i32 (i8*, i8*, i8*, ...) @execl(i8* %65, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %67, i8* null)
br label %72
69: ; preds = %57, %54
%70 = load i8*, i8** @cmd_shellcmd.shell
%71 = call i32 (i8*, i8*, i8*, ...) @execl(i8* %70, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 0), i8* null)
br label %72
72: ; preds = %69, %64
%73 = load i8*, i8** @cmd_shellcmd.shell
%74 = call i32 @printc_err(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %73)
%75 = call i32 @pr_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
%76 = call i32 @_exit(i32 1)
unreachable
77: ; preds = %40
br label %78
78: ; preds = %82, %77
%79 = call i32 @waitpid(i32 -1, i32* null, i32 0)
%80 = load i32, i32* %5
%81 = icmp ne i32 %79, %80
br i1 %81, label %82, label %83
82: ; preds = %78
br label %78
83: ; preds = %78
store i32 0, i32* %2
br label %84
84: ; preds = %83, %38, %25
%85 = load i32, i32* %2
ret i32 %85
}

LLVM IR (-O2):
@cmd_shellcmd.shell = internal unnamed_addr global i8* null
@cmd_shellcmd.shell_name = internal global [16 x i8] zeroinitializer
@.str = [6 x i8] c"SHELL\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [17 x i8] c"!: error: strdup\00"
@.str.3 = [15 x i8] c"!: error: fork\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [22 x i8] c"!: error: execl(\22%s\22)\00"
@.str.6 = [1 x i8] zeroinitializer
define i32 @cmd_shellcmd(i8** %0) {
%2 = load i8*, i8** @cmd_shellcmd.shell
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %18
4: ; preds = %1
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
%7 = select i1 %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %5
%8 = tail call i8* @strdup(i8* %7)
store i8* %8, i8** @cmd_shellcmd.shell
%9 = tail call i8* @strdup(i8* %7)
%10 = icmp eq i8* %8, null
%11 = icmp eq i8* %9, null
%12 = select i1 %10, i1 true, i1 %11
br i1 %12, label %47, label %13
13: ; preds = %4
%14 = tail call i32 @memset(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 0), i32 0, i32 16)
store i8 45, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 0)
%15 = tail call i32 @basename(i8* %9)
%16 = tail call i32 @strncpy(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 1), i32 %15, i32 14)
store i8 0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 15)
%17 = tail call i32 @free(i8* %9)
br label %18
18: ; preds = %13, %1
%19 = tail call i32 (...) @fork()
switch i32 %19, label %44 [
i32 -1, label %47
i32 0, label %20
]
20: ; preds = %18
%21 = tail call i32 (...) @ctrlc_exit()
br label %22
22: ; preds = %20, %22
%23 = phi i32 [ 3, %20 ], [ %25, %22 ]
%24 = tail call i32 @close(i32 %23)
%25 = add nuw nsw i32 %23, 1
%26 = icmp eq i32 %25, 1024
br i1 %26, label %27, label %22
27: ; preds = %22
%28 = icmp eq i8** %0, null
br i1 %28, label %36, label %29
29: ; preds = %27
%30 = load i8*, i8** %0
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = load i8*, i8** @cmd_shellcmd.shell
%35 = tail call i32 (i8*, i8*, i8*, ...) @execl(i8* %34, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %30, i8* null)
br label %39
36: ; preds = %29, %27
%37 = load i8*, i8** @cmd_shellcmd.shell
%38 = tail call i32 (i8*, i8*, i8*, ...) @execl(i8* %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @cmd_shellcmd.shell_name, i64 0, i64 0), i8* null)
br label %39
39: ; preds = %36, %33
%40 = load i8*, i8** @cmd_shellcmd.shell
%41 = tail call i32 @printc_err(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %40)
%42 = tail call i32 @pr_error(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
%43 = tail call i32 @_exit(i32 1)
unreachable
44: ; preds = %18, %44
%45 = tail call i32 @waitpid(i32 -1, i32* null, i32 0)
%46 = icmp eq i32 %45, %19
br i1 %46, label %50, label %44
47: ; preds = %18, %4
%48 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), %4 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), %18 ]
%49 = tail call i32 @pr_error(i8* %48)
br label %50
50: ; preds = %44, %47
%51 = phi i32 [ -1, %47 ], [ 0, %44 ]
ret i32 %51
}