#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 /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  pid_t pid;
  char *argv[10];
  char *cmd = NULL;
  cmd = (char *)malloc(sizeof(char) * 32);
  if (!cmd) {
    perror("Malooc failed!\n");
    exit(1);
  };
  printf("Enter the command to execute: ");
  fgets(cmd, 32, stdin);
  if (!cmd) {
    perror("Malooc failed!\n");
    exit(1);
  };
  *(cmd + strlen(cmd) - 1) = '\0';
  printf("Output of %s command:\n", cmd);
  strcpy(argv[0], cmd);
  argv[1] = NULL;
  pid = fork();
  if (pid == 0) {
    execvp(cmd, argv);
  } else if (pid > 0) {
    wait(NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x68(%rbp)
mov    $0x20,%edi
call   1070 <malloc@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    11e6 <main+0x46>
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x68(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
mov    $0x20,%esi
call   1060 <fgets@plt>
cmpq   $0x0,-0x68(%rbp)
jne    122c <main+0x8c>
lea    0xde3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x68(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
movslq %ecx,%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x68(%rbp),%rsi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rsi
call   1030 <strcpy@plt>
movq   $0x0,-0x58(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    129a <main+0xfa>
mov    -0x68(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1090 <execvp@plt>
jmp    12b3 <main+0x113>
cmpq   $0x0,-0x10(%rbp)
jle    12ae <main+0x10e>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
jmp    12b3 <main+0x113>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x50,%rsp
mov    $0x20,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     123e <main+0x9e>
mov    %rax,%rbx
lea    0xe4e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rbx,%rax,1)
lea    0xe3a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     122b <main+0x8b>
jle    1236 <main+0x96>
xor    %edi,%edi
call   10b0 <wait@plt>
jmp    1236 <main+0x96>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <execvp@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xdbb(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Malooc failed!\0A\00"
@.str.1 = [31 x i8] c"Enter the command to execute: \00"
@stdin = global i32 0
@.str.2 = [23 x i8] c"Output of %s command:\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [10 x i8*]
%4 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %4
%5 = call i64 @malloc(i32 32)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%14 = load i8*, i8** %4
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* %14, i32 32, i32 %15)
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = load i8*, i8** %4
%24 = load i8*, i8** %4
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 -1
store i8 0, i8* %28
%29 = load i8*, i8** %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 0
%32 = load i8*, i8** %31
%33 = load i8*, i8** %4
%34 = call i32 @strcpy(i8* %32, i8* %33)
%35 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 1
store i8* null, i8** %35
%36 = call i64 (...) @fork()
store i64 %36, i64* %2
%37 = load i64, i64* %2
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %22
%40 = load i8*, i8** %4
%41 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 0
%42 = call i32 @execvp(i8* %40, i8** %41)
br label %49
43: ; preds = %22
%44 = load i64, i64* %2
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @wait(i32* null)
br label %48
48: ; preds = %46, %43
br label %49
49: ; preds = %48, %39
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"Malooc failed!\0A\00"
@.str.1 = [31 x i8] c"Enter the command to execute: \00"
@stdin = global i32 0
@.str.2 = [23 x i8] c"Output of %s command:\0A\00"
define i32 @main() {
%1 = alloca [10 x i8*]
%2 = bitcast [10 x i8*]* %1 to i8*
%3 = tail call i64 @malloc(i32 32)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @stdin
%12 = tail call i32 @fgets(i8* %4, i32 32, i32 %11)
%13 = tail call i32 @strlen(i8* %4)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %4, i64 %14
%16 = getelementptr inbounds i8, i8* %15, i64 -1
store i8 0, i8* %16
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%18 = getelementptr inbounds [10 x i8*], [10 x i8*]* %1, i64 0, i64 0
%19 = load i8*, i8** %18
%20 = tail call i32 @strcpy(i8* %19, i8* %4)
%21 = getelementptr inbounds [10 x i8*], [10 x i8*]* %1, i64 0, i64 1
store i8* null, i8** %21
%22 = tail call i64 (...) @fork()
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %9
%25 = call i32 @execvp(i8* %4, i8** %18)
br label %30
26: ; preds = %9
%27 = icmp sgt i64 %22, 0
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = tail call i32 @wait(i32* null)
br label %30
30: ; preds = %26, %28, %24
ret i32 0
}