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

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ getpid();
int open(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char *path = "oxmd";
  if (argc >= 2)
    path = argv[1];
  int fd;
  while ((fd = open(path, O_RDONLY | O_WRONLY)) < 0 && errno == EAGAIN)
    ;
  if (fd < 0) {
    fprintf(stderr, "%s could not be opened\n", path);
    return -1;
  }
  struct flock f_lock = {
      .l_type = F_WRLCK,
      .l_pid = getpid(),
      .l_start = 0,
      .l_len = 0,
      .l_whence = SEEK_SET,
  };
  int status;
  if ((status = fcntl(fd, F_SETLKW, &f_lock))) {
    fprintf(stderr, "fcntl:locking: %s got %d\n", path, status);
    goto done;
  }
  sleep(1000);
  f_lock.l_type = F_UNLCK;
  fcntl(fd, F_SETLKW, &f_lock);
done:
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     1197 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    119c <main+0x3c>
mov    -0x18(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x35(%rbp)
jge    11e4 <main+0x84>
mov    0x2def(%rip),%rax
mov    (%rax),%rax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    11f4 <main+0x94>
jmp    11f9 <main+0x99>
jmp    119c <main+0x3c>
cmpl   $0x0,-0x1c(%rbp)
jge    122a <main+0xca>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <main+0x178>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     129e <main+0x13e>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x34(%rbp),%ecx
lea    0xd8b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12c9 <main+0x169>
mov    $0x3e8,%edi
call   1080 <sleep@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     1179 <main+0x19>
mov    0x8(%rsi),%r15
jmp    1180 <main+0x20>
lea    0xe80(%rip),%r15
mov    0x2e61(%rip),%r14
mov    0x2e12(%rip),%r12
mov    0x2e2b(%rip),%r13
mov    0x2dfc(%rip),%rbx
nopl   0x0(%rax)
mov    (%r12),%esi
or     (%r14),%esi
mov    %r15,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11be <main+0x5e>
mov    (%rbx),%rax
cmp    %rax,0x0(%r13)
je     11a0 <main+0x40>
test   %ebp,%ebp
js     1225 <main+0xc5>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x10(%rsp)
mov    0x2dea(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
je     1246 <main+0xe6>
mov    0x2dba(%rip),%rcx
mov    (%rcx),%edi
lea    0xe06(%rip),%rsi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1269 <main+0x109>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1272 <main+0x112>
mov    $0x3e8,%edi
call   1080 <sleep@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    (%r14),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32, i32 }
@.str = [5 x i8] c"oxmd\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"%s could not be opened\0A\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.2 = [26 x i8] c"fcntl:locking: %s got %d\0A\00"
@F_UNLCK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.flock
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %6
%10 = load i32, i32* %4
%11 = icmp sge i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %6
br label %16
16: ; preds = %12, %2
br label %17
17: ; preds = %30, %16
%18 = load i8*, i8** %6
%19 = load i32, i32* @O_RDONLY
%20 = load i32, i32* @O_WRONLY
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %18, i32 %21)
store i32 %22, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i64, i64* @errno
%26 = load i64, i64* @EAGAIN
%27 = icmp eq i64 %25, %26
br label %28
28: ; preds = %24, %17
%29 = phi i1 [ false, %17 ], [ %27, %24 ]
br i1 %29, label %30, label %31
30: ; preds = %28
br label %17
31: ; preds = %28
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %6
%37 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %36)
store i32 -1, i32* %3
br label %66
38: ; preds = %31
%39 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
%40 = load i32, i32* @F_WRLCK
store i32 %40, i32* %39
%41 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
%42 = load i32, i32* @SEEK_SET
store i32 %42, i32* %41
%43 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i32 0, i32* %43
%44 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i32 0, i32* %44
%45 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 4
%46 = call i32 (...) @getpid()
store i32 %46, i32* %45
%47 = load i32, i32* %7
%48 = load i32, i32* @F_SETLKW
%49 = call i32 @fcntl(i32 %47, i32 %48, %struct.flock* %8)
store i32 %49, i32* %9
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %38
%52 = load i32, i32* @stderr
%53 = load i8*, i8** %6
%54 = load i32, i32* %9
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %53, i32 %54)
br label %63
56: ; preds = %38
%57 = call i32 @sleep(i32 1000)
%58 = load i32, i32* @F_UNLCK
%59 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %58, i32* %59
%60 = load i32, i32* %7
%61 = load i32, i32* @F_SETLKW
%62 = call i32 @fcntl(i32 %60, i32 %61, %struct.flock* %8)
br label %63
63: ; preds = %56, %51
%64 = load i32, i32* %7
%65 = call i32 @close(i32 %64)
store i32 0, i32* %3
br label %66
66: ; preds = %63, %34
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32, i32 }
@.str = [5 x i8] c"oxmd\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"%s could not be opened\0A\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.2 = [26 x i8] c"fcntl:locking: %s got %d\0A\00"
@F_UNLCK = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
br label %8
8: ; preds = %5, %2
%9 = phi i8* [ %7, %5 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ]
br label %10
10: ; preds = %10, %8
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %9, i32 %13)
%15 = icmp slt i32 %14, 0
%16 = load i64, i64* @errno
%17 = load i64, i64* @EAGAIN
%18 = icmp eq i64 %16, %17
%19 = select i1 %15, i1 %18, i1 false
br i1 %19, label %10, label %20
20: ; preds = %10
br i1 %15, label %21, label %24
21: ; preds = %20
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %47
24: ; preds = %20
%25 = bitcast %struct.flock* %3 to i8*
%26 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
%27 = load i32, i32* @F_WRLCK
store i32 %27, i32* %26
%28 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%29 = load i32, i32* @SEEK_SET
store i32 %29, i32* %28
%30 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 0, i32* %30
%31 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 0, i32* %31
%32 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
%33 = tail call i32 (...) @getpid()
store i32 %33, i32* %32
%34 = load i32, i32* @F_SETLKW
%35 = call i32 @fcntl(i32 %14, i32 %34, %struct.flock* %3)
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %24
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %9, i32 %35)
br label %45
40: ; preds = %24
%41 = call i32 @sleep(i32 1000)
%42 = load i32, i32* @F_UNLCK
store i32 %42, i32* %26
%43 = load i32, i32* @F_SETLKW
%44 = call i32 @fcntl(i32 %14, i32 %43, %struct.flock* %3)
br label %45
45: ; preds = %40, %37
%46 = call i32 @close(i32 %14)
br label %47
47: ; preds = %45, %21
%48 = phi i32 [ -1, %21 ], [ 0, %45 ]
ret i32 %48
}