#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 */
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ cow_name(char const *);
int /*<<< orphan*/ stat(char const *, struct stat *);
int /*<<< orphan*/ *strchr(char const *, char const);
int is_cow_fopen(char const *name, char const *mode) {
  struct stat stb;
  return (strchr("wa", *mode) != NULL || strchr(mode, '+') != NULL) &&
         cow_name(name) && !stat(name, &stb) && S_ISREG(stb.st_mode) &&
         stb.st_nlink > 1;
}


x86 assembly (clang, -O0):
<is_cow_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
lea    0xea5(%rip),%rdi
movsbl (%rax),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
jne    118d <is_cow_fopen+0x4d>
mov    -0x10(%rbp),%rdi
mov    $0x2b,%esi
call   1030 <strchr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
je     11e9 <is_cow_fopen+0xa9>
mov    -0x8(%rbp),%rdi
call   1040 <cow_name@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
je     11e9 <is_cow_fopen+0xa9>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
jne    11e9 <is_cow_fopen+0xa9>
mov    -0x14(%rbp),%edi
call   1060 <S_ISREG@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
je     11e9 <is_cow_fopen+0xa9>
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_fopen>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%rbx
movsbl (%rsi),%esi
lea    0xead(%rip),%rdi
call   1030 <strchr@plt>
test   %rax,%rax
jne    116f <is_cow_fopen+0x2f>
mov    %rbp,%rdi
mov    $0x2b,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11a7 <is_cow_fopen+0x67>
mov    %rbx,%rdi
call   1040 <cow_name@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11a9 <is_cow_fopen+0x69>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %eax,%eax
jne    11a9 <is_cow_fopen+0x69>
mov    0x4(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     11a9 <is_cow_fopen+0x69>
xor    %ebp,%ebp
cmpl   $0x2,(%rsp)
setge  %bpl
jmp    11a9 <is_cow_fopen+0x69>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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