#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 linux_dirent {
  int dummy;
};
struct flock {
  int l_len;
  scalar_t__ l_pid;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int O_DIRECTORY;
int O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int main(int argc, char *argv[]) {
  int fd, nread, fd2;
  char buf[1024];
  struct linux_dirent *d;
  int bpos;
  int ret;
  char d_type;
  struct flock tmplk = {0};
  fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
  if (fd == -1)
    do {
      perror("open");
      exit(EXIT_FAILURE);
    } while (0);
  tmplk.l_type = F_WRLCK;
  tmplk.l_whence = SEEK_SET;
  tmplk.l_start = 0;
  tmplk.l_len = 10;
  tmplk.l_pid = 0;
  ret = fcntl(fd, F_SETLK, &tmplk);
  if (ret == -1) {
    perror("fcntl(F_WRLCK)");
    goto out;
  }
out:
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x460(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
cmpl   $0x1,-0x8(%rbp)
jle    119a <main+0x4a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x468(%rbp)
jmp    11ad <main+0x5d>
lea    0xe5f(%rip),%rax
mov    %rax,-0x468(%rbp)
jmp    11ad <main+0x5d>
mov    -0x468(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f7 <main+0xa7>
jmp    11dd <main+0x8d>
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x444(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x448(%rbp)
movq   $0x0,-0x450(%rbp)
movl   $0xa,-0x460(%rbp)
movq   $0x0,-0x458(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
lea    -0x460(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0xffffffff,-0x430(%rbp)
jne    1271 <main+0x121>
lea    0xda0(%rip),%rdi
call   1060 <perror@plt>
jmp    1276 <main+0x126>
jmp    1276 <main+0x126>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
sub    $0x28,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movq   $0x0,0x20(%rsp)
cmp    $0x2,%edi
jl     1164 <main+0x24>
mov    0x8(%rsi),%rdi
jmp    116b <main+0x2b>
lea    0xe95(%rip),%rdi
mov    0x2e76(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    11a1 <main+0x61>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2e10(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x1c(%rsp)
mov    0x2e13(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x18(%rsp)
movl   $0xa,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11ee <main+0xae>
lea    0xe1e(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.linux_dirent = type { i32 }
%struct.flock = type { i32, i64, i64, i32, i32, i32 }
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.2 = [15 x i8] c"fcntl(F_WRLCK)\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca %struct.linux_dirent*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8
%14 = alloca %struct.flock
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = bitcast %struct.flock* %14 to i8*
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
br label %23
22: ; preds = %2
br label %23
23: ; preds = %22, %18
%24 = phi i8* [ %21, %18 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %22 ]
%25 = load i32, i32* @O_RDONLY
%26 = load i32, i32* @O_DIRECTORY
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %24, i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %37
31: ; preds = %23
br label %32
32: ; preds = %31
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; No predecessors!
br label %37
37: ; preds = %36, %23
%38 = load i32, i32* @F_WRLCK
%39 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 4
store i32 %38, i32* %39
%40 = load i32, i32* @SEEK_SET
%41 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 3
store i32 %40, i32* %41
%42 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 2
store i64 0, i64* %42
%43 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 0
store i32 10, i32* %43
%44 = getelementptr inbounds %struct.flock, %struct.flock* %14, i32 0, i32 1
store i64 0, i64* %44
%45 = load i32, i32* %6
%46 = load i32, i32* @F_SETLK
%47 = call i32 @fcntl(i32 %45, i32 %46, %struct.flock* %14)
store i32 %47, i32* %12
%48 = load i32, i32* %12
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %37
%51 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %53
52: ; preds = %37
br label %53
53: ; preds = %52, %50
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @exit(i32 %54)
unreachable
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i32, i32, i32 }
%struct.linux_dirent = type { i32 }
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.2 = [15 x i8] c"fcntl(F_WRLCK)\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %2, %6
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_DIRECTORY
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %9
%21 = load i32, i32* @F_WRLCK
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
store i32 %21, i32* %22
%23 = load i32, i32* @SEEK_SET
%24 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 10, i32* %25
%26 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%27 = bitcast i64* %26 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %27
%28 = load i32, i32* @F_SETLK
%29 = call i32 @fcntl(i32 %14, i32 %28, %struct.flock* %3)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %20, %31
%34 = load i32, i32* @EXIT_SUCCESS
%35 = call i32 @exit(i32 %34)
unreachable
}