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

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int);
int main() {
  int fd = open("hello.txt", O_RDWR | O_CREAT, 0666);
  if (fd > 0) {
    struct flock lock;
    lock.l_type = F_WRLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    lock.l_pid = getpid();
    int rd = fcntl(fd, F_SETLK, &lock);
    printf("return value of lock:%d\n", rd);
    while (1) {
      rd++;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2ea0(%rip),%rax
or     (%rax),%esi
lea    0xe9f(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    11e2 <main+0xa2>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe3e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    %eax,-0x2c(%rbp)
jmp    11d1 <main+0x91>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e94(%rip),%rax
mov    0x2ea5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea2(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
jle    11c2 <main+0x82>
mov    %eax,%ebx
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,(%rsp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
lea    0xe56(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
nopl   0x0(%rax,%rax,1)
jmp    11c0 <main+0x80>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i32, i32 }
@.str = [10 x i8] c"hello.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.1 = [25 x i8] c"return value of lock:%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 ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4, i32 438)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %21
7: ; preds = %0
%8 = bitcast %struct.flock* %1 to i8*
%9 = load i32, i32* @F_WRLCK
%10 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SEEK_SET
%12 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
%14 = bitcast i64* %13 to i8*
%15 = tail call i32 (...) @getpid()
%16 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @F_SETLK
%18 = call i32 @fcntl(i32 %5, i32 %17, %struct.flock* %1)
%19 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %20, %7
br label %20
21: ; preds = %0
ret i32 0
}