#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 */

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ SIGUSR1;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int ejecutaHijo(char *, char *, int);
int /*<<< orphan*/ envia(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int petic_rexec(int sockCliente, char *direccionLocal, char *comando) {
  int rc;
  int pidHijo;
  char cadena[300] = "";
  if (comando == NULL) {
    fprintf(stderr,
            "petic_rexec(): esperaba 1 argumento: comando a ejecutar\n");
    sprintf(cadena, "%d -1\n", EINVAL);
    envia(sockCliente, cadena, strlen(cadena));
    return -1;
  }
  rc = ejecutaHijo(comando, direccionLocal, sockCliente);
  if (rc <= 0) {
    fprintf(stderr, "petic_rexec(): ejecutaHijo(): error\n");
    sprintf(cadena, "%d -1\n", EINVAL);
    envia(sockCliente, cadena, strlen(cadena));
    return -1;
  }
  pidHijo = rc;
  sprintf(cadena, "0 %d\n", pidHijo);
  envia(sockCliente, cadena, strlen(cadena));
  kill(pidHijo, SIGUSR1);
  if (waitpid(pidHijo, &rc, 0) == -1)
    perror("petic_rexec(): waitpid()");
  if (WIFEXITED(rc)) {
    fprintf(stderr, "petic_rexec(): exited, status=%d\n", WEXITSTATUS(rc));
  } else if (WIFSIGNALED(rc)) {
    fprintf(stderr, "petic_rexec(): killed by signal %d\n", WTERMSIG(rc));
  }
  return 0;
}


x86 assembly (clang, -O0):
<petic_rexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x150(%rbp),%rdi
xor    %esi,%esi
mov    $0x12c,%edx
call   1060 <memset@plt>
cmpq   $0x0,-0x18(%rbp)
jne    127a <petic_rexec+0xaa>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    -0x150(%rbp),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
lea    0xe07(%rip),%rsi
call   10f0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x15c(%rbp)
lea    -0x150(%rbp),%rax
mov    %rax,-0x158(%rbp)
lea    -0x150(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x15c(%rbp),%edi
mov    -0x158(%rbp),%rsi
mov    %eax,%edx
call   1040 <envia@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1423 <petic_rexec+0x253>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <ejecutaHijo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     130d <petic_rexec+0x13d>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    -0x150(%rbp),%rdi
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
lea    0xd74(%rip),%rsi
call   10f0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x16c(%rbp)
lea    -0x150(%rbp),%rax
mov    %rax,-0x168(%rbp)
lea    -0x150(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x16c(%rbp),%edi
mov    -0x168(%rbp),%rsi
mov    %eax,%edx
call   1040 <envia@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1423 <petic_rexec+0x253>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0x150(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xd41(%rip),%rsi
call   10f0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x17c(%rbp)
lea    -0x150(%rbp),%rax
mov    %rax,-0x178(%rbp)
lea    -0x150(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x17c(%rbp),%edi
mov    -0x178(%rbp),%rsi
mov    %eax,%edx
call   1040 <envia@plt>
mov    -0x20(%rbp),%edi
mov    0x2c7e(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   10c0 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1394 <petic_rexec+0x1c4>
lea    0xcdc(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     13d8 <petic_rexec+0x208>
mov    0x2c2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x180(%rbp)
mov    -0x1c(%rbp),%edi
call   10b0 <WEXITSTATUS@plt>
mov    -0x180(%rbp),%edi
mov    %eax,%edx
lea    0xcb8(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    141c <petic_rexec+0x24c>
mov    -0x1c(%rbp),%edi
call   1070 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1417 <petic_rexec+0x247>
mov    0x2be7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x184(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <WTERMSIG@plt>
mov    -0x184(%rbp),%edi
mov    %eax,%edx
lea    0xc96(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    141c <petic_rexec+0x24c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<petic_rexec>:
push   %rbp
push   %r14
push   %rbx
sub    $0x140,%rsp
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r14d
lea    0x10(%rsp),%rdi
mov    $0x12c,%edx
xor    %esi,%esi
call   1060 <memset@plt>
test   %rbx,%rbx
je     129c <petic_rexec+0xcc>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <ejecutaHijo@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jle    12ae <petic_rexec+0xde>
mov    %eax,%ebp
lea    0xe44(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10f0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <envia@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1090 <kill@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10c0 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1344 <petic_rexec+0x174>
mov    0xc(%rsp),%edi
call   10e0 <WIFEXITED@plt>
test   %rax,%rax
je     12fe <petic_rexec+0x12e>
mov    0x2d53(%rip),%rax
mov    (%rax),%ebp
mov    0xc(%rsp),%edi
call   10b0 <WEXITSTATUS@plt>
lea    0xded(%rip),%rsi
jmp    1325 <petic_rexec+0x155>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
jmp    12be <petic_rexec+0xee>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd82(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
lea    0xd64(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   10f0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <envia@plt>
mov    $0xffffffff,%ebx
jmp    1332 <petic_rexec+0x162>
mov    0xc(%rsp),%edi
call   1070 <WIFSIGNALED@plt>
test   %rax,%rax
je     1340 <petic_rexec+0x170>
mov    0x2cc5(%rip),%rax
mov    (%rax),%ebp
mov    0xc(%rsp),%edi
call   10a0 <WTERMSIG@plt>
lea    0xd81(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%eax
add    $0x140,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ebx,%ebx
jmp    1332 <petic_rexec+0x162>
lea    0xd20(%rip),%rdi
call   10d0 <perror@plt>
jmp    126c <petic_rexec+0x9c>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [57 x i8] c"petic_rexec(): esperaba 1 argumento: comando a ejecutar\0A\00"
@.str.1 = [7 x i8] c"%d -1\0A\00"
@EINVAL = global i32 0
@.str.2 = [37 x i8] c"petic_rexec(): ejecutaHijo(): error\0A\00"
@.str.3 = [6 x i8] c"0 %d\0A\00"
@SIGUSR1 = global i32 0
@.str.4 = [25 x i8] c"petic_rexec(): waitpid()\00"
@.str.5 = [34 x i8] c"petic_rexec(): exited, status=%d\0A\00"
@.str.6 = [36 x i8] c"petic_rexec(): killed by signal %d\0A\00"
define i32 @petic_rexec(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [300 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = bitcast [300 x i8]* %10 to i8*
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %25
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds [300 x i8], [300 x i8]* %10, i64 0, i64 0
%18 = load i32, i32* @EINVAL
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %5
%21 = getelementptr inbounds [300 x i8], [300 x i8]* %10, i64 0, i64 0
%22 = getelementptr inbounds [300 x i8], [300 x i8]* %10, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @envia(i32 %20, i8* %21, i32 %23)
store i32 -1, i32* %4
br label %81
25: ; preds = %3
%26 = load i8*, i8** %7
%27 = load i8*, i8** %6
%28 = load i32, i32* %5
%29 = call i32 @ejecutaHijo(i8* %26, i8* %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%35 = getelementptr inbounds [300 x i8], [300 x i8]* %10, i64 0, i64 0
%36 = load i32, i32* @EINVAL
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %5
%39 = getelementptr inbounds [300 x i8], [300 x i8]* %10, i64 0, i64 0
%40 = getelementptr inbounds [300 x i8], [300 x i8]* %10, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @envia(i32 %38, i8* %39, i32 %41)
store i32 -1, i32* %4
br label %81
43: ; preds = %25
%44 = load i32, i32* %8
store i32 %44, i32* %9
%45 = getelementptr inbounds [300 x i8], [300 x i8]* %10, i64 0, i64 0
%46 = load i32, i32* %9
%47 = call i32 @sprintf(i8* %45, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %5
%49 = getelementptr inbounds [300 x i8], [300 x i8]* %10, i64 0, i64 0
%50 = getelementptr inbounds [300 x i8], [300 x i8]* %10, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = call i32 @envia(i32 %48, i8* %49, i32 %51)
%53 = load i32, i32* %9
%54 = load i32, i32* @SIGUSR1
%55 = call i32 @kill(i32 %53, i32 %54)
%56 = load i32, i32* %9
%57 = call i32 @waitpid(i32 %56, i32* %8, i32 0)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %43
%60 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %59, %43
%62 = load i32, i32* %8
%63 = call i64 @WIFEXITED(i32 %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %70
65: ; preds = %61
%66 = load i32, i32* @stderr
%67 = load i32, i32* %8
%68 = call i32 @WEXITSTATUS(i32 %67)
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %68)
br label %80
70: ; preds = %61
%71 = load i32, i32* %8
%72 = call i64 @WIFSIGNALED(i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i32, i32* @stderr
%76 = load i32, i32* %8
%77 = call i32 @WTERMSIG(i32 %76)
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %74, %70
br label %80
80: ; preds = %79, %65
store i32 0, i32* %4
br label %81
81: ; preds = %80, %32, %14
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [57 x i8] c"petic_rexec(): esperaba 1 argumento: comando a ejecutar\0A\00"
@.str.1 = [7 x i8] c"%d -1\0A\00"
@EINVAL = global i32 0
@.str.2 = [37 x i8] c"petic_rexec(): ejecutaHijo(): error\0A\00"
@.str.3 = [6 x i8] c"0 %d\0A\00"
@SIGUSR1 = global i32 0
@.str.4 = [25 x i8] c"petic_rexec(): waitpid()\00"
@.str.5 = [34 x i8] c"petic_rexec(): exited, status=%d\0A\00"
@.str.6 = [36 x i8] c"petic_rexec(): killed by signal %d\0A\00"
define i32 @petic_rexec(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca [300 x i8]
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 0
%8 = icmp eq i8* %2, null
br i1 %8, label %9, label %16
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EINVAL
%13 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = call i32 @strlen(i8* %7)
%15 = call i32 @envia(i32 %0, i8* %7, i32 %14)
br label %54
16: ; preds = %3
%17 = tail call i32 @ejecutaHijo(i8* %2, i8* %1, i32 %0)
store i32 %17, i32* %4
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EINVAL
%23 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i32 @strlen(i8* %7)
%25 = call i32 @envia(i32 %0, i8* %7, i32 %24)
br label %54
26: ; preds = %16
%27 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%28 = call i32 @strlen(i8* %7)
%29 = call i32 @envia(i32 %0, i8* %7, i32 %28)
%30 = load i32, i32* @SIGUSR1
%31 = call i32 @kill(i32 %17, i32 %30)
%32 = call i32 @waitpid(i32 %17, i32* %4, i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %26
%37 = load i32, i32* %4
%38 = call i64 @WIFEXITED(i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = load i32, i32* %4
%43 = call i32 @WEXITSTATUS(i32 %42)
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %43)
br label %54
45: ; preds = %36
%46 = load i32, i32* %4
%47 = call i64 @WIFSIGNALED(i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %54, label %49
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = load i32, i32* %4
%52 = call i32 @WTERMSIG(i32 %51)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %40, %49, %45, %19, %9
%55 = phi i32 [ -1, %9 ], [ -1, %19 ], [ 0, %45 ], [ 0, %49 ], [ 0, %40 ]
ret i32 %55
}