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

/* Variables and functions */
scalar_t__ EBUSY;
int /*<<< orphan*/ clear_recorded_errors();
int find_recorded_error(scalar_t__);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
scalar_t__ pthread_mutex_destroy(int /*<<< orphan*/ *);
int pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int pthread_mutex_lock(int /*<<< orphan*/ *);
int pthread_mutex_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int test_destroy_while_same_thread_has_locked(void) {
  pthread_mutex_t mutex;
  do {
    int __my_ret__ = pthread_mutex_init(&mutex, NULL);
    if (__my_ret__) {
      fprintf(stderr, "failed on line %d: %s\n", 107,
              "pthread_mutex_init(&mutex, NULL)");
      return __my_ret__;
    }
  } while (0);
  ;
  do {
    int __my_ret__ = pthread_mutex_lock(&mutex);
    if (__my_ret__) {
      fprintf(stderr, "failed on line %d: %s\n", 108,
              "pthread_mutex_lock(&mutex)");
      return __my_ret__;
    }
  } while (0);
  ;
  do {
    if ((pthread_mutex_destroy(&mutex)) != (EBUSY)) {
      fprintf(stderr, "failed on line %d: %s\n", 109,
              "pthread_mutex_destroy(&mutex)");
      return 1;
    }
  } while (0);
  ;
  do {
    if ((find_recorded_error(EBUSY)) != (1)) {
      fprintf(stderr, "failed on line %d: %s\n", 110,
              "find_recorded_error(EBUSY)");
      return 1;
    }
  } while (0);
  ;
  do {
    int __my_ret__ = pthread_mutex_unlock(&mutex);
    if (__my_ret__) {
      fprintf(stderr, "failed on line %d: %s\n", 111,
              "pthread_mutex_unlock(&mutex)");
      return __my_ret__;
    }
  } while (0);
  ;
  do {
    int __my_ret__ = pthread_mutex_destroy(&mutex);
    if (__my_ret__) {
      fprintf(stderr, "failed on line %d: %s\n", 112,
              "pthread_mutex_destroy(&mutex)");
      return __my_ret__;
    }
  } while (0);
  ;
  clear_recorded_errors();
  return 0;
}


