#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  int fdOut, fdErr, fdIn;
  char **onlyArgs = argv + 1;
  pid_t pid = fork();
  if (pid == -1)
    perror("Error fork");
  else if (pid == 0) {
    fdOut = open("/tmp/daemon.out", O_RDWR | O_CREAT);
    fdErr = open("/tmp/daemon.err", O_RDWR | O_CREAT);
    fdIn = open("/tmp/null", O_RDWR | O_CREAT);
    dup2(fdIn, STDIN_FILENO);
    dup2(fdOut, STDOUT_FILENO);
    dup2(fdErr, STDERR_FILENO);
    execvp(argv[1], onlyArgs);
    close(fdErr);
    close(fdOut);
    close(fdIn);
  } else {
    int status;
    wait(&status);
    printf("El comando termino de ejecutarse\n");
  }
  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)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11c7 <main+0x47>
lea    0xe43(%rip),%rdi
call   1070 <perror@plt>
jmp    12af <main+0x12f>
cmpl   $0x0,-0x2c(%rbp)
jne    1295 <main+0x115>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
lea    0xe21(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
lea    0xe10(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    0x2dd5(%rip),%rax
or     (%rax),%esi
lea    0xdff(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <execvp@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    12aa <main+0x12a>
lea    -0x30(%rbp),%rdi
call   1090 <wait@plt>
lea    0xd90(%rip),%rdi
call   1040 <printf@plt>
jmp    12af <main+0x12f>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11be <main+0x3e>
cmp    $0xffffffff,%eax
je     1274 <main+0xf4>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
jmp    1265 <main+0xe5>
add    $0x8,%rbx
mov    0x2e0f(%rip),%r12
mov    0x2e28(%rip),%rbp
mov    0x0(%rbp),%esi
or     (%r12),%esi
lea    0xe2d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%esi
or     (%r12),%esi
lea    0xe27(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r15d
mov    0x0(%rbp),%esi
or     (%r12),%esi
lea    0xe21(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <dup2@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1040 <dup2@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1080 <execvp@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd85(%rip),%rdi
call   1070 <perror@plt>
jmp    1265 <main+0xe5>

LLVM IR (-O0):
@.str = [11 x i8] c"Error fork\00"
@.str.1 = [16 x i8] c"/tmp/daemon.out\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [16 x i8] c"/tmp/daemon.err\00"
@.str.3 = [10 x i8] c"/tmp/null\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.4 = [34 x i8] c"El comando termino de ejecutarse\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
store i8** %13, i8*** %9
%14 = call i32 (...) @fork()
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %59
19: ; preds = %2
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %55
22: ; preds = %19
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* @O_RDWR
%32 = load i32, i32* @O_CREAT
%33 = or i32 %31, %32
%34 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = load i32, i32* @STDIN_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = load i32, i32* %6
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @dup2(i32 %38, i32 %39)
%41 = load i32, i32* %7
%42 = load i32, i32* @STDERR_FILENO
%43 = call i32 @dup2(i32 %41, i32 %42)
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = load i8**, i8*** %9
%48 = call i32 @execvp(i8* %46, i8** %47)
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
br label %58
55: ; preds = %19
%56 = call i32 @wait(i32* %11)
%57 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %55, %22
br label %59
59: ; preds = %58, %17
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"Error fork\00"
@.str.1 = [16 x i8] c"/tmp/daemon.out\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [16 x i8] c"/tmp/daemon.err\00"
@.str.3 = [10 x i8] c"/tmp/null\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@str = [33 x i8] c"El comando termino de ejecutarse\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = tail call i32 (...) @fork()
switch i32 %5, label %32 [
i32 -1, label %6
i32 0, label %8
]
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %36
8: ; preds = %2
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @STDIN_FILENO
%22 = tail call i32 @dup2(i32 %20, i32 %21)
%23 = load i32, i32* @STDOUT_FILENO
%24 = tail call i32 @dup2(i32 %12, i32 %23)
%25 = load i32, i32* @STDERR_FILENO
%26 = tail call i32 @dup2(i32 %16, i32 %25)
%27 = load i8*, i8** %4
%28 = tail call i32 @execvp(i8* %27, i8** %4)
%29 = tail call i32 @close(i32 %16)
%30 = tail call i32 @close(i32 %12)
%31 = tail call i32 @close(i32 %20)
br label %36
32: ; preds = %2
%33 = bitcast i32* %3 to i8*
%34 = call i32 @wait(i32* %3)
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %8, %32, %6
ret i32 0
}