#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*/ SIGTERM;
scalar_t__ getpid();
int kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ spawn(char *, char **);
int main() {
  printf("Soy el padre: %d\n\n", (int)getpid());
  char *arg_list[] = {"ls", "-l", NULL};
  pid_t pid1, pid2;
  pid1 = spawn("ls", arg_list);
  printf("Soy el hijo 1: %d\n", (int)pid1);
  if (pid1 != 0) {
    char *arg_list[] = {"ps", "-l", NULL};
    pid2 = spawn("ps", arg_list);
    printf("Soy el hijo 2: %d\n", (int)pid2);
    if (pid2 != 0) {
      sleep(1);
      int signal1_sent, signal2_sent;
      signal1_sent = kill(pid1, SIGTERM);
      signal2_sent = kill(pid2, SIGTERM);
      if (signal1_sent == 0 && signal2_sent == 0) {
        printf("Done! Process was killed");
      } else {
        printf("The signals to kill the process couldn'd be sent.");
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c63(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c60(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2c5d(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x20(%rbp),%rsi
lea    0xe71(%rip),%rdi
call   1060 <spawn@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x28(%rbp)
je     1291 <main+0x141>
mov    0x2c2f(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c2c(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2c29(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    -0x50(%rbp),%rsi
lea    0xe36(%rip),%rdi
call   1060 <spawn@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x30(%rbp)
je     128c <main+0x13c>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x28(%rbp),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    %eax,-0x54(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x54(%rbp)
jne    1279 <main+0x129>
cmpl   $0x0,-0x58(%rbp)
jne    1279 <main+0x129>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1287 <main+0x137>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128c <main+0x13c>
jmp    1291 <main+0x141>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe99(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2c79(%rip),%rax
mov    %rax,0x30(%rsp)
movaps 0x2c5d(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xe84(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1060 <spawn@plt>
mov    %rax,%rbx
lea    0xe76(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %rbx,%rbx
je     1238 <main+0xe8>
mov    0x2c54(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2c38(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe59(%rip),%rdi
mov    %rsp,%rsi
call   1060 <spawn@plt>
mov    %rax,%r14
lea    0xe4a(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r14,%r14
je     1238 <main+0xe8>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0x2de3(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   1050 <kill@plt>
mov    %eax,%ebx
mov    (%r15),%esi
mov    %r14,%rdi
call   1050 <kill@plt>
lea    0xe1e(%rip),%rcx
lea    0xe30(%rip),%rdi
or     %ebx,%eax
cmove  %rcx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Soy el padre: %d\0A\0A\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [3 x i8] c"-l\00"
@__const.main.arg_list = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.3 = [19 x i8] c"Soy el hijo 1: %d\0A\00"
@.str.4 = [3 x i8] c"ps\00"
@__const.main.arg_list.5 = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.6 = [19 x i8] c"Soy el hijo 2: %d\0A\00"
@SIGTERM = global i32 0
@.str.7 = [25 x i8] c"Done! Process was killed\00"
@.str.8 = [50 x i8] c"The signals to kill the process couldn'd be sent.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [3 x i8*]
%3 = alloca i64
%4 = alloca i64
%5 = alloca [3 x i8*]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i64 (...) @getpid()
%9 = trunc i64 %8 to i32
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = bitcast [3 x i8*]* %2 to i8*
%12 = getelementptr inbounds [3 x i8*], [3 x i8*]* %2, i64 0, i64 0
%13 = call i64 @spawn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %12)
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = trunc i64 %14 to i32
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = load i64, i64* %3
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %47
19: ; preds = %0
%20 = bitcast [3 x i8*]* %5 to i8*
%21 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%22 = call i64 @spawn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %21)
store i64 %22, i64* %4
%23 = load i64, i64* %4
%24 = trunc i64 %23 to i32
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = load i64, i64* %4
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %46
28: ; preds = %19
%29 = call i32 @sleep(i32 1)
%30 = load i64, i64* %3
%31 = load i32, i32* @SIGTERM
%32 = call i32 @kill(i64 %30, i32 %31)
store i32 %32, i32* %6
%33 = load i64, i64* %4
%34 = load i32, i32* @SIGTERM
%35 = call i32 @kill(i64 %33, i32 %34)
store i32 %35, i32* %7
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %28
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
br label %45
43: ; preds = %38, %28
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.8, i64 0, i64 0))
br label %45
45: ; preds = %43, %41
br label %46
46: ; preds = %45, %19
br label %47
47: ; preds = %46, %0
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"Soy el padre: %d\0A\0A\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [3 x i8] c"-l\00"
@__const.main.arg_list = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.3 = [19 x i8] c"Soy el hijo 1: %d\0A\00"
@.str.4 = [3 x i8] c"ps\00"
@__const.main.arg_list.5 = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.6 = [19 x i8] c"Soy el hijo 2: %d\0A\00"
@SIGTERM = global i32 0
@.str.7 = [25 x i8] c"Done! Process was killed\00"
@.str.8 = [50 x i8] c"The signals to kill the process couldn'd be sent.\00"
define i32 @main() {
%1 = alloca [3 x i8*]
%2 = alloca [3 x i8*]
%3 = tail call i64 (...) @getpid()
%4 = trunc i64 %3 to i32
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = bitcast [3 x i8*]* %1 to i8*
%7 = getelementptr inbounds [3 x i8*], [3 x i8*]* %1, i64 0, i64 0
%8 = call i64 @spawn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %7)
%9 = trunc i64 %8 to i32
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%11 = icmp eq i64 %8, 0
br i1 %11, label %31, label %12
12: ; preds = %0
%13 = bitcast [3 x i8*]* %2 to i8*
%14 = getelementptr inbounds [3 x i8*], [3 x i8*]* %2, i64 0, i64 0
%15 = call i64 @spawn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %14)
%16 = trunc i64 %15 to i32
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %16)
%18 = icmp eq i64 %15, 0
br i1 %18, label %30, label %19
19: ; preds = %12
%20 = call i32 @sleep(i32 1)
%21 = load i32, i32* @SIGTERM
%22 = call i32 @kill(i64 %8, i32 %21)
%23 = load i32, i32* @SIGTERM
%24 = call i32 @kill(i64 %15, i32 %23)
%25 = icmp eq i32 %22, 0
%26 = icmp eq i32 %24, 0
%27 = select i1 %25, i1 %26, i1 false
%28 = select i1 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.8, i64 0, i64 0)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %28)
br label %30
30: ; preds = %19, %12
br label %31
31: ; preds = %30, %0
ret i32 0
}