x86 assembly (clang, -O0):
<test_destroy_while_same_thread_has_locked>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <pthread_mutex_init@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11be <test_destroy_while_same_thread_has_locked+0x4e>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
mov    $0x6b,%edx
lea    0xe69(%rip),%rcx
call   1040 <fprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1354 <test_destroy_while_same_thread_has_locked+0x1e4>
jmp    11c3 <test_destroy_while_same_thread_has_locked+0x53>
jmp    11c8 <test_destroy_while_same_thread_has_locked+0x58>
lea    -0x8(%rbp),%rdi
call   1090 <pthread_mutex_lock@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     120a <test_destroy_while_same_thread_has_locked+0x9a>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
mov    $0x6c,%edx
lea    0xe3e(%rip),%rcx
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1354 <test_destroy_while_same_thread_has_locked+0x1e4>
jmp    120f <test_destroy_while_same_thread_has_locked+0x9f>
jmp    1214 <test_destroy_while_same_thread_has_locked+0xa4>
lea    -0x8(%rbp),%rdi
call   1030 <pthread_mutex_destroy@plt>
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%rax
je     125a <test_destroy_while_same_thread_has_locked+0xea>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
mov    $0x6d,%edx
lea    0xe0a(%rip),%rcx
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1354 <test_destroy_while_same_thread_has_locked+0x1e4>
jmp    125f <test_destroy_while_same_thread_has_locked+0xef>
jmp    1264 <test_destroy_while_same_thread_has_locked+0xf4>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rdi
call   1070 <find_recorded_error@plt>
cmp    $0x1,%eax
je     12a9 <test_destroy_while_same_thread_has_locked+0x139>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
mov    $0x6e,%edx
lea    0xdd9(%rip),%rcx
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1354 <test_destroy_while_same_thread_has_locked+0x1e4>
jmp    12ae <test_destroy_while_same_thread_has_locked+0x13e>
jmp    12b3 <test_destroy_while_same_thread_has_locked+0x143>
lea    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_unlock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12f5 <test_destroy_while_same_thread_has_locked+0x185>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
mov    $0x6f,%edx
lea    0xda7(%rip),%rcx
call   1040 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1354 <test_destroy_while_same_thread_has_locked+0x1e4>
jmp    12fa <test_destroy_while_same_thread_has_locked+0x18a>
jmp    12ff <test_destroy_while_same_thread_has_locked+0x18f>
lea    -0x8(%rbp),%rdi
call   1030 <pthread_mutex_destroy@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1341 <test_destroy_while_same_thread_has_locked+0x1d1>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
lea    0xcdb(%rip),%rsi
mov    $0x70,%edx
lea    0xd22(%rip),%rcx
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1354 <test_destroy_while_same_thread_has_locked+0x1e4>
jmp    1346 <test_destroy_while_same_thread_has_locked+0x1d6>
mov    $0x0,%al
call   1060 <clear_recorded_errors@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_destroy_while_same_thread_has_locked>:
push   %rbx
sub    $0x10,%rsp
lea    0xc(%rsp),%rdi
xor    %esi,%esi
call   1080 <pthread_mutex_init@plt>
test   %eax,%eax
je     11ad <test_destroy_while_same_thread_has_locked+0x3d>
mov    %eax,%ebx
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
lea    0xe79(%rip),%rcx
mov    $0x6b,%edx
call   1040 <fprintf@plt>
jmp    127d <test_destroy_while_same_thread_has_locked+0x10d>
lea    0xc(%rsp),%rdi
call   1090 <pthread_mutex_lock@plt>
test   %eax,%eax
je     11e3 <test_destroy_while_same_thread_has_locked+0x73>
mov    %eax,%ebx
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
lea    0xe64(%rip),%rcx
mov    $0x6c,%edx
call   1040 <fprintf@plt>
jmp    127d <test_destroy_while_same_thread_has_locked+0x10d>
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%rax
jne    1239 <test_destroy_while_same_thread_has_locked+0xc9>
mov    %rax,%rdi
call   1070 <find_recorded_error@plt>
cmp    $0x1,%eax
jne    1257 <test_destroy_while_same_thread_has_locked+0xe7>
lea    0xc(%rsp),%rdi
call   1050 <pthread_mutex_unlock@plt>
test   %eax,%eax
je     1285 <test_destroy_while_same_thread_has_locked+0x115>
mov    %eax,%ebx
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
lea    0xe5f(%rip),%rcx
mov    $0x6f,%edx
call   1040 <fprintf@plt>
jmp    127d <test_destroy_while_same_thread_has_locked+0x10d>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
lea    0xe03(%rip),%rcx
mov    $0x6d,%edx
jmp    1273 <test_destroy_while_same_thread_has_locked+0x103>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xd99(%rip),%rsi
lea    0xe03(%rip),%rcx
mov    $0x6e,%edx
call   1040 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutex_destroy@plt>
mov    %rax,%rbx
test   %ebx,%ebx
je     12b9 <test_destroy_while_same_thread_has_locked+0x149>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xd5a(%rip),%rsi
lea    0xda6(%rip),%rcx
mov    $0x70,%edx
call   1040 <fprintf@plt>
jmp    127d <test_destroy_while_same_thread_has_locked+0x10d>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <clear_recorded_errors@plt>
jmp    127d <test_destroy_while_same_thread_has_locked+0x10d>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"failed on line %d: %s\0A\00"
@.str.1 = [33 x i8] c"pthread_mutex_init(&mutex, NULL)\00"
@.str.2 = [27 x i8] c"pthread_mutex_lock(&mutex)\00"
@EBUSY = global i64 0
@.str.3 = [30 x i8] c"pthread_mutex_destroy(&mutex)\00"
@.str.4 = [27 x i8] c"find_recorded_error(EBUSY)\00"
@.str.5 = [29 x i8] c"pthread_mutex_unlock(&mutex)\00"
define i32 @test_destroy_while_same_thread_has_locked() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
br label %7
7: ; preds = %0
%8 = call i32 @pthread_mutex_init(i32* %2, i32* null)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 107, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* %3
store i32 %14, i32* %1
br label %67
15: ; preds = %7
br label %16
16: ; preds = %15
br label %17
17: ; preds = %16
%18 = call i32 @pthread_mutex_lock(i32* %2)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 108, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* %4
store i32 %24, i32* %1
br label %67
25: ; preds = %17
br label %26
26: ; preds = %25
br label %27
27: ; preds = %26
%28 = call i64 @pthread_mutex_destroy(i32* %2)
%29 = load i64, i64* @EBUSY
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 109, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %67
34: ; preds = %27
br label %35
35: ; preds = %34
br label %36
36: ; preds = %35
%37 = load i64, i64* @EBUSY
%38 = call i32 @find_recorded_error(i64 %37)
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 110, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %67
43: ; preds = %36
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
%46 = call i32 @pthread_mutex_unlock(i32* %2)
store i32 %46, i32* %5
%47 = load i32, i32* %5
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 111, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* %5
store i32 %52, i32* %1
br label %67
53: ; preds = %45
br label %54
54: ; preds = %53
br label %55
55: ; preds = %54
%56 = call i64 @pthread_mutex_destroy(i32* %2)
%57 = trunc i64 %56 to i32
store i32 %57, i32* %6
%58 = load i32, i32* %6
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %55
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 112, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%63 = load i32, i32* %6
store i32 %63, i32* %1
br label %67
64: ; preds = %55
br label %65
65: ; preds = %64
%66 = call i32 (...) @clear_recorded_errors()
store i32 0, i32* %1
br label %67
67: ; preds = %65, %60, %49, %40, %31, %21, %11
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"failed on line %d: %s\0A\00"
@.str.1 = [33 x i8] c"pthread_mutex_init(&mutex, NULL)\00"
@.str.2 = [27 x i8] c"pthread_mutex_lock(&mutex)\00"
@EBUSY = global i64 0
@.str.3 = [30 x i8] c"pthread_mutex_destroy(&mutex)\00"
@.str.4 = [27 x i8] c"find_recorded_error(EBUSY)\00"
@.str.5 = [29 x i8] c"pthread_mutex_unlock(&mutex)\00"
define i32 @test_destroy_while_same_thread_has_locked() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%4 = icmp eq i32 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 107, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %42
8: ; preds = %0
%9 = call i32 @pthread_mutex_lock(i32* %1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 108, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %42
14: ; preds = %8
%15 = call i64 @pthread_mutex_destroy(i32* %1)
%16 = load i64, i64* @EBUSY
%17 = icmp eq i64 %15, %16
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 109, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %42
21: ; preds = %14
%22 = call i32 @find_recorded_error(i64 %15)
%23 = icmp eq i32 %22, 1
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 110, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %42
27: ; preds = %21
%28 = call i32 @pthread_mutex_unlock(i32* %1)
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 111, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %42
33: ; preds = %27
%34 = call i64 @pthread_mutex_destroy(i32* %1)
%35 = trunc i64 %34 to i32
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 112, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %42
40: ; preds = %33
%41 = call i32 (...) @clear_recorded_errors()
br label %42
42: ; preds = %37, %30, %11, %5, %40, %24, %18
%43 = phi i32 [ 1, %18 ], [ 1, %24 ], [ 0, %40 ], [ %35, %37 ], [ %28, %30 ], [ %9, %11 ], [ %3, %5 ]
ret i32 %43
}