#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 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*/ close(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  struct flock lock;
  int fd, err;
  char *filename;
  if (argc > 1 && argv[1]) {
    filename = argv[1];
  } else {
    filename = "conftest.dat";
  }
  lock.l_type = F_WRLCK;
  lock.l_start = 0;
  lock.l_whence = SEEK_SET;
  lock.l_len = 100;
  fd = open(filename, O_RDWR | O_CREAT, 0644);
  err = fcntl(fd, F_SETLKW, &lock);
  if (err) {
    printf("Failed to set a file lock on %s\n", filename);
    perror("Reason ");
  } else {
    printf("fcntl claims success in setting a file lock on %s\n", filename);
  }
  close(fd);
  unlink(filename);
  return err;
}


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)
cmpl   $0x1,-0x8(%rbp)
jle    11a0 <main+0x40>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11a0 <main+0x40>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11ab <main+0x4b>
lea    0xe59(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0x64,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <fcntl@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1238 <main+0xd8>
mov    -0x38(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1050 <printf@plt>
lea    0xe00(%rip),%rdi
call   1080 <perror@plt>
jmp    1248 <main+0xe8>
mov    -0x38(%rbp),%rsi
lea    0xdf3(%rip),%rdi
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jl     1176 <main+0x16>
mov    0x8(%rsi),%rbx
test   %rbx,%rbx
jne    117d <main+0x1d>
lea    0xe83(%rip),%rbx
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movl   $0x64,0x8(%rsp)
mov    0x2e31(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,%r14d
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1040 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1210 <main+0xb0>
lea    0xe49(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
lea    0xe08(%rip),%rdi
call   1080 <perror@plt>
jmp    11f5 <main+0x95>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i32 }
@.str = [13 x i8] c"conftest.dat\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [33 x i8] c"Failed to set a file lock on %s\0A\00"
@.str.2 = [8 x i8] c"Reason \00"
@.str.3 = [51 x i8] c"fcntl claims success in setting a file lock on %s\0A\00"
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
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %21
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %9
br label %22
21: ; preds = %12, %2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %9
br label %22
22: ; preds = %21, %17
%23 = load i32, i32* @F_WRLCK
%24 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i64 0, i64* %25
%26 = load i32, i32* @SEEK_SET
%27 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 100, i32* %28
%29 = load i8*, i8** %9
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %29, i32 %32, i32 420)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = load i32, i32* @F_SETLKW
%36 = call i32 @fcntl(i32 %34, i32 %35, %struct.flock* %6)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %22
%40 = load i8*, i8** %9
%41 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %46
43: ; preds = %22
%44 = load i8*, i8** %9
%45 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %43, %39
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
%49 = load i8*, i8** %9
%50 = call i32 @unlink(i8* %49)
%51 = load i32, i32* %8
ret i32 %51
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i32 }
@.str = [13 x i8] c"conftest.dat\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [33 x i8] c"Failed to set a file lock on %s\0A\00"
@.str.2 = [8 x i8] c"Reason \00"
@.str.3 = [51 x i8] c"fcntl claims success in setting a file lock on %s\0A\00"
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 %10
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %6, %2
br label %11
11: ; preds = %6, %10
%12 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %10 ], [ %8, %6 ]
%13 = load i32, i32* @F_WRLCK
%14 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i64 0, i64* %15
%16 = load i32, i32* @SEEK_SET
%17 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 100, i32* %18
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = tail call i32 @open(i8* %12, i32 %21, i32 420)
%23 = load i32, i32* @F_SETLKW
%24 = call i32 @fcntl(i32 %22, i32 %23, %struct.flock* %3)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %11
%27 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %31
29: ; preds = %11
%30 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %12)
br label %31
31: ; preds = %29, %26
%32 = call i32 @close(i32 %22)
%33 = call i32 @unlink(i8* %12)
ret i32 %24
}