#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*/ generate_segv(int);
int /*<<< orphan*/ lock();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_signal_stack();
int /*<<< orphan*/ printf(char *, unsigned long);
int pthread_create(scalar_t__ *, int /*<<< orphan*/ *, void *(*)(void *),
                   void *);
int pthread_join(scalar_t__, void **);
int /*<<< orphan*/ setup_signal_stack();
int /*<<< orphan*/ unlock();
void *thread_start(void *arg) {
  int ret_val;
  lock();
  printf("thread arg = %d\n", (unsigned int)arg);
  if (arg) {
    setup_signal_stack();
  } else {
    print_signal_stack();
  }
  generate_segv(1);
  unlock();
  if (arg == (void *)2) {
    pthread_t tid;
    void *thread_ret;
    ret_val = pthread_create(&tid, NULL, thread_start, NULL);
    if (ret_val) {
      perror("ERROR, pthread_create failed");
      exit(1);
    }
    lock();
    printf("created thread 0x%lx\n", (unsigned long)tid);
    unlock();
    ret_val = pthread_join(tid, &thread_ret);
    if (ret_val) {
      perror("ERROR, pthread_join failed");
      exit(1);
    }
    ret_val = pthread_create(&tid, NULL, thread_start, (void *)1);
    if (ret_val) {
      perror("ERROR, pthread_create failed");
      exit(1);
    }
    lock();
    printf("created thread 0x%lx\n", (unsigned long)tid);
    unlock();
    ret_val = pthread_join(tid, &thread_ret);
    if (ret_val) {
      perror("ERROR, pthread_join failed");
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<thread_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <lock@plt>
mov    -0x8(%rbp),%rax
mov    %eax,%eax
mov    %eax,%esi
lea    0xe3e(%rip),%rdi
call   1060 <printf@plt>
cmpq   $0x0,-0x8(%rbp)
je     11de <thread_start+0x3e>
mov    $0x0,%al
call   1080 <setup_signal_stack@plt>
jmp    11e5 <thread_start+0x45>
mov    $0x0,%al
call   1070 <print_signal_stack@plt>
mov    $0x1,%edi
call   1050 <generate_segv@plt>
mov    $0x0,%al
call   1030 <unlock@plt>
mov    $0x2,%eax
cmp    %rax,-0x8(%rbp)
jne    131c <thread_start+0x17c>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    0x2dcc(%rip),%rdx
mov    %rcx,%rsi
call   1090 <pthread_create@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     123f <thread_start+0x9f>
lea    0xde1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1040 <lock@plt>
mov    -0x18(%rbp),%rsi
lea    0xddd(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <unlock@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10c0 <pthread_join@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     128d <thread_start+0xed>
lea    0xdc6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    0x2d44(%rip),%rdx
mov    $0x1,%ecx
call   1090 <pthread_create@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     12c9 <thread_start+0x129>
lea    0xd57(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   1040 <lock@plt>
mov    -0x18(%rbp),%rsi
lea    0xd53(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <unlock@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10c0 <pthread_join@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1317 <thread_start+0x177>
lea    0xd3c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    131c <thread_start+0x17c>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_start>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <lock@plt>
mov    %ebx,%esi
lea    0xe48(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
test   %rbx,%rbx
je     11cb <thread_start+0x2b>
call   1080 <setup_signal_stack@plt>
jmp    11d0 <thread_start+0x30>
call   1070 <print_signal_stack@plt>
mov    $0x1,%edi
call   1050 <generate_segv@plt>
xor    %eax,%eax
call   1030 <unlock@plt>
cmp    $0x2,%rbx
jne    1280 <thread_start+0xe0>
mov    0x2dee(%rip),%rdx
mov    %rsp,%rdi
xor    %esi,%esi
xor    %ecx,%ecx
call   1090 <pthread_create@plt>
test   %eax,%eax
jne    1288 <thread_start+0xe8>
xor    %eax,%eax
call   1040 <lock@plt>
mov    (%rsp),%rsi
lea    0xe16(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1030 <unlock@plt>
mov    (%rsp),%rdi
lea    0x8(%rsp),%rsi
call   10c0 <pthread_join@plt>
test   %eax,%eax
jne    1291 <thread_start+0xf1>
mov    0x2da3(%rip),%rdx
mov    %rsp,%rdi
mov    $0x1,%ecx
xor    %esi,%esi
call   1090 <pthread_create@plt>
test   %eax,%eax
jne    1288 <thread_start+0xe8>
xor    %eax,%eax
call   1040 <lock@plt>
mov    (%rsp),%rsi
lea    0xdcc(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1030 <unlock@plt>
mov    (%rsp),%rdi
lea    0x8(%rsp),%rsi
call   10c0 <pthread_join@plt>
test   %eax,%eax
jne    1291 <thread_start+0xf1>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd82(%rip),%rdi
jmp    1298 <thread_start+0xf8>
lea    0xdac(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"thread arg = %d\0A\00"
@.str.1 = [29 x i8] c"ERROR, pthread_create failed\00"
@.str.2 = [22 x i8] c"created thread 0x%lx\0A\00"
@.str.3 = [27 x i8] c"ERROR, pthread_join failed\00"
define i8* @thread_start(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
%6 = call i32 (...) @lock()
%7 = load i8*, i8** %2
%8 = ptrtoint i8* %7 to i32
%9 = zext i32 %8 to i64
%10 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %9)
%11 = load i8*, i8** %2
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 (...) @setup_signal_stack()
br label %17
15: ; preds = %1
%16 = call i32 (...) @print_signal_stack()
br label %17
17: ; preds = %15, %13
%18 = call i32 @generate_segv(i32 1)
%19 = call i32 (...) @unlock()
%20 = load i8*, i8** %2
%21 = icmp eq i8* %20, inttoptr (i64 2 to i8*)
br i1 %21, label %22, label %61
22: ; preds = %17
%23 = call i32 @pthread_create(i64* %4, i32* null, i8* (i8*)* @thread_start, i8* null)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = call i32 (...) @lock()
%31 = load i64, i64* %4
%32 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %31)
%33 = call i32 (...) @unlock()
%34 = load i64, i64* %4
%35 = call i32 @pthread_join(i64 %34, i8** %5)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %29
%42 = call i32 @pthread_create(i64* %4, i32* null, i8* (i8*)* @thread_start, i8* inttoptr (i64 1 to i8*))
store i32 %42, i32* %3
%43 = load i32, i32* %3
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %41
%49 = call i32 (...) @lock()
%50 = load i64, i64* %4
%51 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %50)
%52 = call i32 (...) @unlock()
%53 = load i64, i64* %4
%54 = call i32 @pthread_join(i64 %53, i8** %5)
store i32 %54, i32* %3
%55 = load i32, i32* %3
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %48
%58 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %48
br label %61
61: ; preds = %60, %17
ret i8* null
}

LLVM IR (-O2):
@.str = [17 x i8] c"thread arg = %d\0A\00"
@.str.1 = [29 x i8] c"ERROR, pthread_create failed\00"
@.str.2 = [22 x i8] c"created thread 0x%lx\0A\00"
@.str.3 = [27 x i8] c"ERROR, pthread_join failed\00"
define noalias i8* @thread_start(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = tail call i32 (...) @lock()
%5 = ptrtoint i8* %0 to i64
%6 = and i64 %5, 4294967295
%7 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %6)
%8 = icmp eq i8* %0, null
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = tail call i32 (...) @setup_signal_stack()
br label %13
11: ; preds = %1
%12 = tail call i32 (...) @print_signal_stack()
br label %13
13: ; preds = %11, %9
%14 = tail call i32 @generate_segv(i32 1)
%15 = tail call i32 (...) @unlock()
%16 = icmp eq i8* %0, inttoptr (i64 2 to i8*)
br i1 %16, label %17, label %54
17: ; preds = %13
%18 = bitcast i64* %2 to i8*
%19 = bitcast i8** %3 to i8*
%20 = call i32 @pthread_create(i64* %2, i32* null, i8* (i8*)* @thread_start, i8* null)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = call i32 (...) @lock()
%27 = load i64, i64* %2
%28 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %27)
%29 = call i32 (...) @unlock()
%30 = load i64, i64* %2
%31 = call i32 @pthread_join(i64 %30, i8** %3)
%32 = icmp eq i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %25
%37 = call i32 @pthread_create(i64* %2, i32* null, i8* (i8*)* @thread_start, i8* inttoptr (i64 1 to i8*))
%38 = icmp eq i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %36
%43 = call i32 (...) @lock()
%44 = load i64, i64* %2
%45 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %44)
%46 = call i32 (...) @unlock()
%47 = load i64, i64* %2
%48 = call i32 @pthread_join(i64 %47, i8** %3)
%49 = icmp eq i32 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %42
%51 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %42
br label %54
54: ; preds = %53, %13
ret i8* null
}