#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int WIFSTOPPED(int);
int /*<<< orphan*/ err_sys(char *);
int execl(char *, char *, char *, char *, char *, char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ vfork();
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  if (argc == 1)
    err_sys("Usage:./mysystem command");
  int ret;
  pid_t pid;
  pid = vfork();
  if (pid < 0)
    err_sys("vfork error");
  else if (pid == 0) {
    ret = execl("/bin/sh", "sh", "-c", argv[1], argv[2], (char *)0);
    if (ret < 0) {
      err_sys("execv error");
    }
  } else {
    int status;
    wait(&status);
    if (WIFEXITED(status) != 0)
      printf("end normally:%d\n", WEXITSTATUS(status));
    else
      printf("end unusual:%d\n", WIFSTOPPED(status));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11ac <main+0x2c>
lea    0xe59(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   1070 <vfork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11d3 <main+0x53>
lea    0xe50(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1281 <main+0x101>
cmpq   $0x0,-0x20(%rbp)
jne    122b <main+0xab>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%r8
lea    0xe30(%rip),%rdi
lea    0xe2e(%rip),%rsi
lea    0xe2a(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r9d
call   1090 <execl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1226 <main+0xa6>
lea    0xe0f(%rip),%rdi
call   1030 <err_sys@plt>
jmp    127c <main+0xfc>
lea    -0x24(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x24(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1261 <main+0xe1>
mov    -0x24(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xde5(%rip),%rdi
call   1040 <printf@plt>
jmp    1277 <main+0xf7>
mov    -0x24(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
mov    %eax,%esi
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
jmp    127c <main+0xfc>
jmp    1281 <main+0x101>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jne    1199 <main+0x19>
lea    0xe6c(%rip),%rdi
call   1030 <err_sys@plt>
xor    %eax,%eax
call   1070 <vfork@plt>
test   %rax,%rax
js     11d1 <main+0x51>
je     11df <main+0x5f>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     1216 <main+0x96>
call   1060 <WEXITSTATUS@plt>
lea    0xe6d(%rip),%rdi
jmp    1222 <main+0xa2>
lea    0xe41(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1229 <main+0xa9>
mov    0x8(%rbx),%rcx
mov    0x10(%rbx),%r8
lea    0xe37(%rip),%rdi
lea    0xe35(%rip),%rsi
lea    0xe31(%rip),%rdx
xor    %r9d,%r9d
call   1090 <execl@plt>
test   %eax,%eax
jns    1229 <main+0xa9>
lea    0xe21(%rip),%rdi
call   1030 <err_sys@plt>
jmp    1229 <main+0xa9>
call   1050 <WIFSTOPPED@plt>
lea    0xe2b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Usage:./mysystem command\00"
@.str.1 = [12 x i8] c"vfork error\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [12 x i8] c"execv error\00"
@.str.6 = [17 x i8] c"end normally:%d\0A\00"
@.str.7 = [16 x i8] c"end unusual:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @err_sys(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = call i64 (...) @vfork()
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %50
19: ; preds = %13
%20 = load i64, i64* %7
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %25, i8* %28, i8* null)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32, %22
br label %49
35: ; preds = %19
%36 = call i32 @wait(i32* %8)
%37 = load i32, i32* %8
%38 = call i64 @WIFEXITED(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = load i32, i32* %8
%42 = call i32 @WEXITSTATUS(i32 %41)
%43 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %42)
br label %48
44: ; preds = %35
%45 = load i32, i32* %8
%46 = call i32 @WIFSTOPPED(i32 %45)
%47 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %44, %40
br label %49
49: ; preds = %48, %34
br label %50
50: ; preds = %49, %17
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"Usage:./mysystem command\00"
@.str.1 = [12 x i8] c"vfork error\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [12 x i8] c"execv error\00"
@.str.6 = [17 x i8] c"end normally:%d\0A\00"
@.str.7 = [16 x i8] c"end unusual:%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @err_sys(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = tail call i64 (...) @vfork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %37
12: ; preds = %7
%13 = icmp eq i64 %8, 0
br i1 %13, label %14, label %23
14: ; preds = %12
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %16, i8* %18, i8* null)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %14
%22 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %37
23: ; preds = %12
%24 = bitcast i32* %3 to i8*
%25 = call i32 @wait(i32* %3)
%26 = load i32, i32* %3
%27 = call i64 @WIFEXITED(i32 %26)
%28 = icmp eq i64 %27, 0
%29 = load i32, i32* %3
br i1 %28, label %33, label %30
30: ; preds = %23
%31 = call i32 @WEXITSTATUS(i32 %29)
%32 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %31)
br label %36
33: ; preds = %23
%34 = call i32 @WIFSTOPPED(i32 %29)
%35 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %33, %30
br label %37
37: ; preds = %36, %21, %14, %10
ret i32 0
}