#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 /*<<< orphan*/ str2;
typedef int /*<<< orphan*/ str1;

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_CANCEL_DISABLE;
int /*<<< orphan*/ PTHREAD_CANCEL_ENABLE;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ pthread_cancel(int /*<<< orphan*/);
int /*<<< orphan*/ pthread_self();
int /*<<< orphan*/ pthread_setcancelstate(int /*<<< orphan*/,
                                          int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(void) {
  char str1[] = "You should see this.\n";
  char str2[] = "You should not see this.\n";
  printf("Cancellation test: Self-cancellation and disabling.\n");
  pthread_cancel(pthread_self());
  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  write(STDOUT_FILENO, str1, sizeof(str1) - 1);
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  write(STDOUT_FILENO, str2, sizeof(str2) - 1);
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe87(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe85(%rip),%eax
mov    %eax,-0x10(%rbp)
mov    0xe7f(%rip),%ax
mov    %ax,-0xc(%rbp)
mov    0xe80(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe7d(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe77(%rip),%ax
mov    %ax,-0x28(%rbp)
lea    0xe6e(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%edi
call   1080 <pthread_cancel@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <pthread_setcancelstate@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
mov    $0x15,%edx
call   1030 <write@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <pthread_setcancelstate@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
mov    $0x19,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x40,%rsp
movaps 0xe94(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0xa2e7369687420,%rax
mov    %rax,0xe(%rsp)
movups 0xea4(%rip),%xmm0
movups %xmm0,0x2a(%rsp)
movaps 0xe8e(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <pthread_self@plt>
mov    %eax,%edi
call   1080 <pthread_cancel@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <pthread_setcancelstate@plt>
mov    0x2e28(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%rsi
mov    $0x15,%edx
call   1040 <write@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <pthread_setcancelstate@plt>
mov    (%rbx),%edi
lea    0x20(%rsp),%rsi
mov    $0x19,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@__const.main.str1 = [22 x i8] c"You should see this.\0A\00"
@__const.main.str2 = [26 x i8] c"You should not see this.\0A\00"
@.str = [53 x i8] c"Cancellation test: Self-cancellation and disabling.\0A\00"
@PTHREAD_CANCEL_DISABLE = global i32 0
@STDOUT_FILENO = global i32 0
@PTHREAD_CANCEL_ENABLE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [22 x i8]
%3 = alloca [26 x i8]
store i32 0, i32* %1
%4 = bitcast [22 x i8]* %2 to i8*
%5 = bitcast [26 x i8]* %3 to i8*
%6 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (...) @pthread_self()
%8 = call i32 @pthread_cancel(i32 %7)
%9 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%10 = call i32 @pthread_setcancelstate(i32 %9, i32* null)
%11 = load i32, i32* @STDOUT_FILENO
%12 = getelementptr inbounds [22 x i8], [22 x i8]* %2, i64 0, i64 0
%13 = call i32 @write(i32 %11, i8* %12, i32 21)
%14 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%15 = call i32 @pthread_setcancelstate(i32 %14, i32* null)
%16 = load i32, i32* @STDOUT_FILENO
%17 = getelementptr inbounds [26 x i8], [26 x i8]* %3, i64 0, i64 0
%18 = call i32 @write(i32 %16, i8* %17, i32 25)
%19 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@__const.main.str1 = [22 x i8] c"You should see this.\0A\00"
@__const.main.str2 = [26 x i8] c"You should not see this.\0A\00"
@PTHREAD_CANCEL_DISABLE = global i32 0
@STDOUT_FILENO = global i32 0
@PTHREAD_CANCEL_ENABLE = global i32 0
@str = [52 x i8] c"Cancellation test: Self-cancellation and disabling.\00"
define i32 @main() {
%1 = alloca [22 x i8]
%2 = alloca [26 x i8]
%3 = getelementptr inbounds [22 x i8], [22 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [26 x i8], [26 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (...) @pthread_self()
%7 = tail call i32 @pthread_cancel(i32 %6)
%8 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%9 = tail call i32 @pthread_setcancelstate(i32 %8, i32* null)
%10 = load i32, i32* @STDOUT_FILENO
%11 = call i32 @write(i32 %10, i8* %3, i32 21)
%12 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%13 = tail call i32 @pthread_setcancelstate(i32 %12, i32* null)
%14 = load i32, i32* @STDOUT_FILENO
%15 = call i32 @write(i32 %14, i8* %4, i32 25)
%16 = tail call i32 @exit(i32 1)
unreachable
}