#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 stat {
  int st_nlink;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int O_RDWR;
int O_WRONLY;
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ cow_name(char const *);
int /*<<< orphan*/ stat(char const *, struct stat *);
int is_cow_open(char const *name, int flags) {
  struct stat stb;
  return (flags & O_RDWR || flags & O_WRONLY) && cow_name(name) &&
         !stat(name, &stb) && S_ISREG(stb.st_mode) && stb.st_nlink > 1;
}


x86 assembly (clang, -O0):
<is_cow_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e9f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    116e <is_cow_open+0x3e>
mov    -0xc(%rbp),%ecx
mov    0x2e72(%rip),%rax
and    (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
je     11ca <is_cow_open+0x9a>
mov    -0x8(%rbp),%rdi
call   1030 <cow_name@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
je     11ca <is_cow_open+0x9a>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
jne    11ca <is_cow_open+0x9a>
mov    -0x14(%rbp),%edi
call   1050 <S_ISREG@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
je     11ca <is_cow_open+0x9a>
cmpl   $0x1,-0x18(%rbp)
setg   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_cow_open>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eab(%rip),%rax
test   %esi,(%rax)
jne    114c <is_cow_open+0x1c>
mov    0x2e88(%rip),%rax
test   %esi,(%rax)
je     1184 <is_cow_open+0x54>
mov    %rbx,%rdi
call   1030 <cow_name@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1186 <is_cow_open+0x56>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
jne    1186 <is_cow_open+0x56>
mov    0x4(%rsp),%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
je     1186 <is_cow_open+0x56>
xor    %ebp,%ebp
cmpl   $0x2,(%rsp)
setge  %bpl
jmp    1186 <is_cow_open+0x56>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@O_RDWR = global i32 0
@O_WRONLY = global i32 0
define i32 @is_cow_open(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = load i32, i32* @O_RDWR
%8 = and i32 %6, %7
%9 = icmp ne i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = load i32, i32* %4
%12 = load i32, i32* @O_WRONLY
%13 = and i32 %11, %12
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %10, %2
%16 = load i8*, i8** %3
%17 = call i64 @cow_name(i8* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = call i32 @stat(i8* %20, %struct.stat* %5)
%22 = icmp ne i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %19
%24 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i64 @S_ISREG(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%30 = load i32, i32* %29
%31 = icmp sgt i32 %30, 1
br label %32
32: ; preds = %28, %23, %19, %15, %10
%33 = phi i1 [ false, %23 ], [ false, %19 ], [ false, %15 ], [ false, %10 ], [ %31, %28 ]
%34 = zext i1 %33 to i32
ret i32 %34
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDWR = global i32 0
@O_WRONLY = global i32 0
define i32 @is_cow_open(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = and i32 %5, %1
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @O_WRONLY
%10 = and i32 %9, %1
%11 = icmp eq i32 %10, 0
br i1 %11, label %28, label %12
12: ; preds = %8, %2
%13 = tail call i64 @cow_name(i8* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %12
%16 = call i32 @stat(i8* %0, %struct.stat* %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %15
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%20 = load i32, i32* %19
%21 = tail call i64 @S_ISREG(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = icmp sgt i32 %25, 1
%27 = zext i1 %26 to i32
br label %28
28: ; preds = %23, %18, %15, %12, %8
%29 = phi i32 [ 0, %18 ], [ 0, %15 ], [ 0, %12 ], [ 0, %8 ], [ %27, %23 ]
ret i32 %29
}