#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*/ EXIT_FAILURE;
int /*<<< orphan*/ PTHREAD_CANCEL_DISABLE;
int /*<<< orphan*/ PTHREAD_CANCEL_ENABLE;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int pthread_setcancelstate(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
void *thread_func(void *ignored_argument) {
  int s;
  s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  if (s != 0)
    do {
      errno = s;
      perror("pthread_setcancelstate");
      exit(EXIT_FAILURE);
    } while (0);
  printf("thread_func(): started; cancellation disabled\n");
  sleep(5);
  printf("thread_func(): about to enable cancellation\n");
  s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  if (s != 0)
    do {
      errno = s;
      perror("pthread_setcancelstate");
      exit(EXIT_FAILURE);
    } while (0);
  sleep(1000);
  printf("thread_func(): not canceled!\n");
  return NULL;
}


x86 assembly (clang, -O0):
<thread_func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <pthread_setcancelstate@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11a6 <thread_func+0x56>
jmp    1180 <thread_func+0x30>
mov    -0xc(%rbp),%ecx
mov    0x2e4e(%rip),%rax
mov    %ecx,(%rax)
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
lea    0xe83(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <pthread_setcancelstate@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1212 <thread_func+0xc2>
jmp    11ec <thread_func+0x9c>
mov    -0xc(%rbp),%ecx
mov    0x2de2(%rip),%rax
mov    %ecx,(%rax)
lea    0xe01(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    $0x3e8,%edi
call   1070 <sleep@plt>
lea    0xe50(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_func>:
push   %rax
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <pthread_setcancelstate@plt>
test   %eax,%eax
jne    11b5 <thread_func+0x65>
lea    0xeab(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <pthread_setcancelstate@plt>
test   %eax,%eax
jne    11b5 <thread_func+0x65>
mov    $0x3e8,%edi
call   1070 <sleep@plt>
lea    0xec5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2e1c(%rip),%rcx
mov    %eax,(%rcx)
lea    0xe3b(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PTHREAD_CANCEL_DISABLE = global i32 0
@errno = global i32 0
@.str = [23 x i8] c"pthread_setcancelstate\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [47 x i8] c"thread_func(): started; cancellation disabled\0A\00"
@.str.2 = [45 x i8] c"thread_func(): about to enable cancellation\0A\00"
@PTHREAD_CANCEL_ENABLE = global i32 0
@.str.3 = [30 x i8] c"thread_func(): not canceled!\0A\00"
define i8* @thread_func(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%5 = call i32 @pthread_setcancelstate(i32 %4, i32* null)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %1
br label %9
9: ; preds = %8
%10 = load i32, i32* %3
store i32 %10, i32* @errno
%11 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; No predecessors!
br label %15
15: ; preds = %14, %1
%16 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @sleep(i32 5)
%18 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%20 = call i32 @pthread_setcancelstate(i32 %19, i32* null)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %15
br label %24
24: ; preds = %23
%25 = load i32, i32* %3
store i32 %25, i32* @errno
%26 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; No predecessors!
br label %30
30: ; preds = %29, %15
%31 = call i32 @sleep(i32 1000)
%32 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
ret i8* null
}

LLVM IR (-O2):
@PTHREAD_CANCEL_DISABLE = global i32 0
@errno = global i32 0
@.str = [23 x i8] c"pthread_setcancelstate\00"
@EXIT_FAILURE = global i32 0
@PTHREAD_CANCEL_ENABLE = global i32 0
@str = [46 x i8] c"thread_func(): started; cancellation disabled\00"
@str.4 = [44 x i8] c"thread_func(): about to enable cancellation\00"
@str.5 = [29 x i8] c"thread_func(): not canceled!\00"
define noalias i8* @thread_func(i8* nocapture readnone %0) {
%2 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%3 = tail call i32 @pthread_setcancelstate(i32 %2, i32* null)
%4 = icmp eq i32 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %1
store i32 %3, i32* @errno
%6 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @sleep(i32 5)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.4, i64 0, i64 0))
%13 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%14 = tail call i32 @pthread_setcancelstate(i32 %13, i32* null)
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %9
store i32 %14, i32* @errno
%17 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %9
%21 = tail call i32 @sleep(i32 1000)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.5, i64 0, i64 0))
ret i8* null
}