#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_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*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
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_nofcntl:open");
    exit(2);
  }
  rc = fcntl(fd, F_SETFL, 0);
  if (rc != -1) {
    fprintf(stderr, "selinux_nofcntl:F_SETFL\n");
    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) {
    fprintf(stderr, "selinux_nofcntl:F_GETLK\n");
    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     11ad <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f1 <main+0x81>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     1232 <main+0xc2>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     129b <main+0x12b>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jne    11b2 <main+0x42>
mov    0x8(%rsi),%rdi
mov    0x2e5b(%rip),%rax
mov    0x2e24(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    11cb <main+0x5b>
lea    0xe71(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    (%rsi),%rsi
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    %eax,%ebx
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1235 <main+0xc5>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movq   $0x0,(%rsp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1247 <main+0xd7>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
jmp    1257 <main+0xe7>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <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 = [21 x i8] c"selinux_nofcntl:open\00"
@F_SETFL = global i32 0
@stderr = global i32 0
@.str.2 = [25 x i8] c"selinux_nofcntl:F_SETFL\0A\00"
@F_RDLCK = global i32 0
@SEEK_CUR = global i32 0
@F_GETLK = global i32 0
@.str.3 = [25 x i8] c"selinux_nofcntl:F_GETLK\0A\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 ([21 x i8], [21 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 @fcntl(i32 %31, i32 %32, %struct.flock* null)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
%41 = load i32, i32* @F_RDLCK
%42 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i64 0, i64* %43
%44 = load i32, i32* @SEEK_CUR
%45 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i32 %44, i32* %45
%46 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i64 0, i64* %46
%47 = load i32, i32* %6
%48 = load i32, i32* @F_GETLK
%49 = call i32 @fcntl(i32 %47, i32 %48, %struct.flock* %8)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp ne i32 %50, -1
br i1 %51, label %52, label %56
52: ; preds = %40
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %40
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = 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 = [21 x i8] c"selinux_nofcntl:open\00"
@F_SETFL = global i32 0
@stderr = global i32 0
@.str.2 = [25 x i8] c"selinux_nofcntl:F_SETFL\0A\00"
@F_RDLCK = global i32 0
@SEEK_CUR = global i32 0
@F_GETLK = global i32 0
@.str.3 = [25 x i8] c"selinux_nofcntl:F_GETLK\0A\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 ([21 x i8], [21 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 @fcntl(i32 %16, i32 %22, %struct.flock* null)
%24 = icmp eq i32 %23, -1
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = load i32, i32* @F_RDLCK
%31 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i64 0, i64* %32
%33 = load i32, i32* @SEEK_CUR
%34 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i64 0, i64* %35
%36 = load i32, i32* @F_GETLK
%37 = call i32 @fcntl(i32 %16, i32 %36, %struct.flock* %3)
%38 = icmp eq i32 %37, -1
br i1 %38, label %43, label %39
39: ; preds = %29
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %29
%44 = call i32 @close(i32 %16)
%45 = call i32 @exit(i32 0)
unreachable
}