#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int const);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage:command filename\n");
    exit(EXIT_FAILURE);
  }
  static const int BUFFSIZE = 255;
  int fd;
  pid_t cpid;
  char *filename = argv[argc - 1];
  char buffer[BUFFSIZE];
  char command[BUFFSIZE];
  fd = open(filename, O_RDONLY);
  cpid = fork();
  if (cpid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (cpid == 0) {
    int rdbyte = read(fd, buffer, BUFFSIZE);
    if (rdbyte == -1) {
      perror("read");
      exit(EXIT_FAILURE);
    }
    buffer[rdbyte] = '\0';
    strcpy(command, argv[1]);
    strcat(command, " ");
    strcat(command, buffer);
    printf("command: %s\n", command);
    if (execlp("/bin/sh", "sh", "-c", command, NULL) == -1) {
      perror("execl");
      exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
  } else {
    wait(NULL);
    close(fd);
    exit(EXIT_SUCCESS);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ec <main+0x3c>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
add    $0xffffffff,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rsp,%rax
add    $0xffffffffffffff00,%rax
mov    %rax,-0x40(%rbp)
mov    %rax,%rsp
mov    %rsp,%rax
add    $0xffffffffffffff00,%rax
mov    %rax,-0x38(%rbp)
mov    %rax,%rsp
mov    -0x20(%rbp),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    126b <main+0xbb>
lea    0xdc0(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    134d <main+0x19d>
mov    -0x40(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12ad <main+0xfd>
lea    0xd83(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rdi
lea    0xd4d(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdi
call   1090 <strcat@plt>
mov    -0x38(%rbp),%rsi
lea    0xd32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rcx
lea    0xd2d(%rip),%rdi
lea    0xd2b(%rip),%rsi
lea    0xd27(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   10b0 <execlp@plt>
cmp    $0xffffffff,%eax
jne    133f <main+0x18f>
lea    0xd10(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2c8a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2c6b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jg     11e1 <main+0x21>
lea    0xe53(%rip),%rdi
call   1040 <puts@plt>
jmp    12de <main+0x11e>
mov    %rsi,%r15
add    $0xffffffff,%edi
mov    (%rsi,%rdi,8),%rdi
mov    %rsp,%rbx
add    $0xffffffffffffff00,%rbx
mov    %rbx,%rsp
mov    %rsp,%r14
add    $0xffffffffffffff00,%r14
mov    %r14,%rsp
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1232 <main+0x72>
cmp    $0xffffffff,%eax
jne    1253 <main+0x93>
lea    0xdd3(%rip),%rdi
jmp    12d9 <main+0x119>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
jne    1270 <main+0xb0>
lea    0xdb7(%rip),%rdi
jmp    12d9 <main+0x119>
xor    %edi,%edi
call   10d0 <wait@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    0x8(%r15),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0xd81(%rip),%rsi
mov    %r14,%rdi
call   10a0 <strcat@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
lea    0xd69(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd65(%rip),%rdi
lea    0xd63(%rip),%rsi
lea    0xd5f(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   10c0 <execlp@plt>
cmp    $0xffffffff,%eax
jne    1262 <main+0xa2>
lea    0xd4b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Usage:command filename\0A\00"
@EXIT_FAILURE = global i32 0
@main.BUFFSIZE = internal constant i32 255
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [13 x i8] c"command: %s\0A\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [3 x i8] c"sh\00"
@.str.7 = [3 x i8] c"-c\00"
@.str.8 = [6 x i8] c"execl\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = load i32, i32* %4
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8*, i8** %18, i64 %21
%23 = load i8*, i8** %22
store i8* %23, i8** %8
%24 = call i8* @llvm.stacksave()
store i8* %24, i8** %9
%25 = alloca i8, i64 255
%26 = alloca i8, i64 255
%27 = load i8*, i8** %8
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %6
%30 = call i32 (...) @fork()
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %17
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %17
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %69
40: ; preds = %37
%41 = load i32, i32* %6
%42 = call i32 @read(i32 %41, i8* %25, i32 255)
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %40
%50 = load i32, i32* %10
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %25, i64 %51
store i8 0, i8* %52
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = call i32 @strcpy(i8* %26, i8* %55)
%57 = call i32 @strcat(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%58 = call i32 @strcat(i8* %26, i8* %25)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%60 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %26, i32* null)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %66
62: ; preds = %49
%63 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %49
%67 = load i32, i32* @EXIT_SUCCESS
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %37
%70 = call i32 @wait(i32* null)
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* @EXIT_SUCCESS
%74 = call i32 @exit(i32 %73)
unreachable
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [13 x i8] c"command: %s\0A\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [3 x i8] c"sh\00"
@.str.7 = [3 x i8] c"-c\00"
@.str.8 = [6 x i8] c"execl\00"
@EXIT_SUCCESS = global i32 0
@str = [23 x i8] c"Usage:command filename\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2
%9 = add nsw i32 %0, -1
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds i8*, i8** %1, i64 %10
%12 = load i8*, i8** %11
%13 = alloca [255 x i8]
%14 = alloca [255 x i8]
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %14, i64 0, i64 0
%16 = getelementptr inbounds [255 x i8], [255 x i8]* %13, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %12, i32 %17)
%19 = tail call i32 (...) @fork()
switch i32 %19, label %49 [
i32 -1, label %20
i32 0, label %24
]
20: ; preds = %8
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %8
%25 = call i32 @read(i32 %18, i8* %16, i32 255)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %24
%32 = sext i32 %25 to i64
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %13, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds i8*, i8** %1, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @strcpy(i8* %15, i8* %35)
%37 = call i32 @strcat(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @strcat(i8* %15, i8* %16)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %15)
%40 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %15, i32* null)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %31
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %31
%47 = load i32, i32* @EXIT_SUCCESS
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %8
%50 = tail call i32 @wait(i32* null)
%51 = tail call i32 @close(i32 %18)
%52 = load i32, i32* @EXIT_SUCCESS
%53 = tail call i32 @exit(i32 %52)
unreachable
}