#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 O_RDWR;
int O_WRONLY;
int libc_openat(int, char const *, int);
int /*<<< orphan*/ watch_log(char *);
int openat(int dirfd, const char *path, int flags, ...) {
  if ((flags & O_WRONLY) || (flags & O_RDWR)) {
    watch_log("openat %s w\n");
    return 0;
  }
  return libc_openat(dirfd, path, flags);
}


x86 assembly (clang, -O0):
<openat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e94(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    115c <openat+0x3c>
mov    -0x14(%rbp),%eax
mov    0x2e97(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1174 <openat+0x54>
lea    0xe9d(%rip),%rdi
call   1030 <watch_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    1186 <openat+0x66>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <libc_openat@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openat>:
push   %rax
mov    0x2ea8(%rip),%rax
test   %edx,(%rax)
jne    1137 <openat+0x17>
mov    0x2eb5(%rip),%rax
test   %edx,(%rax)
je     1147 <openat+0x27>
lea    0xec2(%rip),%rdi
call   1030 <watch_log@plt>
xor    %eax,%eax
pop    %rcx
ret
pop    %rax
jmp    1040 <libc_openat@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@.str = [13 x i8] c"openat %s w\0A\00"
define i32 @openat(i32 %0, i8* %1, i32 %2, ...) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%8 = load i32, i32* %7
%9 = load i32, i32* @O_WRONLY
%10 = and i32 %8, %9
%11 = icmp ne i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %3
%13 = load i32, i32* %7
%14 = load i32, i32* @O_RDWR
%15 = and i32 %13, %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12, %3
%18 = call i32 @watch_log(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %24
19: ; preds = %12
%20 = load i32, i32* %5
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = call i32 @libc_openat(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %4
br label %24
24: ; preds = %19, %17
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@.str = [13 x i8] c"openat %s w\0A\00"
define i32 @openat(i32 %0, i8* %1, i32 %2, ...) {
%4 = load i32, i32* @O_WRONLY
%5 = and i32 %4, %2
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = load i32, i32* @O_RDWR
%9 = and i32 %8, %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7, %3
%12 = tail call i32 @watch_log(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %15
13: ; preds = %7
%14 = tail call i32 @libc_openat(i32 %0, i8* %1, i32 %2)
br label %15
15: ; preds = %13, %11
%16 = phi i32 [ 0, %11 ], [ %14, %13 ]
ret i32 %16
}