#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_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int unlock_file(char *pathname) {
  int fd;
  struct flock lock;
  char filename[1024] = {0};
  strncpy(filename, pathname, sizeof(filename) - 1);
  if ((fd = open(filename, O_RDWR | O_EXCL, S_IRUSR | S_IWUSR)) == -1) {
    goto out;
  }
  lock.l_type = F_UNLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  if (fcntl(fd, F_SETLKW, &lock) == -1) {
    goto err;
  }
  return 0;
err:
  close(fd);
out:
  return -1;
}


x86 assembly (clang, -O0):
<unlock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    -0x430(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    0x2e28(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11c8 <unlock_file+0x78>
jmp    1227 <unlock_file+0xd7>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1213 <unlock_file+0xc3>
jmp    121f <unlock_file+0xcf>
movl   $0x0,-0x4(%rbp)
jmp    122e <unlock_file+0xde>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlock_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %rdi,%rbp
lea    0x20(%rsp),%rbx
xor    %r14d,%r14d
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
mov    0x2e5c(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e12(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f8 <unlock_file+0xa8>
mov    %eax,%ebp
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11fe <unlock_file+0xae>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_UNLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
define i32 @unlock_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.flock
%6 = alloca [1024 x i8]
store i8* %0, i8** %3
%7 = bitcast [1024 x i8]* %6 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 @strncpy(i8* %8, i8* %9, i32 1023)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %11, i32 %14, i32 %17)
store i32 %18, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %1
br label %37
21: ; preds = %1
%22 = load i32, i32* @F_UNLCK
%23 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i32 %22, i32* %23
%24 = load i32, i32* @SEEK_SET
%25 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i64 0, i64* %26
%27 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i64 0, i64* %27
%28 = load i32, i32* %4
%29 = load i32, i32* @F_SETLKW
%30 = call i32 @fcntl(i32 %28, i32 %29, %struct.flock* %5)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %21
br label %34
33: ; preds = %21
store i32 0, i32* %2
br label %38
34: ; preds = %32
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34, %20
store i32 -1, i32* %2
br label %38
38: ; preds = %37, %33
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_UNLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
define i32 @unlock_file(i8* %0) {
%2 = alloca %struct.flock
%3 = alloca [1024 x i8]
%4 = bitcast %struct.flock* %2 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = call i32 @strncpy(i8* %5, i8* %0, i32 1023)
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_EXCL
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %11, %10
%13 = call i32 @open(i8* %5, i32 %9, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %26, label %15
15: ; preds = %1
%16 = load i32, i32* @F_UNLCK
%17 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
store i32 %16, i32* %17
%18 = load i32, i32* @SEEK_SET
%19 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
store i32 %18, i32* %19
%20 = bitcast %struct.flock* %2 to i8*
%21 = load i32, i32* @F_SETLKW
%22 = call i32 @fcntl(i32 %13, i32 %21, %struct.flock* %2)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 @close(i32 %13)
br label %26
26: ; preds = %24, %1, %15
%27 = phi i32 [ 0, %15 ], [ -1, %1 ], [ -1, %24 ]
ret i32 %27
}