#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_CREAT;
int O_NONBLOCK;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
scalar_t__ malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ stdout;
int main() {
  int fd, rval, arg;
  struct flock *lockarg;
  lockarg = (struct flock *)malloc(4096);
  fd = open("x.x", O_WRONLY | O_CREAT, 0666);
  fprintf(stdout, "Filedes = %d\n", fd);
  rval = fcntl(fd, F_GETFD);
  fprintf(stdout, "F_GETFD gives: %d\n", rval);
  arg = 1;
  rval = fcntl(fd, F_SETFD, arg);
  rval = fcntl(fd, F_GETFD);
  fprintf(stdout, "F_GETFD gives: %d\t(should be 1)\n", rval);
  arg = 0;
  rval = fcntl(fd, F_SETFD, arg);
  rval = fcntl(fd, F_GETFD);
  fprintf(stdout, "F_GETFD gives: %d\t(should be 0)\n", rval);
  rval = fcntl(fd, F_GETFL);
  fprintf(stdout, "F_GETFL gives: %d\t(should be 0)\n",
          rval & O_NONBLOCK ? 1 : 0);
  rval = fcntl(fd, F_SETFL, O_NONBLOCK);
  rval = fcntl(fd, F_GETFL);
  fprintf(stdout, "F_GETFL gives: %d\t(should be 1)\n",
          rval & O_NONBLOCK ? 1 : 0);
  arg = 5;
  rval = fcntl(fd, F_DUPFD, arg);
  fprintf(stdout, "F_DUPFD returns: %d with arg = %d\n", rval, arg);
  close(fd);
  close(rval);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2e6b(%rip),%rax
or     (%rax),%esi
lea    0xe6a(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%ecx
mov    0x2d2a(%rip),%rax
and    (%rax),%ecx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    0x2ce3(%rip),%rax
mov    (%rax),%esi
mov    0x2cfa(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cb4(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%ecx
mov    0x2cc5(%rip),%rax
and    (%rax),%ecx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x5,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2c8f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xd3b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    0x2e20(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe76(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x2e0b(%rip),%r14
mov    (%r14),%edi
lea    0xe5d(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e01(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r14),%edi
lea    0xe45(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2deb(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r15),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r14),%edi
lea    0xe21(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
xor    %edx,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r15),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r14),%edi
lea    0xe15(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d6f(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r14),%edi
mov    0x2d89(%rip),%rbp
xor    %edx,%edx
and    0x0(%rbp),%eax
setne  %dl
lea    0xe01(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    0x0(%rbp),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r15),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r14),%edi
xor    %edx,%edx
and    0x0(%rbp),%eax
setne  %dl
lea    0xde8(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x5,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebp
mov    (%r14),%edi
lea    0xddf(%rip),%rsi
mov    %eax,%edx
mov    $0x5,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32 }
@.str = [4 x i8] c"x.x\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stdout = global i32 0
@.str.1 = [14 x i8] c"Filedes = %d\0A\00"
@F_GETFD = global i32 0
@.str.2 = [19 x i8] c"F_GETFD gives: %d\0A\00"
@F_SETFD = global i32 0
@.str.3 = [33 x i8] c"F_GETFD gives: %d\09(should be 1)\0A\00"
@.str.4 = [33 x i8] c"F_GETFD gives: %d\09(should be 0)\0A\00"
@F_GETFL = global i32 0
@.str.5 = [33 x i8] c"F_GETFL gives: %d\09(should be 0)\0A\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.6 = [33 x i8] c"F_GETFL gives: %d\09(should be 1)\0A\00"
@F_DUPFD = global i32 0
@.str.7 = [35 x i8] c"F_DUPFD returns: %d with arg = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock*
store i32 0, i32* %1
%6 = call i64 @malloc(i32 4096)
%7 = inttoptr i64 %6 to %struct.flock*
store %struct.flock* %7, %struct.flock** %5
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %10, i32 438)
store i32 %11, i32* %2
%12 = load i32, i32* @stdout
%13 = load i32, i32* %2
%14 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %2
%16 = load i32, i32* @F_GETFD
%17 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* @stdout
%19 = load i32, i32* %3
%20 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %19)
store i32 1, i32* %4
%21 = load i32, i32* %2
%22 = load i32, i32* @F_SETFD
%23 = load i32, i32* %4
%24 = call i32 (i32, i32, ...) @fcntl(i32 %21, i32 %22, i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %2
%26 = load i32, i32* @F_GETFD
%27 = call i32 (i32, i32, ...) @fcntl(i32 %25, i32 %26)
store i32 %27, i32* %3
%28 = load i32, i32* @stdout
%29 = load i32, i32* %3
%30 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %29)
store i32 0, i32* %4
%31 = load i32, i32* %2
%32 = load i32, i32* @F_SETFD
%33 = load i32, i32* %4
%34 = call i32 (i32, i32, ...) @fcntl(i32 %31, i32 %32, i32 %33)
store i32 %34, i32* %3
%35 = load i32, i32* %2
%36 = load i32, i32* @F_GETFD
%37 = call i32 (i32, i32, ...) @fcntl(i32 %35, i32 %36)
store i32 %37, i32* %3
%38 = load i32, i32* @stdout
%39 = load i32, i32* %3
%40 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %2
%42 = load i32, i32* @F_GETFL
%43 = call i32 (i32, i32, ...) @fcntl(i32 %41, i32 %42)
store i32 %43, i32* %3
%44 = load i32, i32* @stdout
%45 = load i32, i32* %3
%46 = load i32, i32* @O_NONBLOCK
%47 = and i32 %45, %46
%48 = icmp ne i32 %47, 0
%49 = zext i1 %48 to i64
%50 = select i1 %48, i32 1, i32 0
%51 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %2
%53 = load i32, i32* @F_SETFL
%54 = load i32, i32* @O_NONBLOCK
%55 = call i32 (i32, i32, ...) @fcntl(i32 %52, i32 %53, i32 %54)
store i32 %55, i32* %3
%56 = load i32, i32* %2
%57 = load i32, i32* @F_GETFL
%58 = call i32 (i32, i32, ...) @fcntl(i32 %56, i32 %57)
store i32 %58, i32* %3
%59 = load i32, i32* @stdout
%60 = load i32, i32* %3
%61 = load i32, i32* @O_NONBLOCK
%62 = and i32 %60, %61
%63 = icmp ne i32 %62, 0
%64 = zext i1 %63 to i64
%65 = select i1 %63, i32 1, i32 0
%66 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %65)
store i32 5, i32* %4
%67 = load i32, i32* %2
%68 = load i32, i32* @F_DUPFD
%69 = load i32, i32* %4
%70 = call i32 (i32, i32, ...) @fcntl(i32 %67, i32 %68, i32 %69)
store i32 %70, i32* %3
%71 = load i32, i32* @stdout
%72 = load i32, i32* %3
%73 = load i32, i32* %4
%74 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %72, i32 %73)
%75 = load i32, i32* %2
%76 = call i32 @close(i32 %75)
%77 = load i32, i32* %3
%78 = call i32 @close(i32 %77)
%79 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [4 x i8] c"x.x\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stdout = global i32 0
@.str.1 = [14 x i8] c"Filedes = %d\0A\00"
@F_GETFD = global i32 0
@.str.2 = [19 x i8] c"F_GETFD gives: %d\0A\00"
@F_SETFD = global i32 0
@.str.3 = [33 x i8] c"F_GETFD gives: %d\09(should be 1)\0A\00"
@.str.4 = [33 x i8] c"F_GETFD gives: %d\09(should be 0)\0A\00"
@F_GETFL = global i32 0
@.str.5 = [33 x i8] c"F_GETFL gives: %d\09(should be 0)\0A\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.6 = [33 x i8] c"F_GETFL gives: %d\09(should be 1)\0A\00"
@F_DUPFD = global i32 0
@.str.7 = [35 x i8] c"F_DUPFD returns: %d with arg = %d\0A\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 4096)
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %4, i32 438)
%6 = load i32, i32* @stdout
%7 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%8 = load i32, i32* @F_GETFD
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %8)
%10 = load i32, i32* @stdout
%11 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%12 = load i32, i32* @F_SETFD
%13 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %12, i32 1)
%14 = load i32, i32* @F_GETFD
%15 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %14)
%16 = load i32, i32* @stdout
%17 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%18 = load i32, i32* @F_SETFD
%19 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %18, i32 0)
%20 = load i32, i32* @F_GETFD
%21 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %20)
%22 = load i32, i32* @stdout
%23 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%24 = load i32, i32* @F_GETFL
%25 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %24)
%26 = load i32, i32* @stdout
%27 = load i32, i32* @O_NONBLOCK
%28 = and i32 %27, %25
%29 = icmp ne i32 %28, 0
%30 = zext i1 %29 to i32
%31 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = load i32, i32* @F_SETFL
%33 = load i32, i32* @O_NONBLOCK
%34 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %32, i32 %33)
%35 = load i32, i32* @F_GETFL
%36 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %35)
%37 = load i32, i32* @stdout
%38 = load i32, i32* @O_NONBLOCK
%39 = and i32 %38, %36
%40 = icmp ne i32 %39, 0
%41 = zext i1 %40 to i32
%42 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %41)
%43 = load i32, i32* @F_DUPFD
%44 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %43, i32 5)
%45 = load i32, i32* @stdout
%46 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %44, i32 5)
%47 = tail call i32 @close(i32 %5)
%48 = tail call i32 @close(i32 %44)
%49 = tail call i32 @exit(i32 0)
unreachable
}