#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__ pthread_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int pthread_detach(scalar_t__);
int /*<<< orphan*/ pthread_exit(void *);
scalar_t__ pthread_self();
int /*<<< orphan*/ stderr;
void *secondary_detach(void *arg) {
  (void)(arg);
  pthread_t tid = pthread_self();
  if (tid == 0) {
    fprintf(stderr, "Error: pthread_self() returned 0\n");
    exit(1);
  }
  int res = pthread_detach(tid);
  if (res != 0) {
    fprintf(stderr, "Error: pthread_detach(%lld) returned %d\n",
            (unsigned long long)tid, res);
    exit(res);
  }
  printf("Secondary detached thread alive\n");
  pthread_exit(((void *)0xBAADC0DE));
  return ((void *)0xDEADDEAD);
}


x86 assembly (clang, -O0):
<secondary_detach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a3 <secondary_detach+0x43>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1060 <pthread_detach@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11df <secondary_detach+0x7f>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1080 <exit@plt>
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
mov    $0xbaadc0de,%edi
call   1050 <pthread_exit@plt>
mov    $0xdeaddead,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secondary_detach>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <pthread_self@plt>
test   %rax,%rax
je     11aa <secondary_detach+0x4a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <pthread_detach@plt>
test   %eax,%eax
jne    11cb <secondary_detach+0x6b>
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xbaadc0de,%ebx
mov    $0xbaadc0de,%edi
call   1050 <pthread_exit@plt>
add    $0x24001dcf,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%ebp
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"Error: pthread_self() returned 0\0A\00"
@.str.1 = [41 x i8] c"Error: pthread_detach(%lld) returned %d\0A\00"
@.str.2 = [33 x i8] c"Secondary detached thread alive\0A\00"
define i8* @secondary_detach(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 (...) @pthread_self()
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i64, i64* %3
%15 = call i32 @pthread_detach(i64 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i64, i64* %3
%21 = load i32, i32* %4
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i64 %20, i32 %21)
%23 = load i32, i32* %4
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %13
%26 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @pthread_exit(i8* inttoptr (i64 3131949278 to i8*))
ret i8* inttoptr (i64 3735936685 to i8*)
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"Error: pthread_self() returned 0\0A\00"
@.str.1 = [41 x i8] c"Error: pthread_detach(%lld) returned %d\0A\00"
@str = [32 x i8] c"Secondary detached thread alive\00"
define i8* @secondary_detach(i8* nocapture readnone %0) {
%2 = tail call i64 (...) @pthread_self()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = tail call i32 @pthread_detach(i64 %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i64 %2, i32 %9)
%14 = tail call i32 @exit(i32 %9)
unreachable
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @pthread_exit(i8* inttoptr (i64 3131949278 to i8*))
ret i8* inttoptr (i64 3735936685 to i8*)
}