void imprimir_pid(int numero_trozos, char *comando_troceado[]) {
  if (numero_trozos == 1) {
    printf("%d\n", getpid());
  } else if (numero_trozos > 1 && strcmp("-p", comando_troceado[1]) == 0) {
    printf("%d\n", getppid());
  } else if (numero_trozos > 1 && strcmp("-h", comando_troceado[1]) == 0) {
    printf("Imprime el PID del shell y de su proceso padre\n");
    printf("Uso: pid [-p]\n");
    return;
  } else {
    printf("Parametro %s desconocido\n", comando_troceado[1]);
    return;
  }
}


x86 assembly (clang, -O0):
<imprimir_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    1175 <imprimir_pid+0x35>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1220 <imprimir_pid+0xe0>
cmpl   $0x1,-0x4(%rbp)
jle    11b8 <imprimir_pid+0x78>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe76(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11b8 <imprimir_pid+0x78>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121b <imprimir_pid+0xdb>
cmpl   $0x1,-0x4(%rbp)
jle    1200 <imprimir_pid+0xc0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe36(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1200 <imprimir_pid+0xc0>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1220 <imprimir_pid+0xe0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1220 <imprimir_pid+0xe0>
jmp    1220 <imprimir_pid+0xe0>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imprimir_pid>:
push   %rbx
cmp    $0x1,%edi
jne    116e <imprimir_pid+0x1e>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe9c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
pop    %rbx
jmp    1050 <printf@plt>
mov    0x8(%rsi),%rbx
jle    119a <imprimir_pid+0x4a>
lea    0xe89(%rip),%rdi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11ac <imprimir_pid+0x5c>
lea    0xe79(%rip),%rdi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11b5 <imprimir_pid+0x65>
lea    0xe69(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
pop    %rbx
jmp    1050 <printf@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
jmp    115d <imprimir_pid+0xd>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
lea    0xe8b(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [3 x i8] c"-p\00"
@.str.2 = [3 x i8] c"-h\00"
@.str.3 = [48 x i8] c"Imprime el PID del shell y de su proceso padre\0A\00"
@.str.4 = [15 x i8] c"Uso: pid [-p]\0A\00"
@.str.5 = [26 x i8] c"Parametro %s desconocido\0A\00"
define void @imprimir_pid(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
store i32 %0, i32* %3
store i8** %1, i8*** %4
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, 1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = call i32 (...) @getpid()
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %40
10: ; preds = %2
%11 = load i32, i32* %3
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = load i8**, i8*** %4
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 (...) @getppid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %39
22: ; preds = %13, %10
%23 = load i32, i32* %3
%24 = icmp sgt i32 %23, 1
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = load i8**, i8*** %4
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %40
34: ; preds = %25, %22
%35 = load i8**, i8*** %4
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %37)
br label %40
39: ; preds = %19
br label %40
40: ; preds = %31, %34, %39, %7
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [3 x i8] c"-p\00"
@.str.2 = [3 x i8] c"-h\00"
@.str.5 = [26 x i8] c"Parametro %s desconocido\0A\00"
@str = [47 x i8] c"Imprime el PID del shell y de su proceso padre\00"
@str.6 = [14 x i8] c"Uso: pid [-p]\00"
define void @imprimir_pid(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %25
7: ; preds = %2
%8 = icmp sgt i32 %0, 1
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
br i1 %8, label %11, label %23
11: ; preds = %7
%12 = tail call i32 @strcmp(i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* dereferenceable(1) %10)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 (...) @getppid()
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %25
17: ; preds = %11
%18 = tail call i32 @strcmp(i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* dereferenceable(1) %10)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
br label %25
23: ; preds = %7, %17
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %10)
br label %25
25: ; preds = %20, %23, %14, %4
ret void
}