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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int setlock(int fd) {
  struct flock lock, savelock;
  lock.l_type = F_WRLCK;
  lock.l_start = 0;
  lock.l_whence = SEEK_SET;
  lock.l_len = 0;
  savelock = lock;
  fcntl(fd, F_GETLK, &lock);
  if (lock.l_type == F_WRLCK) {
    return 1;
  } else if (lock.l_type == F_RDLCK) {
    return 1;
  } else
    fcntl(fd, F_SETLK, &savelock);
  return 0;
}


x86 assembly (clang, -O0):
<setlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x28(%rbp),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <setlock+0x8a>
movl   $0x1,-0x4(%rbp)
jmp    11db <setlock+0xcb>
mov    -0x28(%rbp),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
jne    11ba <setlock+0xaa>
movl   $0x1,-0x4(%rbp)
jmp    11db <setlock+0xcb>
mov    -0x8(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1030 <fcntl@plt>
jmp    11d4 <setlock+0xc4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setlock>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
mov    0x2ea8(%rip),%r14
mov    (%r14),%rax
mov    %rax,(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    (%rsp),%rax
mov    %rax,0x20(%rsp)
mov    0x8(%rsp),%rax
mov    %rax,0x28(%rsp)
mov    0x10(%rsp),%eax
mov    %eax,0x30(%rsp)
mov    0x14(%rsp),%eax
mov    %eax,0x34(%rsp)
mov    0x18(%rsp),%rax
mov    %rax,0x38(%rsp)
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
mov    (%rsp),%rcx
mov    $0x1,%eax
cmp    (%r14),%rcx
je     11b5 <setlock+0xa5>
mov    0x2e1f(%rip),%rdx
cmp    (%rdx),%rcx
je     11b5 <setlock+0xa5>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i64 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_RDLCK = global i64 0
@F_SETLK = global i32 0
define i32 @setlock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.flock
%5 = alloca %struct.flock
store i32 %0, i32* %3
%6 = load i64, i64* @F_WRLCK
%7 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
store i64 %6, i64* %7
%8 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 3
store i64 0, i64* %8
%9 = load i32, i32* @SEEK_SET
%10 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 1
store i64 0, i64* %11
%12 = bitcast %struct.flock* %5 to i8*
%13 = bitcast %struct.flock* %4 to i8*
%14 = load i32, i32* %3
%15 = load i32, i32* @F_GETLK
%16 = call i32 @fcntl(i32 %14, i32 %15, %struct.flock* %4)
%17 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* @F_WRLCK
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %1
store i32 1, i32* %2
br label %34
22: ; preds = %1
%23 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i64, i64* @F_RDLCK
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 1, i32* %2
br label %34
28: ; preds = %22
%29 = load i32, i32* %3
%30 = load i32, i32* @F_SETLK
%31 = call i32 @fcntl(i32 %29, i32 %30, %struct.flock* %5)
br label %32
32: ; preds = %28
br label %33
33: ; preds = %32
store i32 0, i32* %2
br label %34
34: ; preds = %33, %27, %21
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i64 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_RDLCK = global i64 0
@F_SETLK = global i32 0
define i32 @setlock(i32 %0) {
%2 = alloca %struct.flock
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %2 to i8*
%5 = bitcast %struct.flock* %3 to i8*
%6 = load i64, i64* @F_WRLCK
%7 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
store i64 %6, i64* %7
%8 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
store i64 0, i64* %8
%9 = load i32, i32* @SEEK_SET
%10 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* @F_GETLK
%13 = call i32 @fcntl(i32 %0, i32 %12, %struct.flock* %2)
%14 = load i64, i64* %7
%15 = load i64, i64* @F_WRLCK
%16 = icmp eq i64 %14, %15
%17 = load i64, i64* @F_RDLCK
%18 = icmp eq i64 %14, %17
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %23, label %20
20: ; preds = %1
%21 = load i32, i32* @F_SETLK
%22 = call i32 @fcntl(i32 %0, i32 %21, %struct.flock* %3)
br label %23
23: ; preds = %1, %20
%24 = phi i32 [ 0, %20 ], [ 1, %1 ]
ret i32 %24
}