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

/* Variables and functions */
int /*<<< orphan*/ chmod(char const *, int);
scalar_t__ fossil_stat(char const *, struct stat *, int);
int file_wd_setexe(const char *zFilename, int onoff) {
  int rc = 0;
  struct stat buf;
  if (fossil_stat(zFilename, &buf, 1) != 0 || (0))
    return 0;
  if (onoff) {
    int targetMode = (buf.st_mode & 0444) >> 2;
    if ((buf.st_mode & 0111) != targetMode) {
      chmod(zFilename, buf.st_mode | targetMode);
      rc = 1;
    }
  } else {
    if ((buf.st_mode & 0111) != 0) {
      chmod(zFilename, buf.st_mode & ~0111);
      rc = 1;
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<file_wd_setexe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <fossil_stat@plt>
cmp    $0x0,%rax
je     115e <file_wd_setexe+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <file_wd_setexe+0xb0>
cmpl   $0x0,-0x14(%rbp)
je     11a0 <file_wd_setexe+0x80>
mov    -0x20(%rbp),%eax
and    $0x124,%eax
sar    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
and    $0x49,%eax
cmp    -0x24(%rbp),%eax
je     119b <file_wd_setexe+0x7b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
or     -0x24(%rbp),%esi
call   1040 <chmod@plt>
movl   $0x1,-0x18(%rbp)
jmp    11ca <file_wd_setexe+0xaa>
mov    -0x20(%rbp),%eax
and    $0x49,%eax
cmp    $0x0,%eax
je     11c5 <file_wd_setexe+0xa5>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
and    $0xffffffb6,%esi
call   1040 <chmod@plt>
movl   $0x1,-0x18(%rbp)
jmp    11ca <file_wd_setexe+0xaa>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_wd_setexe>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
mov    $0x1,%edx
call   1030 <fossil_stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1172 <file_wd_setexe+0x52>
mov    (%rsp),%ecx
test   %ebp,%ebp
je     115b <file_wd_setexe+0x3b>
mov    %ecx,%esi
shr    $0x2,%esi
and    $0x49,%esi
mov    %ecx,%edx
and    $0x49,%edx
cmp    %esi,%edx
je     1172 <file_wd_setexe+0x52>
or     %ecx,%esi
jmp    1165 <file_wd_setexe+0x45>
test   $0x49,%cl
je     1172 <file_wd_setexe+0x52>
and    $0xffffffb6,%ecx
mov    %ecx,%esi
mov    %rbx,%rdi
call   1040 <chmod@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @file_wd_setexe(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
%9 = load i8*, i8** %4
%10 = call i64 @fossil_stat(i8* %9, %struct.stat* %7, i32 1)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* %3
br label %48
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%18 = load i32, i32* %17
%19 = and i32 %18, 292
%20 = ashr i32 %19, 2
store i32 %20, i32* %8
%21 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%22 = load i32, i32* %21
%23 = and i32 %22, 73
%24 = load i32, i32* %8
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %33
26: ; preds = %16
%27 = load i8*, i8** %4
%28 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* %8
%31 = or i32 %29, %30
%32 = call i32 @chmod(i8* %27, i32 %31)
store i32 1, i32* %6
br label %33
33: ; preds = %26, %16
br label %46
34: ; preds = %13
%35 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%36 = load i32, i32* %35
%37 = and i32 %36, 73
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %34
%40 = load i8*, i8** %4
%41 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%42 = load i32, i32* %41
%43 = and i32 %42, -74
%44 = call i32 @chmod(i8* %40, i32 %43)
store i32 1, i32* %6
br label %45
45: ; preds = %39, %34
br label %46
46: ; preds = %45, %33
%47 = load i32, i32* %6
store i32 %47, i32* %3
br label %48
48: ; preds = %46, %12
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @file_wd_setexe(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @fossil_stat(i8* %0, %struct.stat* %3, i32 1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %26
7: ; preds = %2
%8 = icmp eq i32 %1, 0
%9 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%10 = load i32, i32* %9
br i1 %8, label %18, label %11
11: ; preds = %7
%12 = lshr i32 %10, 2
%13 = and i32 %12, 73
%14 = and i32 %10, 73
%15 = icmp eq i32 %14, %13
br i1 %15, label %26, label %16
16: ; preds = %11
%17 = or i32 %13, %10
br label %23
18: ; preds = %7
%19 = and i32 %10, 73
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %18
%22 = and i32 %10, -74
br label %23
23: ; preds = %16, %21
%24 = phi i32 [ %22, %21 ], [ %17, %16 ]
%25 = call i32 @chmod(i8* %0, i32 %24)
br label %26
26: ; preds = %23, %18, %11, %2
%27 = phi i32 [ 0, %2 ], [ 0, %18 ], [ 0, %11 ], [ 1, %23 ]
ret i32 %27
}