#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 */
int /*<<< orphan*/ printf(char *, unsigned int, int);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
scalar_t__ pthread_self();
void *doit(void *vptr) {
  int i, val;
  int localcnt = 0;
  pthread_mutex_t localmutex;
  for (i = 0; i < 5000; i++) {
    pthread_mutex_lock(&localmutex);
    val = localcnt;
    printf("%x: %d\n", (unsigned int)pthread_self(), val + 1);
    localcnt = val + 1;
    pthread_mutex_unlock(&localmutex);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<doit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x1388,-0xc(%rbp)
jge    11b1 <doit+0x71>
lea    -0x18(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <pthread_self@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
add    $0x1,%edx
lea    0xe74(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    115a <doit+0x1a>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doit>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x1,%ebx
lea    0xc(%rsp),%r14
lea    0xea6(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1060 <pthread_mutex_lock@plt>
xor    %eax,%eax
call   1050 <pthread_self@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %ebx,%edx
call   1030 <printf@plt>
mov    %r14,%rdi
call   1040 <pthread_mutex_unlock@plt>
add    $0x1,%ebx
cmp    $0x1389,%ebx
jne    1160 <doit+0x20>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"%x: %d\0A\00"
define i8* @doit(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
store i32 0, i32* %3
br label %7
7: ; preds = %21, %1
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 5000
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = call i32 @pthread_mutex_lock(i32* %6)
%12 = load i32, i32* %5
store i32 %12, i32* %4
%13 = call i64 (...) @pthread_self()
%14 = trunc i64 %13 to i32
%15 = load i32, i32* %4
%16 = add nsw i32 %15, 1
%17 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %16)
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %5
%20 = call i32 @pthread_mutex_unlock(i32* %6)
br label %21
21: ; preds = %10
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %7
24: ; preds = %7
ret i8* null
}

LLVM IR (-O2):
@.str = [8 x i8] c"%x: %d\0A\00"
define noalias i8* @doit(i8* nocapture readnone %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %1, %4
%5 = phi i32 [ 0, %1 ], [ %9, %4 ]
%6 = call i32 @pthread_mutex_lock(i32* %2)
%7 = call i64 (...) @pthread_self()
%8 = trunc i64 %7 to i32
%9 = add nuw nsw i32 %5, 1
%10 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
%11 = call i32 @pthread_mutex_unlock(i32* %2)
%12 = icmp eq i32 %9, 5000
br i1 %12, label %13, label %4
13: ; preds = %4
ret i8* null
}