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

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETFL;
int O_APPEND;
int O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, ...);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char **argv) {
  int fd;
  int rc;
  struct flock my_lock;
  if (argc != 2) {
    printf("usage: %s filename\n", argv[0]);
    exit(2);
  }
  fd = open(argv[1], O_RDONLY | O_APPEND, 0);
  if (fd == -1) {
    perror("selinux_fcntl:open");
    exit(2);
  }
  rc = fcntl(fd, F_SETFL, 0);
  if (rc == -1) {
    perror("selinux_fcntl:F_SETFL");
    exit(1);
  }
  rc = fcntl(fd, F_GETFL);
  if (rc == -1) {
    perror("selinux_fcntl:F_GETFL");
    exit(1);
  }
  my_lock.l_type = F_RDLCK;
  my_lock.l_start = 0;
  my_lock.l_whence = SEEK_CUR;
  my_lock.l_len = 0;
  rc = fcntl(fd, F_GETLK, &my_lock);
  if (rc == -1) {
    perror("selinux_fcntl:F_GETLK");
    exit(1);
  }
  close(fd);
  exit(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)
cmpl   $0x2,-0x8(%rbp)
je     119d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe72(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e1 <main+0x81>
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1219 <main+0xb9>
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    124f <main+0xef>
lea    0xdfd(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12b1 <main+0x151>
lea    0xdb1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jne    11a2 <main+0x42>
mov    0x8(%rsi),%rdi
mov    0x2e6b(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
jne    11bb <main+0x5b>
lea    0xe81(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    (%rsi),%rsi
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    %eax,%ebx
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11df <main+0x7f>
lea    0xe4a(%rip),%rdi
jmp    124b <main+0xeb>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11ff <main+0x9f>
lea    0xe40(%rip),%rdi
jmp    124b <main+0xeb>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movq   $0x0,(%rsp)
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    125a <main+0xfa>
lea    0xe08(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i32, i64, i32 }
@.str = [20 x i8] c"usage: %s filename\0A\00"
@O_RDONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [19 x i8] c"selinux_fcntl:open\00"
@F_SETFL = global i32 0
@.str.2 = [22 x i8] c"selinux_fcntl:F_SETFL\00"
@F_GETFL = global i32 0
@.str.3 = [22 x i8] c"selinux_fcntl:F_GETFL\00"
@F_RDLCK = global i32 0
@SEEK_CUR = global i32 0
@F_GETLK = global i32 0
@.str.4 = [22 x i8] c"selinux_fcntl:F_GETLK\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.flock
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = load i32, i32* @O_APPEND
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %20, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 2)
unreachable
30: ; preds = %17
%31 = load i32, i32* %6
%32 = load i32, i32* @F_SETFL
%33 = call i32 (i32, i32, ...) @fcntl(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = load i32, i32* %6
%41 = load i32, i32* @F_GETFL
%42 = call i32 (i32, i32, ...) @fcntl(i32 %40, i32 %41)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %39
%49 = load i32, i32* @F_RDLCK
%50 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i32 %49, i32* %50
%51 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i64 0, i64* %51
%52 = load i32, i32* @SEEK_CUR
%53 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i32 %52, i32* %53
%54 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i64 0, i64* %54
%55 = load i32, i32* %6
%56 = load i32, i32* @F_GETLK
%57 = call i32 (i32, i32, ...) @fcntl(i32 %55, i32 %56, %struct.flock* %8)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %48
%61 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %48
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
%66 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i64, i32 }
@.str = [20 x i8] c"usage: %s filename\0A\00"
@O_RDONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [19 x i8] c"selinux_fcntl:open\00"
@F_SETFL = global i32 0
@.str.2 = [22 x i8] c"selinux_fcntl:F_SETFL\00"
@F_GETFL = global i32 0
@.str.3 = [22 x i8] c"selinux_fcntl:F_GETFL\00"
@F_RDLCK = global i32 0
@SEEK_CUR = global i32 0
@F_GETLK = global i32 0
@.str.4 = [22 x i8] c"selinux_fcntl:F_GETLK\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 2)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = load i32, i32* @O_APPEND
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* %12, i32 %15, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 2)
unreachable
21: ; preds = %10
%22 = load i32, i32* @F_SETFL
%23 = tail call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %21
%29 = load i32, i32* @F_GETFL
%30 = tail call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %28
%36 = load i32, i32* @F_RDLCK
%37 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i64 0, i64* %38
%39 = load i32, i32* @SEEK_CUR
%40 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i64 0, i64* %41
%42 = load i32, i32* @F_GETLK
%43 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %42, %struct.flock* %3)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %35
%46 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %35
%49 = call i32 @close(i32 %16)
%50 = call i32 @exit(i32 0)
unreachable
}