#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 */

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ unlink(char *);
int test_open_flag(int flag) {
  int fd, res;
  res = -1;
  fd = open("eatmydatatest.dat", O_CREAT | O_TRUNC | O_WRONLY | flag,
            S_IRUSR | S_IWUSR);
  if (fd != -1) {
    res = fcntl(fd, F_GETFL);
  }
  if (res == -1) {
    res = errno;
    close(fd);
    unlink("eatmydatatest.dat");
    errno = res;
    return -1;
  }
  close(fd);
  unlink("eatmydatatest.dat");
  return res & flag;
}


x86 assembly (clang, -O0):
<test_open_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
mov    0x2e8e(%rip),%rax
or     (%rax),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
or     -0x8(%rbp),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    0x2e58(%rip),%rax
or     (%rax),%edx
lea    0xe77(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     11af <test_open_flag+0x6f>
mov    -0xc(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1040 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11f1 <test_open_flag+0xb1>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    0xe2c(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%ecx
mov    0x2de5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <test_open_flag+0xce>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    0xe00(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_open_flag>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
mov    0x2eaa(%rip),%rax
mov    0x2e9b(%rip),%rcx
mov    (%rax),%esi
or     %edi,%esi
or     (%rcx),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
mov    0x2e4d(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe83(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11b8 <test_open_flag+0x78>
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11b8 <test_open_flag+0x78>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
lea    0xe52(%rip),%rdi
call   1030 <unlink@plt>
and    %r14d,%ebx
jmp    11dd <test_open_flag+0x9d>
mov    0x2e09(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1050 <close@plt>
lea    0xe30(%rip),%rdi
call   1030 <unlink@plt>
mov    %ebx,(%r14)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"eatmydatatest.dat\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_GETFL = global i32 0
@errno = global i32 0
define i32 @test_open_flag(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %5
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %8, %9
%11 = load i32, i32* %3
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %15)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %1
%20 = load i32, i32* %4
%21 = load i32, i32* @F_GETFL
%22 = call i32 @fcntl(i32 %20, i32 %21)
store i32 %22, i32* %5
br label %23
23: ; preds = %19, %1
%24 = load i32, i32* %5
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i32, i32* @errno
store i32 %27, i32* %5
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
%30 = call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* %5
store i32 %31, i32* @errno
store i32 -1, i32* %2
br label %39
32: ; preds = %23
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
%35 = call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* %5
%37 = load i32, i32* %3
%38 = and i32 %36, %37
store i32 %38, i32* %2
br label %39
39: ; preds = %32, %26
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [18 x i8] c"eatmydatatest.dat\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@F_GETFL = global i32 0
@errno = global i32 0
define i32 @test_open_flag(i32 %0) {
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_TRUNC
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %2, %0
%6 = or i32 %5, %3
%7 = or i32 %6, %4
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = load i32, i32* @F_GETFL
%15 = tail call i32 @fcntl(i32 %11, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %1, %13
%18 = load i32, i32* @errno
%19 = tail call i32 @close(i32 %11)
%20 = tail call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 %18, i32* @errno
br label %25
21: ; preds = %13
%22 = tail call i32 @close(i32 %11)
%23 = tail call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%24 = and i32 %15, %0
br label %25
25: ; preds = %21, %17
%26 = phi i32 [ -1, %17 ], [ %24, %21 ]
ret i32 %26
}