#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 */

/* Variables and functions */
int /*<<< orphan*/ E2BIG;
int buildargv(char *, char **, int);
int cmd_exec(int, char **);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *);
int cmd_system(char *s) {
  char *argv[20];
  int argc = buildargv(s, argv, 20);
  if (argc < 0) {
    printf("too many arguments\n");
    errno = E2BIG;
    return -1;
  }
  for (int i = 0; i < argc; i++) {
    if (argv[i][0] == '#') {
      argv[i] = NULL;
      argc = i;
      break;
    }
  }
  return cmd_exec(argc, argv);
}


x86 assembly (clang, -O0):
<cmd_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
mov    $0x14,%edx
call   1030 <buildargv@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jge    1191 <cmd_system+0x61>
lea    0xe92(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e55(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <cmd_system+0xea>
movl   $0x0,-0xb8(%rbp)
mov    -0xb8(%rbp),%eax
cmp    -0xb4(%rbp),%eax
jge    1205 <cmd_system+0xd5>
movslq -0xb8(%rbp),%rax
mov    -0xb0(%rbp,%rax,8),%rax
ff
movsbl (%rax),%eax
cmp    $0x23,%eax
jne    11ec <cmd_system+0xbc>
movslq -0xb8(%rbp),%rax
movq   $0x0,-0xb0(%rbp,%rax,8)
mov    -0xb8(%rbp),%eax
mov    %eax,-0xb4(%rbp)
jmp    1205 <cmd_system+0xd5>
jmp    11f1 <cmd_system+0xc1>
mov    -0xb8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb8(%rbp)
jmp    119b <cmd_system+0x6b>
mov    -0xb4(%rbp),%edi
lea    -0xb0(%rbp),%rsi
call   1040 <cmd_exec@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_system>:
sub    $0xa8,%rsp
mov    %rsp,%rsi
mov    $0x14,%edx
call   1030 <buildargv@plt>
test   %eax,%eax
js     1176 <cmd_system+0x46>
mov    %eax,%eax
xor    %edi,%edi
test   %rax,%rax
je     11a9 <cmd_system+0x79>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rsp,%rdi,8),%rcx
cmpb   $0x23,(%rcx)
je     11a1 <cmd_system+0x71>
add    $0x1,%rdi
cmp    %rdi,%rax
jne    1160 <cmd_system+0x30>
mov    %eax,%edi
jmp    11a9 <cmd_system+0x79>
lea    0xe83(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    0x2e46(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0xa8,%rsp
ret
movq   $0x0,(%rsp,%rdi,8)
mov    %rsp,%rsi
call   1050 <cmd_exec@plt>
add    $0xa8,%rsp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"too many arguments\0A\00"
@E2BIG = global i32 0
@errno = global i32 0
define i32 @cmd_system(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [20 x i8*]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 0
%9 = call i32 @buildargv(i8* %7, i8** %8, i32 20)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @E2BIG
store i32 %14, i32* @errno
store i32 -1, i32* %2
br label %42
15: ; preds = %1
store i32 0, i32* %6
br label %16
16: ; preds = %35, %15
%17 = load i32, i32* %6
%18 = load i32, i32* %5
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %38
20: ; preds = %16
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 %22
%24 = load i8*, i8** %23
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 35
br i1 %28, label %29, label %34
29: ; preds = %20
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 %31
store i8* null, i8** %32
%33 = load i32, i32* %6
store i32 %33, i32* %5
br label %38
34: ; preds = %20
br label %35
35: ; preds = %34
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %16
38: ; preds = %29, %16
%39 = load i32, i32* %5
%40 = getelementptr inbounds [20 x i8*], [20 x i8*]* %4, i64 0, i64 0
%41 = call i32 @cmd_exec(i32 %39, i8** %40)
store i32 %41, i32* %2
br label %42
42: ; preds = %38, %12
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@E2BIG = global i32 0
@errno = global i32 0
@str = [19 x i8] c"too many arguments\00"
define i32 @cmd_system(i8* %0) {
%2 = alloca [20 x i8*]
%3 = bitcast [20 x i8*]* %2 to i8*
%4 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 0
%5 = call i32 @buildargv(i8* %0, i8** %4, i32 20)
%6 = icmp slt i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = icmp eq i32 %5, 0
br i1 %8, label %27, label %9
9: ; preds = %7
%10 = zext i32 %5 to i64
br label %14
11: ; preds = %1
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* @E2BIG
store i32 %13, i32* @errno
br label %30
14: ; preds = %9, %24
%15 = phi i64 [ 0, %9 ], [ %25, %24 ]
%16 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %15
%17 = load i8*, i8** %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 35
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = trunc i64 %15 to i32
%22 = and i64 %15, 4294967295
%23 = getelementptr inbounds [20 x i8*], [20 x i8*]* %2, i64 0, i64 %22
store i8* null, i8** %23
br label %27
24: ; preds = %14
%25 = add nuw nsw i64 %15, 1
%26 = icmp eq i64 %25, %10
br i1 %26, label %27, label %14
27: ; preds = %24, %7, %20
%28 = phi i32 [ %21, %20 ], [ 0, %7 ], [ %5, %24 ]
%29 = call i32 @cmd_exec(i32 %28, i8** %4)
br label %30
30: ; preds = %27, %11
%31 = phi i32 [ -1, %11 ], [ %29, %27 ]
ret i32 %31
}