#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 /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stack_free(void *, int);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void delete_thread(pid_t pid, void *stack) {
  pid_t result;
  printf("Waiting for child to exit\n");
  result = waitpid(pid, NULL, 0);
  printf("Child has exited\n");
  if (result == -1 || result != pid)
    perror("delete_thread waitpid");
  stack_free(stack, (32 * 1024));
}


x86 assembly (clang, -O0):
<delete_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeaa(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x14(%rbp)
lea    0xea8(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0xffffffff,-0x14(%rbp)
je     118e <delete_thread+0x4e>
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     119a <delete_thread+0x5a>
lea    0xe98(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
mov    $0x8000,%esi
call   1030 <stack_free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delete_thread>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xec6(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,%ebx
lea    0xec7(%rip),%rdi
call   1040 <puts@plt>
cmp    $0xffffffff,%ebx
je     1188 <delete_thread+0x48>
cmp    %ebp,%ebx
jne    1188 <delete_thread+0x48>
mov    %r14,%rdi
mov    $0x8000,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <stack_free@plt>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
jmp    1177 <delete_thread+0x37>

LLVM IR (-O0):
@.str = [27 x i8] c"Waiting for child to exit\0A\00"
@.str.1 = [18 x i8] c"Child has exited\0A\00"
@.str.2 = [22 x i8] c"delete_thread waitpid\00"
define void @delete_thread(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* %3
%8 = call i32 @waitpid(i32 %7, i32* null, i32 0)
store i32 %8, i32* %5
%9 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* %5
%14 = load i32, i32* %3
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12, %2
%17 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i8*, i8** %4
%20 = call i32 @stack_free(i8* %19, i32 32768)
ret void
}

LLVM IR (-O2):
@.str.2 = [22 x i8] c"delete_thread waitpid\00"
@str = [26 x i8] c"Waiting for child to exit\00"
@str.3 = [17 x i8] c"Child has exited\00"
define void @delete_thread(i32 %0, i8* %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @waitpid(i32 %0, i32* null, i32 0)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.3, i64 0, i64 0))
%6 = icmp ne i32 %4, -1
%7 = icmp eq i32 %4, %0
%8 = and i1 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %2, %9
%12 = tail call i32 @stack_free(i8* %1, i32 32768)
ret void
}