#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*/ execute_command(char *, char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char const *);
int main(int argc, char *argv[]) {
  pid_t pid;
  bool en_background = false;
  int n_params = argc;
  const char *str_bg = "bg";
  char *str_command;
  char str_params[200];
  if (argc < 2) {
    printf("Modo de uso: %s <programa> [opciones] [bg]\n\n", argv[0]);
    exit(1);
  } else {
    if (strcmp(argv[argc - 1], str_bg) == 0) {
      en_background = true;
      n_params--;
    }
    str_command = argv[1];
    for (int i = 2; i < n_params; i++) {
      strcat(str_params, argv[i]);
      strcat(str_params, " ");
    }
    if (en_background) {
      printf("%s se ejecutara en background\n", str_command);
      if ((pid = fork()) < 0)
        return (-1);
      else if (pid != 0)
        exit(0);
      execute_command(str_command, str_params);
    } else {
      printf("%s se ejecutara en foreground\n", str_command);
      ;
      execute_command(str_command, str_params);
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    0xe73(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x58>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11ec <main+0x8c>
movl   $0x1,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
movl   $0x2,-0x104(%rbp)
mov    -0x104(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1253 <main+0xf3>
lea    -0x100(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x104(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1050 <strcat@plt>
lea    -0x100(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x104(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x104(%rbp)
jmp    1202 <main+0xa2>
cmpl   $0x0,-0x1c(%rbp)
je     12bc <main+0x15c>
mov    -0x30(%rbp),%rsi
lea    0xdca(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1290 <main+0x130>
mov    $0xffffffff,%eax
add    $0x110,%rsp
pop    %rbp
ret
cmpq   $0x0,-0x18(%rbp)
je     12a2 <main+0x142>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    12a7 <main+0x147>
mov    -0x30(%rbp),%rdi
lea    -0x100(%rbp),%rsi
call   1060 <execute_command@plt>
jmp    12dc <main+0x17c>
mov    -0x30(%rbp),%rsi
lea    0xd8a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x30(%rbp),%rdi
lea    -0x100(%rbp),%rsi
call   1060 <execute_command@plt>
jmp    12e1 <main+0x181>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
mov    %rsi,%r13
cmp    $0x1,%edi
jle    122a <main+0xca>
mov    %edi,%r12d
lea    -0x1(%r12),%ebx
mov    0x0(%r13,%rbx,8),%rdi
lea    0xe6f(%rip),%rsi
call   1040 <strcmp@plt>
mov    %rax,%r15
test   %rax,%rax
cmove  %ebx,%r12d
mov    0x8(%r13),%rax
mov    %rax,0x8(%rsp)
cmp    $0x3,%r12d
jb     11f1 <main+0x91>
mov    %r12d,%ebp
mov    $0x2,%r14d
lea    0x10(%rsp),%rbx
lea    0xe6c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%r13,%r14,8),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <strcat@plt>
add    $0x1,%r14
cmp    %r14,%rbp
jne    11d0 <main+0x70>
test   %r15,%r15
jne    1244 <main+0xe4>
lea    0xe35(%rip),%rdi
mov    0x8(%rsp),%rsi
call   1030 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jns    1257 <main+0xf7>
mov    $0xffffffff,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%r13),%rsi
lea    0xdce(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe06(%rip),%rdi
mov    0x8(%rsp),%rsi
call   1030 <printf@plt>
jmp    1259 <main+0xf9>
jne    1268 <main+0x108>
lea    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdi
call   1060 <execute_command@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"bg\00"
@.str.1 = [45 x i8] c"Modo de uso: %s <programa> [opciones] [bg]\0A\0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [31 x i8] c"%s se ejecutara en background\0A\00"
@.str.4 = [31 x i8] c"%s se ejecutara en foreground\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [200 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%13 = load i32, i32* %4
store i32 %13, i32* %8
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %9
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = load i32, i32* %4
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %23, i64 %26
%28 = load i8*, i8** %27
%29 = load i8*, i8** %9
%30 = call i64 @strcmp(i8* %28, i8* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %22
store i32 1, i32* %7
%33 = load i32, i32* %8
%34 = add nsw i32 %33, -1
store i32 %34, i32* %8
br label %35
35: ; preds = %32, %22
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
store i8* %38, i8** %10
store i32 2, i32* %12
br label %39
39: ; preds = %53, %35
%40 = load i32, i32* %12
%41 = load i32, i32* %8
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %56
43: ; preds = %39
%44 = getelementptr inbounds [200 x i8], [200 x i8]* %11, i64 0, i64 0
%45 = load i8**, i8*** %5
%46 = load i32, i32* %12
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8*, i8** %45, i64 %47
%49 = load i8*, i8** %48
%50 = call i32 @strcat(i8* %44, i8* %49)
%51 = getelementptr inbounds [200 x i8], [200 x i8]* %11, i64 0, i64 0
%52 = call i32 @strcat(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %43
%54 = load i32, i32* %12
%55 = add nsw i32 %54, 1
store i32 %55, i32* %12
br label %39
56: ; preds = %39
%57 = load i32, i32* %7
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %75
59: ; preds = %56
%60 = load i8*, i8** %10
%61 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %60)
%62 = call i64 (...) @fork()
store i64 %62, i64* %6
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %59
ret i32 -1
65: ; preds = %59
%66 = load i64, i64* %6
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %65
%69 = call i32 @exit(i32 0)
unreachable
70: ; preds = %65
br label %71
71: ; preds = %70
%72 = load i8*, i8** %10
%73 = getelementptr inbounds [200 x i8], [200 x i8]* %11, i64 0, i64 0
%74 = call i32 @execute_command(i8* %72, i8* %73)
br label %81
75: ; preds = %56
%76 = load i8*, i8** %10
%77 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %76)
%78 = load i8*, i8** %10
%79 = getelementptr inbounds [200 x i8], [200 x i8]* %11, i64 0, i64 0
%80 = call i32 @execute_command(i8* %78, i8* %79)
br label %81
81: ; preds = %75, %71
br label %82
82: ; preds = %81
%83 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [3 x i8] c"bg\00"
@.str.1 = [45 x i8] c"Modo de uso: %s <programa> [opciones] [bg]\0A\0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [31 x i8] c"%s se ejecutara en background\0A\00"
@.str.4 = [31 x i8] c"%s se ejecutara en foreground\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [200 x i8]
%4 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = add nsw i32 %0, -1
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8*, i8** %1, i64 %12
%14 = load i8*, i8** %13
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i32 %11, i32 %0
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = icmp ugt i32 %17, 2
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = zext i32 %17 to i64
br label %24
23: ; preds = %24, %10
br i1 %16, label %32, label %41
24: ; preds = %21, %24
%25 = phi i64 [ 2, %21 ], [ %30, %24 ]
%26 = getelementptr inbounds i8*, i8** %1, i64 %25
%27 = load i8*, i8** %26
%28 = call i32 @strcat(i8* %4, i8* %27)
%29 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = add nuw nsw i64 %25, 1
%31 = icmp eq i64 %30, %22
br i1 %31, label %23, label %24
32: ; preds = %23
%33 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%34 = call i64 (...) @fork()
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
ret i32 -1
37: ; preds = %32
%38 = icmp eq i64 %34, 0
br i1 %38, label %43, label %39
39: ; preds = %37
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %23
%42 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %19)
br label %43
43: ; preds = %37, %41
%44 = call i32 @execute_command(i8* %19, i8* %4)
%45 = call i32 @exit(i32 0)
unreachable
}