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

/* Variables and functions */
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  if (argc > 1) {
    int fd = open(argv[1], O_WRONLY);
    if (fd == -1) {
      printf("Unable to open the file\n");
      exit(1);
    }
    static struct flock lock;
    lock.l_type = F_WRLCK;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_len = 0;
    lock.l_pid = getpid();
    int ret = fcntl(fd, F_SETLKW, &lock);
    printf("Return value of fcntl:%d\n", ret);
    sleep(110000);
    if (ret == 0) {
      while (1) {
        scanf("%c", NULL);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    126b <main+0xfb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11cb <main+0x5b>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2eae(%rip)
movq   $0x0,0x2e9b(%rip)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2e84(%rip)
movq   $0x0,0x2e71(%rip)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,0x2e5c(%rip)
mov    -0x14(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    0x2e49(%rip),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1adb0,%edi
call   1090 <sleep@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1266 <main+0xf6>
jmp    1251 <main+0xe1>
lea    0xddb(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <scanf@plt>
jmp    1251 <main+0xe1>
jmp    126b <main+0xfb>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     1225 <main+0xa5>
mov    0x8(%rsi),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124c <main+0xcc>
mov    %eax,%ebx
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2ed7(%rip)
lea    0x2eb0(%rip),%r14
movq   $0x0,0x2ebd(%rip)
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2ea6(%rip)
movq   $0x0,0x2e93(%rip)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x2e7e(%rip)
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
mov    %eax,%ebx
lea    0xdf2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1adb0,%edi
call   10a0 <sleep@plt>
test   %ebx,%ebx
je     122f <main+0xaf>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde4(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <scanf@plt>
jmp    1240 <main+0xc0>
lea    0xdca(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i64, i32, i64, i32 }
@O_WRONLY = global i32 0
@.str = [25 x i8] c"Unable to open the file\0A\00"
@main.lock = internal global %struct.flock zeroinitializer
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [26 x i8] c"Return value of fcntl:%d\0A\00"
@.str.2 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %37
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %10
%22 = load i32, i32* @F_WRLCK
store i32 %22, i32* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i32 0, i32 4)
store i64 0, i64* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i32 0, i32 3)
%23 = load i32, i32* @SEEK_SET
store i32 %23, i32* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i32 0, i32 2)
store i64 0, i64* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i32 0, i32 1)
%24 = call i32 (...) @getpid()
store i32 %24, i32* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i32 0, i32 0)
%25 = load i32, i32* %6
%26 = load i32, i32* @F_SETLKW
%27 = call i32 @fcntl(i32 %25, i32 %26, %struct.flock* @main.lock)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = call i32 @sleep(i32 110000)
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %21
br label %34
34: ; preds = %33, %34
%35 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %34
36: ; preds = %21
br label %37
37: ; preds = %36, %2
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i32, i64, i32 }
@O_WRONLY = global i32 0
@main.lock = internal global %struct.flock zeroinitializer
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [26 x i8] c"Return value of fcntl:%d\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@str = [24 x i8] c"Unable to open the file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %24
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_WRONLY
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %4
%14 = load i32, i32* @F_WRLCK
store i32 %14, i32* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i64 0, i32 4)
store i64 0, i64* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i64 0, i32 3)
%15 = load i32, i32* @SEEK_SET
store i32 %15, i32* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i64 0, i32 2)
store i64 0, i64* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i64 0, i32 1)
%16 = tail call i32 (...) @getpid()
store i32 %16, i32* getelementptr inbounds (%struct.flock, %struct.flock* @main.lock, i64 0, i32 0)
%17 = load i32, i32* @F_SETLKW
%18 = tail call i32 @fcntl(i32 %8, i32 %17, %struct.flock* @main.lock)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = tail call i32 @sleep(i32 110000)
%21 = icmp eq i32 %18, 0
br i1 %21, label %22, label %24
22: ; preds = %13, %22
%23 = tail call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %22
24: ; preds = %13, %2
ret i32 0
}