#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 BUFSIZ;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ printf(char *, int, ...);
int ref_count(int *);
int ref_pthread_mutex_lock(int *);
int /*<<< orphan*/ ref_pthread_mutex_unlock(int *);
int /*<<< orphan*/ ref_release(int *);
int /*<<< orphan*/ sched_yield();
int /*<<< orphan*/ stderr;
int strerror_r(int, char *, int);
void *incr(void *arg) {
  int i;
  int *an_int;
  int lock_ret;
  an_int = (int *)arg;
  printf("\tincr in ref count: %d\n", ref_count(an_int));
  for (i = 0; i < 100000; ++i) {
    if ((lock_ret = ref_pthread_mutex_lock(an_int))) {
      char error_message[BUFSIZ];
      int strerror_error;
      strerror_error = strerror_r(lock_ret, error_message, BUFSIZ);
      if (strerror_error)
        printf("incr could not lock (%d), could not get error message (%d)\n",
               lock_ret, strerror_error);
      else
        fprintf(stderr, "incr could not lock (%d): %s\n", lock_ret,
                error_message);
      exit(1);
    }
    (*an_int)++;
    ref_pthread_mutex_unlock(an_int);
    sched_yield();
  }
  ref_release(an_int);
  printf("\tincr ref count after release: %d\n", ref_count(an_int));
  return 0;
}


x86 assembly (clang, -O0):
<incr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <ref_count@plt>
mov    %eax,%esi
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x186a0,-0xc(%rbp)
jge    1298 <incr+0x108>
mov    -0x18(%rbp),%rdi
call   1070 <ref_pthread_mutex_lock@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     126f <incr+0xdf>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x40(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
call   1060 <strerror_r@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1249 <incr+0xb9>
mov    -0x1c(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1265 <incr+0xd5>
mov    -0x40(%rbp),%rcx
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xdf4(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
call   10b0 <ref_pthread_mutex_unlock@plt>
mov    $0x0,%al
call   1040 <sched_yield@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c4 <incr+0x34>
mov    -0x18(%rbp),%rdi
call   1080 <ref_release@plt>
mov    -0x18(%rbp),%rdi
call   1090 <ref_count@plt>
mov    %eax,%esi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<incr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1090 <ref_count@plt>
lea    0xe57(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x186a0,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1070 <ref_pthread_mutex_lock@plt>
test   %eax,%eax
jne    1211 <incr+0x81>
addl   $0x1,(%r14)
mov    %r14,%rdi
call   10b0 <ref_pthread_mutex_unlock@plt>
xor    %eax,%eax
call   1040 <sched_yield@plt>
add    $0xffffffff,%ebx
jne    11c0 <incr+0x30>
mov    %r14,%rdi
call   1080 <ref_release@plt>
mov    %r14,%rdi
call   1090 <ref_count@plt>
lea    0xe77(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r15d
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r14
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %r15d,%edi
mov    %r14,%rsi
call   1060 <strerror_r@plt>
test   %eax,%eax
jne    1265 <incr+0xd5>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
mov    %r15d,%edx
mov    %r14,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdac(%rip),%rdi
mov    %r15d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"\09incr in ref count: %d\0A\00"
@BUFSIZ = global i32 0
@.str.1 = [60 x i8] c"incr could not lock (%d), could not get error message (%d)\0A\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"incr could not lock (%d): %s\0A\00"
@.str.3 = [35 x i8] c"\09incr ref count after release: %d\0A\00"
define i8* @incr(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = call i32 @ref_count(i32* %11)
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 0, i32* %3
br label %14
14: ; preds = %48, %1
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 100000
br i1 %16, label %17, label %51
17: ; preds = %14
%18 = load i32*, i32** %4
%19 = call i32 @ref_pthread_mutex_lock(i32* %18)
store i32 %19, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %41
21: ; preds = %17
%22 = load i32, i32* @BUFSIZ
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
store i8* %24, i8** %6
%25 = alloca i8, i64 %23
store i64 %23, i64* %7
%26 = load i32, i32* %5
%27 = load i32, i32* @BUFSIZ
%28 = call i32 @strerror_r(i32 %26, i8* %25, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = load i32, i32* %5
%33 = load i32, i32* %8
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %33)
br label %39
35: ; preds = %21
%36 = load i32, i32* @stderr
%37 = load i32, i32* %5
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %37, i8* %25)
br label %39
39: ; preds = %35, %31
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %17
%42 = load i32*, i32** %4
%43 = load i32, i32* %42
%44 = add nsw i32 %43, 1
store i32 %44, i32* %42
%45 = load i32*, i32** %4
%46 = call i32 @ref_pthread_mutex_unlock(i32* %45)
%47 = call i32 (...) @sched_yield()
br label %48
48: ; preds = %41
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
br label %14
51: ; preds = %14
%52 = load i32*, i32** %4
%53 = call i32 @ref_release(i32* %52)
%54 = load i32*, i32** %4
%55 = call i32 @ref_count(i32* %54)
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %55)
ret i8* null
}

LLVM IR (-O2):
@.str = [24 x i8] c"\09incr in ref count: %d\0A\00"
@BUFSIZ = global i32 0
@.str.1 = [60 x i8] c"incr could not lock (%d), could not get error message (%d)\0A\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"incr could not lock (%d): %s\0A\00"
@.str.3 = [35 x i8] c"\09incr ref count after release: %d\0A\00"
define noalias i8* @incr(i8* %0) {
%2 = bitcast i8* %0 to i32*
%3 = tail call i32 @ref_count(i32* %2)
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %5
5: ; preds = %1, %22
%6 = phi i32 [ 0, %1 ], [ %27, %22 ]
%7 = tail call i32 @ref_pthread_mutex_lock(i32* %2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %5
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = alloca i8, i64 %11
%13 = call i32 @strerror_r(i32 %7, i8* %12, i32 %10)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %13)
br label %20
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %7, i8* %12)
br label %20
20: ; preds = %17, %15
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %5
%23 = load i32, i32* %2
%24 = add nsw i32 %23, 1
store i32 %24, i32* %2
%25 = tail call i32 @ref_pthread_mutex_unlock(i32* %2)
%26 = tail call i32 (...) @sched_yield()
%27 = add nuw nsw i32 %6, 1
%28 = icmp eq i32 %27, 100000
br i1 %28, label %29, label %5
29: ; preds = %22
%30 = tail call i32 @ref_release(i32* %2)
%31 = tail call i32 @ref_count(i32* %2)
%32 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %31)
ret i8* null
}