#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 */
struct ptl_lock {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/);
scalar_t__ pthread_self();
int /*<<< orphan*/ ptl_lock(struct ptl_lock *);
int /*<<< orphan*/ ptl_unlock(struct ptl_lock *);
int /*<<< orphan*/ usleep(int);
void *thread4(void *args) {
  struct ptl_lock *lock;
  lock = args;
  usleep(5 * 1000 * 1000);
  printf("thread4-%d: test: race for lock\n", (int)pthread_self());
  ptl_lock(lock);
  printf("thread4-%d: pass: race for lock\n", (int)pthread_self());
  printf("thread4-%d: debug: release lock\n", (int)pthread_self());
  ptl_unlock(lock);
  pthread_exit(0);
}


x86 assembly (clang, -O0):
<thread4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x4c4b40,%edi
call   1080 <usleep@plt>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xe72(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <ptl_lock@plt>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xe75(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xe81(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <ptl_unlock@plt>
xor    %edi,%edi
call   1060 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread4>:
push   %rbx
mov    %rdi,%rbx
mov    $0x4c4b40,%edi
call   1080 <usleep@plt>
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0xe84(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <ptl_lock@plt>
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0xe88(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0xe94(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <ptl_unlock@plt>
xor    %edi,%edi
pop    %rbx
jmp    1060 <pthread_exit@plt>

LLVM IR (-O0):
%struct.ptl_lock = type { i32 }
@.str = [33 x i8] c"thread4-%d: test: race for lock\0A\00"
@.str.1 = [33 x i8] c"thread4-%d: pass: race for lock\0A\00"
@.str.2 = [33 x i8] c"thread4-%d: debug: release lock\0A\00"
define i8* @thread4(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.ptl_lock*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = bitcast i8* %5 to %struct.ptl_lock*
store %struct.ptl_lock* %6, %struct.ptl_lock** %4
%7 = call i32 @usleep(i32 5000000)
%8 = call i64 (...) @pthread_self()
%9 = trunc i64 %8 to i32
%10 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load %struct.ptl_lock*, %struct.ptl_lock** %4
%12 = call i32 @ptl_lock(%struct.ptl_lock* %11)
%13 = call i64 (...) @pthread_self()
%14 = trunc i64 %13 to i32
%15 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i64 (...) @pthread_self()
%17 = trunc i64 %16 to i32
%18 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = load %struct.ptl_lock*, %struct.ptl_lock** %4
%20 = call i32 @ptl_unlock(%struct.ptl_lock* %19)
%21 = call i32 @pthread_exit(i32 0)
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
%struct.ptl_lock = type { i32 }
@.str = [33 x i8] c"thread4-%d: test: race for lock\0A\00"
@.str.1 = [33 x i8] c"thread4-%d: pass: race for lock\0A\00"
@.str.2 = [33 x i8] c"thread4-%d: debug: release lock\0A\00"
define noalias i8* @thread4(i8* %0) {
%2 = bitcast i8* %0 to %struct.ptl_lock*
%3 = tail call i32 @usleep(i32 5000000)
%4 = tail call i64 (...) @pthread_self()
%5 = trunc i64 %4 to i32
%6 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 @ptl_lock(%struct.ptl_lock* %2)
%8 = tail call i64 (...) @pthread_self()
%9 = trunc i64 %8 to i32
%10 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i64 (...) @pthread_self()
%12 = trunc i64 %11 to i32
%13 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = tail call i32 @ptl_unlock(%struct.ptl_lock* %2)
%15 = tail call i32 @pthread_exit(i32 0)
ret i8* undef
}