#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_ENABLE;
int /*<<< orphan*/ abort();
scalar_t__ pthread_setcancelstate(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_testcancel();
int /*<<< orphan*/ sched_yield();
int /*<<< orphan*/ write(int, char *, int);
void *process(void *arg) {
  int i;
  if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
    abort();
  write(2, "a", 1);
  for (i = 0; i < 10; i++) {
    sched_yield();
    pthread_testcancel();
    write(2, "b", 1);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_setcancelstate@plt>
cmp    $0x0,%rax
je     117f <process+0x2f>
mov    $0x0,%al
call   1030 <abort@plt>
mov    $0x2,%edi
lea    0xe75(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    11d8 <process+0x88>
mov    $0x0,%al
call   1060 <sched_yield@plt>
mov    $0x0,%al
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
lea    0xe42(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    119c <process+0x4c>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <pthread_setcancelstate@plt>
test   %rax,%rax
jne    12cb <process+0x17b>
lea    0xe8f(%rip),%rsi
mov    $0x2,%edi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
lea    0xe6d(%rip),%rbx
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <sched_yield@plt>
xor    %eax,%eax
call   1070 <pthread_testcancel@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
pop    %rbx
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@PTHREAD_CANCEL_ENABLE = global i32 0
@.str = [2 x i8] c"a\00"
@.str.1 = [2 x i8] c"b\00"
define i8* @process(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%5 = call i64 @pthread_setcancelstate(i32 %4, i32* null)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 (...) @abort()
unreachable
9: ; preds = %1
%10 = call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 0, i32* %3
br label %11
11: ; preds = %18, %9
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 10
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = call i32 (...) @sched_yield()
%16 = call i32 (...) @pthread_testcancel()
%17 = call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %18
18: ; preds = %14
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 1
store i32 %20, i32* %3
br label %11
21: ; preds = %11
ret i8* null
}

LLVM IR (-O2):
@PTHREAD_CANCEL_ENABLE = global i32 0
@.str = [2 x i8] c"a\00"
@.str.1 = [2 x i8] c"b\00"
define noalias i8* @process(i8* nocapture readnone %0) {
%2 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%3 = tail call i64 @pthread_setcancelstate(i32 %2, i32* null)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = tail call i32 (...) @abort()
unreachable
7: ; preds = %1
%8 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%9 = tail call i32 (...) @sched_yield()
%10 = tail call i32 (...) @pthread_testcancel()
%11 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%12 = tail call i32 (...) @sched_yield()
%13 = tail call i32 (...) @pthread_testcancel()
%14 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%15 = tail call i32 (...) @sched_yield()
%16 = tail call i32 (...) @pthread_testcancel()
%17 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%18 = tail call i32 (...) @sched_yield()
%19 = tail call i32 (...) @pthread_testcancel()
%20 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%21 = tail call i32 (...) @sched_yield()
%22 = tail call i32 (...) @pthread_testcancel()
%23 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%24 = tail call i32 (...) @sched_yield()
%25 = tail call i32 (...) @pthread_testcancel()
%26 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%27 = tail call i32 (...) @sched_yield()
%28 = tail call i32 (...) @pthread_testcancel()
%29 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%30 = tail call i32 (...) @sched_yield()
%31 = tail call i32 (...) @pthread_testcancel()
%32 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%33 = tail call i32 (...) @sched_yield()
%34 = tail call i32 (...) @pthread_testcancel()
%35 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%36 = tail call i32 (...) @sched_yield()
%37 = tail call i32 (...) @pthread_testcancel()
%38 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
ret i8* null
}