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

/* Variables and functions */
int /*<<< orphan*/ E2BIG;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execv(char *, char **);
int fork();
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strtok(char *, char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int system(const char *cmd) {
  char tmp[2048];
  char *argv[128 + 1];
  int nargs = 0;
  char *s;
  int pid, status;
  if (strlen(cmd) >= sizeof(tmp)) {
    errno = E2BIG;
    return -1;
  }
  strcpy(tmp, cmd);
  for (s = strtok(tmp, " \t"); s; s = strtok(NULL, " \t")) {
    if (nargs < 128) {
      argv[nargs++] = s;
    } else {
      errno = E2BIG;
      return 1;
    }
  }
  argv[nargs] = NULL;
  pid = fork();
  switch (pid) {
  case -1:
    return -1;
  case 0:
    execv(argv[0], argv);
    _exit(255);
  default:
    waitpid(pid, &status, 0);
    return status;
  }
}


x86 assembly (clang, -O0):
<system>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0xc24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
cmp    $0x800,%rax
jb     11be <system+0x4e>
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
mov    0x2e28(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <system+0x18f>
lea    -0x810(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x810(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0xc30(%rbp)
cmpq   $0x0,-0xc30(%rbp)
je     126c <system+0xfc>
cmpl   $0x80,-0xc24(%rbp)
jge    122d <system+0xbd>
mov    -0xc30(%rbp),%rcx
mov    -0xc24(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0xc24(%rbp)
cltq
mov    %rcx,-0xc20(%rbp,%rax,8)
ff
jmp    124b <system+0xdb>
mov    0x2db4(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9b(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12ff <system+0x18f>
jmp    1250 <system+0xe0>
xor    %eax,%eax
mov    %eax,%edi
lea    0xda5(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0xc30(%rbp)
jmp    11e8 <system+0x78>
movslq -0xc24(%rbp),%rax
movq   $0x0,-0xc20(%rbp,%rax,8)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0xc34(%rbp)
mov    -0xc34(%rbp),%eax
mov    %eax,-0xc3c(%rbp)
sub    $0xffffffff,%eax
je     12b9 <system+0x149>
jmp    12a6 <system+0x136>
mov    -0xc3c(%rbp),%eax
test   %eax,%eax
je     12c5 <system+0x155>
jmp    12e2 <system+0x172>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ff <system+0x18f>
mov    -0xc20(%rbp),%rdi
lea    -0xc20(%rbp),%rsi
call   1080 <execv@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>
mov    -0xc34(%rbp),%edi
lea    -0xc38(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    -0xc38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<system>:
push   %r14
push   %rbx
sub    $0xc28,%rsp
mov    %rdi,%rbx
call   1050 <strlen@plt>
cmp    $0x800,%eax
jb     11a5 <system+0x35>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    0x2e3f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1245 <system+0xd5>
lea    0x420(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe41(%rip),%rsi
mov    %r14,%rdi
call   1070 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1201 <system+0x91>
lea    0xe2b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x80,%rbx
je     122e <system+0xbe>
mov    %rax,0x10(%rsp,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r14,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
jne    11e0 <system+0x70>
movq   $0x0,0x10(%rsp,%rbx,8)
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     1245 <system+0xd5>
test   %eax,%eax
je     1250 <system+0xe0>
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    0xc(%rsp),%eax
jmp    1245 <system+0xd5>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    0x2d9a(%rip),%rcx
mov    %eax,(%rcx)
mov    $0x1,%eax
add    $0xc28,%rsp
pop    %rbx
pop    %r14
ret
mov    0x10(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1080 <execv@plt>
mov    $0xff,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@E2BIG = global i32 0
@errno = global i32 0
@.str = [3 x i8] c" \09\00"
define i32 @system(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [2048 x i8]
%5 = alloca [129 x i8*]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = sext i32 %11 to i64
%13 = icmp uge i64 %12, 2048
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i32, i32* @E2BIG
store i32 %15, i32* @errno
store i32 -1, i32* %2
br label %56
16: ; preds = %1
%17 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%18 = load i8*, i8** %3
%19 = call i32 @strcpy(i8* %17, i8* %18)
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%21 = call i8* @strtok(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %21, i8** %7
br label %22
22: ; preds = %37, %16
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %39
25: ; preds = %22
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 128
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i8*, i8** %7
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds [129 x i8*], [129 x i8*]* %5, i64 0, i64 %32
store i8* %29, i8** %33
br label %36
34: ; preds = %25
%35 = load i32, i32* @E2BIG
store i32 %35, i32* @errno
store i32 1, i32* %2
br label %56
36: ; preds = %28
br label %37
37: ; preds = %36
%38 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %38, i8** %7
br label %22
39: ; preds = %22
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [129 x i8*], [129 x i8*]* %5, i64 0, i64 %41
store i8* null, i8** %42
%43 = call i32 (...) @fork()
store i32 %43, i32* %8
%44 = load i32, i32* %8
switch i32 %44, label %52 [
i32 -1, label %45
i32 0, label %46
]
45: ; preds = %39
store i32 -1, i32* %2
br label %56
46: ; preds = %39
%47 = getelementptr inbounds [129 x i8*], [129 x i8*]* %5, i64 0, i64 0
%48 = load i8*, i8** %47
%49 = getelementptr inbounds [129 x i8*], [129 x i8*]* %5, i64 0, i64 0
%50 = call i32 @execv(i8* %48, i8** %49)
%51 = call i32 @_exit(i32 255)
unreachable
52: ; preds = %39
%53 = load i32, i32* %8
%54 = call i32 @waitpid(i32 %53, i32* %9, i32 0)
%55 = load i32, i32* %9
store i32 %55, i32* %2
br label %56
56: ; preds = %52, %45, %34, %14
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
@E2BIG = global i32 0
@errno = global i32 0
@.str = [3 x i8] c" \09\00"
define i32 @system(i8* %0) {
%2 = alloca [2048 x i8]
%3 = alloca [129 x i8*]
%4 = alloca i32
%5 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%6 = bitcast [129 x i8*]* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = tail call i32 @strlen(i8* %0)
%9 = icmp ugt i32 %8, 2047
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32, i32* @E2BIG
store i32 %11, i32* @errno
br label %41
12: ; preds = %1
%13 = call i32 @strcpy(i8* %5, i8* %0)
%14 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %29, label %16
16: ; preds = %12, %20
%17 = phi i64 [ %21, %20 ], [ 0, %12 ]
%18 = phi i8* [ %23, %20 ], [ %14, %12 ]
%19 = icmp eq i64 %17, 128
br i1 %19, label %25, label %20
20: ; preds = %16
%21 = add nuw nsw i64 %17, 1
%22 = getelementptr inbounds [129 x i8*], [129 x i8*]* %3, i64 0, i64 %17
store i8* %18, i8** %22
%23 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i8* %23, null
br i1 %24, label %27, label %16
25: ; preds = %16
%26 = load i32, i32* @E2BIG
store i32 %26, i32* @errno
br label %41
27: ; preds = %20
%28 = and i64 %21, 4294967295
br label %29
29: ; preds = %27, %12
%30 = phi i64 [ 0, %12 ], [ %28, %27 ]
%31 = getelementptr inbounds [129 x i8*], [129 x i8*]* %3, i64 0, i64 %30
store i8* null, i8** %31
%32 = call i32 (...) @fork()
switch i32 %32, label %38 [
i32 -1, label %41
i32 0, label %33
]
33: ; preds = %29
%34 = getelementptr inbounds [129 x i8*], [129 x i8*]* %3, i64 0, i64 0
%35 = load i8*, i8** %34
%36 = call i32 @execv(i8* %35, i8** %34)
%37 = call i32 @_exit(i32 255)
unreachable
38: ; preds = %29
%39 = call i32 @waitpid(i32 %32, i32* %4, i32 0)
%40 = load i32, i32* %4
br label %41
41: ; preds = %29, %38, %25, %10
%42 = phi i32 [ -1, %10 ], [ 1, %25 ], [ %40, %38 ], [ %32, %29 ]
ret i32 %42
}