#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 */

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_CANCEL_DEFERRED;
int /*<<< orphan*/ PTHREAD_CANCEL_ENABLE;
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ pthread_exit(char *);
int pthread_setcancelstate(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
void newthread(void) {
  int i, res;
  res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  res = pthread_setcancelstate(PTHREAD_CANCEL_DEFERRED, NULL);
  for (i = 0; i < 5; i++) {
    printf("new thread\n");
    sleep(1);
  }
  pthread_exit("thread exit!\n");
}


x86 assembly (clang, -O0):
<newthread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <pthread_setcancelstate@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <pthread_setcancelstate@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x5,-0x4(%rbp)
jge    11a7 <newthread+0x67>
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1179 <newthread+0x39>
lea    0xe5e(%rip),%rdi
call   1050 <pthread_exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newthread>:
push   %rbx
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <pthread_setcancelstate@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <pthread_setcancelstate@plt>
lea    0xea6(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xe37(%rip),%rdi
pop    %rbx
jmp    1050 <pthread_exit@plt>

LLVM IR (-O0):
@PTHREAD_CANCEL_ENABLE = global i32 0
@PTHREAD_CANCEL_DEFERRED = global i32 0
@.str = [12 x i8] c"new thread\0A\00"
@.str.1 = [14 x i8] c"thread exit!\0A\00"
define void @newthread() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%4 = call i32 @pthread_setcancelstate(i32 %3, i32* null)
store i32 %4, i32* %2
%5 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%6 = call i32 @pthread_setcancelstate(i32 %5, i32* null)
store i32 %6, i32* %2
store i32 0, i32* %1
br label %7
7: ; preds = %13, %0
%8 = load i32, i32* %1
%9 = icmp slt i32 %8, 5
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @sleep(i32 1)
br label %13
13: ; preds = %10
%14 = load i32, i32* %1
%15 = add nsw i32 %14, 1
store i32 %15, i32* %1
br label %7
16: ; preds = %7
%17 = call i32 @pthread_exit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@PTHREAD_CANCEL_ENABLE = global i32 0
@PTHREAD_CANCEL_DEFERRED = global i32 0
@.str.1 = [14 x i8] c"thread exit!\0A\00"
@str = [11 x i8] c"new thread\00"
define void @newthread() {
%1 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%2 = tail call i32 @pthread_setcancelstate(i32 %1, i32* null)
%3 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%4 = tail call i32 @pthread_setcancelstate(i32 %3, i32* null)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @sleep(i32 1)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @sleep(i32 1)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @sleep(i32 1)
%15 = tail call i32 @pthread_exit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
ret void
}