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

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, ...);
int ftruncate(int, int /*<<< orphan*/);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strlen(char *);
int write(int, char *, int);
int lock(int fd) {
  int retval, len;
  struct flock lock;
  char buffer[16];
  lock.l_type = F_WRLCK;
  lock.l_start = 0;
  lock.l_whence = SEEK_SET;
  lock.l_len = 0;
  retval = fcntl(fd, F_SETLK, &lock);
  if (retval < 0) {
    if ((errno == EACCES) || (errno == EAGAIN)) {
      return (-1);
    } else {
      perror("fatal error when locking file");
      exit(-1);
    }
  }
  retval = ftruncate(fd, 0);
  if (retval < 0) {
    perror("fatal error when emptying lockfile");
    exit(-1);
  }
  sprintf(buffer, "%d\n", getpid());
  len = strlen(buffer);
  retval = write(fd, buffer, len) < len;
  if (retval < 0) {
    perror("fatal error when writing pid to lockfile");
    exit(-1);
  }
  retval = fcntl(fd, F_GETFD, 0);
  if (retval < 0) {
    perror("fatal error when reading lockfile flags");
    exit(-1);
  }
  retval = retval | FD_CLOEXEC;
  retval = fcntl(fd, F_SETFD, retval);
  if (retval < 0) {
    perror("fatal error when setting lockfile flags");
    exit(-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    122d <lock+0xad>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
je     120b <lock+0x8b>
mov    0x2dc8(%rip),%rax
mov    (%rax),%rax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <lock+0x97>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <lock+0x1cb>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1070 <ftruncate@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    125a <lock+0xda>
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xdcb(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x40(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <write@plt>
cmp    -0x10(%rbp),%eax
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12c4 <lock+0x144>
lea    0xd90(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2ce2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12fc <lock+0x17c>
lea    0xd81(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%eax
mov    0x2cc2(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cbb(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1344 <lock+0x1c4>
lea    0xd61(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movq   $0x0,(%rsp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1257 <lock+0xd7>
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <ftruncate@plt>
test   %eax,%eax
js     128b <lock+0x10b>
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe4f(%rip),%rsi
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1294 <lock+0x114>
mov    0x2d94(%rip),%rcx
or     (%rcx),%eax
mov    0x2d93(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
jns    127f <lock+0xff>
lea    0xe18(%rip),%rdi
jmp    12a4 <lock+0x124>
mov    0x2d62(%rip),%rax
mov    (%rax),%rax
mov    0x2d78(%rip),%rcx
mov    $0xffffffff,%r14d
cmp    (%rcx),%rax
je     127f <lock+0xff>
mov    0x2d1e(%rip),%rcx
cmp    (%rcx),%rax
jne    129d <lock+0x11d>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd8c(%rip),%rdi
jmp    12a4 <lock+0x124>
lea    0xdaa(%rip),%rdi
jmp    12a4 <lock+0x124>
lea    0xd5c(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i32, i64, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str = [30 x i8] c"fatal error when locking file\00"
@.str.1 = [35 x i8] c"fatal error when emptying lockfile\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [41 x i8] c"fatal error when writing pid to lockfile\00"
@F_GETFD = global i32 0
@.str.4 = [40 x i8] c"fatal error when reading lockfile flags\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.5 = [40 x i8] c"fatal error when setting lockfile flags\00"
define i32 @lock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.flock
%7 = alloca [16 x i8]
store i32 %0, i32* %3
%8 = load i32, i32* @F_WRLCK
%9 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i64 0, i64* %10
%11 = load i32, i32* @SEEK_SET
%12 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 0, i64* %13
%14 = load i32, i32* %3
%15 = load i32, i32* @F_SETLK
%16 = call i32 (i32, i32, ...) @fcntl(i32 %14, i32 %15, %struct.flock* %6)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %1
%20 = load i64, i64* @errno
%21 = load i64, i64* @EACCES
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23, %19
store i32 -1, i32* %2
br label %80
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %1
%32 = load i32, i32* %3
%33 = call i32 @ftruncate(i32 %32, i32 0)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %31
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%41 = call i32 (...) @getpid()
%42 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
store i32 %44, i32* %5
%45 = load i32, i32* %3
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%47 = load i32, i32* %5
%48 = call i32 @write(i32 %45, i8* %46, i32 %47)
%49 = load i32, i32* %5
%50 = icmp slt i32 %48, %49
%51 = zext i1 %50 to i32
store i32 %51, i32* %4
%52 = load i32, i32* %4
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %39
%55 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%56 = call i32 @exit(i32 -1)
unreachable
57: ; preds = %39
%58 = load i32, i32* %3
%59 = load i32, i32* @F_GETFD
%60 = call i32 (i32, i32, ...) @fcntl(i32 %58, i32 %59, i32 0)
store i32 %60, i32* %4
%61 = load i32, i32* %4
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%65 = call i32 @exit(i32 -1)
unreachable
66: ; preds = %57
%67 = load i32, i32* %4
%68 = load i32, i32* @FD_CLOEXEC
%69 = or i32 %67, %68
store i32 %69, i32* %4
%70 = load i32, i32* %3
%71 = load i32, i32* @F_SETFD
%72 = load i32, i32* %4
%73 = call i32 (i32, i32, ...) @fcntl(i32 %70, i32 %71, i32 %72)
store i32 %73, i32* %4
%74 = load i32, i32* %4
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %66
%77 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%78 = call i32 @exit(i32 -1)
unreachable
79: ; preds = %66
store i32 0, i32* %2
br label %80
80: ; preds = %79, %27
%81 = load i32, i32* %2
ret i32 %81
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i64, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str = [30 x i8] c"fatal error when locking file\00"
@.str.1 = [35 x i8] c"fatal error when emptying lockfile\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@F_GETFD = global i32 0
@.str.4 = [40 x i8] c"fatal error when reading lockfile flags\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.5 = [40 x i8] c"fatal error when setting lockfile flags\00"
define i32 @lock(i32 %0) {
%2 = alloca %struct.flock
%3 = alloca [16 x i8]
%4 = bitcast %struct.flock* %2 to i8*
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @F_WRLCK
%7 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
store i64 0, i64* %8
%9 = load i32, i32* @SEEK_SET
%10 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
store i64 0, i64* %11
%12 = load i32, i32* @F_SETLK
%13 = call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %12, %struct.flock* %2)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EACCES
%18 = icmp eq i64 %16, %17
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %16, %19
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %51, label %22
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %1
%26 = call i32 @ftruncate(i32 %0, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %25
%32 = call i32 (...) @getpid()
%33 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i32 @strlen(i8* %5)
%35 = call i32 @write(i32 %0, i8* %5, i32 %34)
%36 = load i32, i32* @F_GETFD
%37 = call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %36, i32 0)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %31
%43 = load i32, i32* @FD_CLOEXEC
%44 = or i32 %43, %37
%45 = load i32, i32* @F_SETFD
%46 = call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %45, i32 %44)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
%50 = call i32 @exit(i32 -1)
unreachable
51: ; preds = %42, %15
%52 = phi i32 [ -1, %15 ], [ 0, %42 ]
ret i32 %52
}