#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_DEFERRED;
int /*<<< orphan*/ PTHREAD_CANCEL_ENABLE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/);
int pthread_setcancelstate(int /*<<< orphan*/, int /*<<< orphan*/ *);
int pthread_setcanceltype(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
void *thread_function(void *arg) {
  int i, res, j;
  res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  if (res != 0) {
    perror("Thread pthread_setcancelstate failed");
    exit(EXIT_FAILURE);
  }
  res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  if (res != 0) {
    perror("Thread pthread_setcanceltype failed");
    exit(EXIT_FAILURE);
  }
  printf("thread_function is running\n");
  for (i = 0; i < 10; i++) {
    printf("Thread is still running (%d)...\n", i);
    sleep(1);
  }
  pthread_exit(0);
}


x86 assembly (clang, -O0):
<thread_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <pthread_setcancelstate@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11b5 <thread_function+0x45>
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <pthread_setcanceltype@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11ee <thread_function+0x7e>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    1236 <thread_function+0xc6>
mov    -0x14(%rbp),%esi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1203 <thread_function+0x93>
xor    %edi,%edi
call   1050 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_function>:
push   %rbx
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <pthread_setcancelstate@plt>
test   %eax,%eax
jne    12c3 <thread_function+0x143>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1080 <pthread_setcanceltype@plt>
test   %eax,%eax
jne    12cc <thread_function+0x14c>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe85(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %edi,%edi
pop    %rbx
jmp    1060 <pthread_exit@plt>
lea    0xd36(%rip),%rdi
jmp    12d3 <thread_function+0x153>
lea    0xd52(%rip),%rdi
call   1070 <perror@plt>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@PTHREAD_CANCEL_ENABLE = global i32 0
@.str = [37 x i8] c"Thread pthread_setcancelstate failed\00"
@EXIT_FAILURE = global i32 0
@PTHREAD_CANCEL_DEFERRED = global i32 0
@.str.1 = [36 x i8] c"Thread pthread_setcanceltype failed\00"
@.str.2 = [28 x i8] c"thread_function is running\0A\00"
@.str.3 = [33 x i8] c"Thread is still running (%d)...\0A\00"
define i8* @thread_function(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%8 = call i32 @pthread_setcancelstate(i32 %7, i32* null)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%17 = call i32 @pthread_setcanceltype(i32 %16, i32* null)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %26
26: ; preds = %33, %24
%27 = load i32, i32* %4
%28 = icmp slt i32 %27, 10
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = load i32, i32* %4
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = call i32 @sleep(i32 1)
br label %33
33: ; preds = %29
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
br label %26
36: ; preds = %26
%37 = call i32 @pthread_exit(i32 0)
%38 = load i8*, i8** %2
ret i8* %38
}

LLVM IR (-O2):
@PTHREAD_CANCEL_ENABLE = global i32 0
@.str = [37 x i8] c"Thread pthread_setcancelstate failed\00"
@EXIT_FAILURE = global i32 0
@PTHREAD_CANCEL_DEFERRED = global i32 0
@.str.1 = [36 x i8] c"Thread pthread_setcanceltype failed\00"
@.str.3 = [33 x i8] c"Thread is still running (%d)...\0A\00"
@str = [27 x i8] c"thread_function is running\00"
define noalias i8* @thread_function(i8* nocapture readnone %0) {
%2 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%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
%6 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 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 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%11 = tail call i32 @pthread_setcanceltype(i32 %10, i32* null)
%12 = icmp eq i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %9
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 0)
%20 = tail call i32 @sleep(i32 1)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 1)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 2)
%24 = tail call i32 @sleep(i32 1)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 3)
%26 = tail call i32 @sleep(i32 1)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 4)
%28 = tail call i32 @sleep(i32 1)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 5)
%30 = tail call i32 @sleep(i32 1)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 6)
%32 = tail call i32 @sleep(i32 1)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 7)
%34 = tail call i32 @sleep(i32 1)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 8)
%36 = tail call i32 @sleep(i32 1)
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 9)
%38 = tail call i32 @sleep(i32 1)
%39 = tail call i32 @pthread_exit(i32 0)
ret i8* undef
}