#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 flock {
  int l_len;
  scalar_t__ l_pid;
  scalar_t__ l_start;
  scalar_t__ l_whence;
  int /*<<< orphan*/ l_type;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pthread_exit(void *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
void *test_thread(void *thread_parm) {
  int *status = thread_parm;
  int fd, ret;
  struct flock lock;
  sleep(2);
  fd = open("conftest.fcntl", O_RDWR);
  if (fd == -1) {
    fprintf(stderr, "ERROR: failed to open %s (errno=%d)\n", "conftest.fcntl",
            (int)errno);
    pthread_exit(thread_parm);
  }
  lock.l_type = F_WRLCK;
  lock.l_whence = 0;
  lock.l_start = 0;
  lock.l_len = 4;
  lock.l_pid = 0;
  ret = fcntl(fd, F_SETLK, &lock);
  if ((ret != -1)) {
    fprintf(stderr, "ERROR: lock test failed (ret=%d errno=%d)\n", ret,
            (int)errno);
  } else {
    *status = 0;
  }
  pthread_exit(thread_parm);
}


x86 assembly (clang, -O0):
<test_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c3 <test_thread+0x73>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    %eax,%ecx
lea    0xe63(%rip),%rsi
lea    0xe4d(%rip),%rdx
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <pthread_exit@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x4,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     123b <test_thread+0xeb>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
mov    0x2da5(%rip),%rax
mov    (%rax),%rax
mov    %eax,%ecx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1245 <test_thread+0xf5>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_thread>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ae <test_thread+0x5e>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
lea    0xe77(%rip),%rsi
lea    0xe61(%rip),%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <pthread_exit@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
movl   $0x4,(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     120f <test_thread+0xbf>
mov    0x2dde(%rip),%rcx
mov    (%rcx),%edi
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%ecx
lea    0xe30(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1215 <test_thread+0xc5>
movl   $0x0,(%rbx)
mov    %rbx,%rdi
call   1050 <pthread_exit@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i64, i32 }
@.str = [15 x i8] c"conftest.fcntl\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"ERROR: failed to open %s (errno=%d)\0A\00"
@errno = global i64 0
@F_WRLCK = global i32 0
@F_SETLK = global i32 0
@.str.2 = [43 x i8] c"ERROR: lock test failed (ret=%d errno=%d)\0A\00"
define i8* @test_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.flock
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to i32*
store i32* %9, i32** %4
%10 = call i32 @sleep(i32 2)
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %22
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i64, i64* @errno
%18 = trunc i64 %17 to i32
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i8*, i8** %3
%21 = call i32 @pthread_exit(i8* %20)
br label %22
22: ; preds = %15, %1
%23 = load i32, i32* @F_WRLCK
%24 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 4
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i64 0, i64* %26
%27 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 4, i32* %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i64 0, i64* %28
%29 = load i32, i32* %5
%30 = load i32, i32* @F_SETLK
%31 = call i32 @fcntl(i32 %29, i32 %30, %struct.flock* %7)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %22
%35 = load i32, i32* @stderr
%36 = load i32, i32* %6
%37 = load i64, i64* @errno
%38 = trunc i64 %37 to i32
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %38)
br label %42
40: ; preds = %22
%41 = load i32*, i32** %4
store i32 0, i32* %41
br label %42
42: ; preds = %40, %34
%43 = load i8*, i8** %3
%44 = call i32 @pthread_exit(i8* %43)
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i64, i32 }
@.str = [15 x i8] c"conftest.fcntl\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"ERROR: failed to open %s (errno=%d)\0A\00"
@errno = global i64 0
@F_WRLCK = global i32 0
@F_SETLK = global i32 0
@.str.2 = [43 x i8] c"ERROR: lock test failed (ret=%d errno=%d)\0A\00"
define noalias i8* @test_thread(i8* %0) {
%2 = alloca %struct.flock
%3 = bitcast i8* %0 to i32*
%4 = bitcast %struct.flock* %2 to i8*
%5 = tail call i32 @sleep(i32 2)
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i64, i64* @errno
%12 = trunc i64 %11 to i32
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = tail call i32 @pthread_exit(i8* %0)
br label %15
15: ; preds = %9, %1
%16 = load i32, i32* @F_WRLCK
%17 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 4
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
%19 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
%20 = bitcast i64* %18 to i8*
store i32 4, i32* %19
%21 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
store i64 0, i64* %21
%22 = load i32, i32* @F_SETLK
%23 = call i32 @fcntl(i32 %7, i32 %22, %struct.flock* %2)
%24 = icmp eq i32 %23, -1
br i1 %24, label %30, label %25
25: ; preds = %15
%26 = load i32, i32* @stderr
%27 = load i64, i64* @errno
%28 = trunc i64 %27 to i32
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %28)
br label %31
30: ; preds = %15
store i32 0, i32* %3
br label %31
31: ; preds = %30, %25
%32 = call i32 @pthread_exit(i8* %0)
ret i8* undef
}