#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
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 /*<<< orphan*/ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
int open(char *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int lock_file(char *pathname) {
  int fd;
  struct flock lock;
  char buf[10] = {0};
  char filename[1024] = {0};
  strncpy(filename, pathname, sizeof(filename) - 1);
  if ((fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {
    return -1;
  }
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  if (fcntl(fd, F_SETLK, &lock) == -1) {
    close(fd);
    return -1;
  }
  ftruncate(fd, 0);
  snprintf(buf, sizeof(buf), "%ld", (long)getpid());
  write(fd, buf, strlen(buf));
  return 0;
}


x86 assembly (clang, -O0):
<lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x3a(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10a0 <memset@plt>
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
lea    -0x440(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    -0x440(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    0x2dc0(%rip),%rax
or     (%rax),%edx
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    122f <lock_file+0x8f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <lock_file+0x158>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1289 <lock_file+0xe9>
mov    -0x14(%rbp),%edi
call   10b0 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <lock_file+0x158>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1090 <ftruncate@plt>
lea    -0x3a(%rbp),%rax
mov    %rax,-0x458(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x458(%rbp),%rdi
mov    %rax,%rcx
mov    $0xa,%esi
lea    0xd45(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44c(%rbp)
lea    -0x3a(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x3a(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x44c(%rbp),%edi
mov    -0x448(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x430,%rsp
mov    %rdi,%rbx
movw   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
lea    0x30(%rsp),%rbp
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   10a0 <memset@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
mov    0x2df8(%rip),%rax
mov    0x2e09(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    0x2dcf(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
call   10c0 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     12a2 <lock_file+0x102>
mov    %eax,%ebp
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     129b <lock_file+0xfb>
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1090 <ftruncate@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd8e(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xa,%esi
mov    %rax,%rcx
call   1080 <snprintf@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
jmp    12a2 <lock_file+0x102>
mov    %ebp,%edi
call   10b0 <close@plt>
mov    %ebx,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str = [4 x i8] c"%ld\00"
define i32 @lock_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.flock
%6 = alloca [10 x i8]
%7 = alloca [1024 x i8]
store i8* %0, i8** %3
%8 = bitcast [10 x i8]* %6 to i8*
%9 = bitcast [1024 x i8]* %7 to i8*
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%11 = load i8*, i8** %3
%12 = call i32 @strncpy(i8* %10, i8* %11, i32 1023)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %13, i32 %16, i32 %19)
store i32 %20, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %1
store i32 -1, i32* %2
br label %48
23: ; preds = %1
%24 = load i32, i32* @F_WRLCK
%25 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i32 %24, i32* %25
%26 = load i32, i32* @SEEK_SET
%27 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i64 0, i64* %28
%29 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i64 0, i64* %29
%30 = load i32, i32* %4
%31 = load i32, i32* @F_SETLK
%32 = call i32 @fcntl(i32 %30, i32 %31, %struct.flock* %5)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %23
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %2
br label %48
37: ; preds = %23
%38 = load i32, i32* %4
%39 = call i32 @ftruncate(i32 %38, i32 0)
%40 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%41 = call i64 (...) @getpid()
%42 = call i32 @snprintf(i8* %40, i32 10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %41)
%43 = load i32, i32* %4
%44 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%45 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i32 %46)
store i32 0, i32* %2
br label %48
48: ; preds = %37, %34, %22
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str = [4 x i8] c"%ld\00"
define i32 @lock_file(i8* %0) {
%2 = alloca %struct.flock
%3 = alloca [10 x i8]
%4 = alloca [1024 x i8]
%5 = bitcast %struct.flock* %2 to i8*
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i32 @strncpy(i8* %7, i8* %0, i32 1023)
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %13, %12
%15 = call i32 @open(i8* %7, i32 %11, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %34, label %17
17: ; preds = %1
%18 = load i32, i32* @F_WRLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
store i32 %18, i32* %19
%20 = load i32, i32* @SEEK_SET
%21 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
store i32 %20, i32* %21
%22 = bitcast %struct.flock* %2 to i8*
%23 = load i32, i32* @F_SETLK
%24 = call i32 @fcntl(i32 %15, i32 %23, %struct.flock* %2)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @close(i32 %15)
br label %34
28: ; preds = %17
%29 = call i32 @ftruncate(i32 %15, i32 0)
%30 = call i64 (...) @getpid()
%31 = call i32 @snprintf(i8* %6, i32 10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %30)
%32 = call i32 @strlen(i8* %6)
%33 = call i32 @write(i32 %15, i8* %6, i32 %32)
br label %34
34: ; preds = %1, %28, %26
%35 = phi i32 [ -1, %26 ], [ 0, %28 ], [ -1, %1 ]
ret i32 %35
}