#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int);
int main() {
  int fd;
  fd = open("hello", O_RDWR | O_CREAT, 0666);
  if (fd < 0) {
    exit(-1);
  }
  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:%d\n", ret);
  while (1) {
    ++ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1199 <main+0x49>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xe19(%rip),%rdi
call   1050 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    %eax,-0x34(%rbp)
jmp    11f2 <main+0xa2>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
mov    0x2e84(%rip),%rax
mov    0x2e95(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe92(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11e2 <main+0x92>
mov    %eax,%ebx
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x8(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
lea    0xe35(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
nopl   0x0(%rax,%rax,1)
jmp    11e0 <main+0x90>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i64, i32, i64, i32 }
@.str = [6 x i8] c"hello\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [20 x i8] c"return value of:%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.flock
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = load i32, i32* @F_WRLCK
%15 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 4
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
store i64 0, i64* %16
%17 = load i32, i32* @SEEK_SET
%18 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
store i64 0, i64* %19
%20 = call i32 (...) @getpid()
%21 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %2
%23 = load i32, i32* @F_SETLKW
%24 = call i32 @fcntl(i32 %22, i32 %23, %struct.flock* %3)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %13, %27
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %27
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i32, i64, i32 }
@.str = [6 x i8] c"hello\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [20 x i8] c"return value of:%d\0A\00"
define i32 @main() {
%1 = alloca %struct.flock
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %4, i32 438)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = bitcast %struct.flock* %1 to i8*
%11 = load i32, i32* @F_WRLCK
%12 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 4
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
store i64 0, i64* %13
%14 = load i32, i32* @SEEK_SET
%15 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
store i64 0, i64* %16
%17 = tail call i32 (...) @getpid()
%18 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @F_SETLKW
%20 = call i32 @fcntl(i32 %5, i32 %19, %struct.flock* %1)
%21 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %22, %9
br label %22
}