#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_pid;
  int /*<<< orphan*/ member_4;
  int /*<<< orphan*/ member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_WRLCK;
int LOCK_EX;
int LOCK_NB;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ exit(int);
int flock(int, int);
int /*<<< orphan*/ getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int main(int argc, char *argv[]) {
  struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
  int fd;
  int ret;
  fl.l_pid = getpid();
  if (argc > 1)
    fl.l_type = F_RDLCK;
  if ((fd = open("lockdemo.c", O_RDWR)) == -1) {
    perror("open");
    exit(1);
  }
  ret = flock(fd, LOCK_EX | LOCK_NB);
  printf("ret=%d,error=%d\n", ret, errno);
  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)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11d1 <main+0x71>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe1f(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1208 <main+0xa8>
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x34(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
or     (%rax),%esi
call   1070 <flock@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
lea    0xdd8(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b6 <main+0x56>
mov    0x2e57(%rip),%rcx
mov    0x2e30(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%edi
call   1070 <flock@plt>
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%edx
lea    0xe65(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_RDLCK = global i32 0
@.str = [11 x i8] c"lockdemo.c\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [17 x i8] c"ret=%d,error=%d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.flock
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
%10 = load i32, i32* @F_WRLCK
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
%12 = load i32, i32* @SEEK_SET
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 4
store i32 0, i32* %15
%16 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 5
store i32 0, i32* %16
%17 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 6
store i32 0, i32* %17
%18 = call i32 (...) @getpid()
%19 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = load i32, i32* @F_RDLCK
%24 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 %23, i32* %24
br label %25
25: ; preds = %22, %2
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 %27, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = load i32, i32* %7
%34 = load i32, i32* @LOCK_EX
%35 = load i32, i32* @LOCK_NB
%36 = or i32 %34, %35
%37 = call i32 @flock(i32 %33, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = load i32, i32* @errno
%40 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %38, i32 %39)
ret i32 0
}

LLVM IR (-O2):
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_RDLCK = global i32 0
@.str = [11 x i8] c"lockdemo.c\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [17 x i8] c"ret=%d,error=%d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @LOCK_EX
%12 = load i32, i32* @LOCK_NB
%13 = or i32 %12, %11
%14 = tail call i32 @flock(i32 %5, i32 %13)
%15 = load i32, i32* @errno
%16 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %15)
ret i32 0
}