#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 /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ ENOSYS;
scalar_t__ S_ISLNK(int /*<<< orphan*/);
int chmod(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ lstat(char const *, struct stat *);
int haiku_lchmod(const char *pszPath, mode_t mode) {
  struct stat s;
  if (!lstat(pszPath, &s) && S_ISLNK(s.st_mode)) {
    errno = -ENOSYS;
    return -1;
  }
  return chmod(pszPath, mode);
}


x86 assembly (clang, -O0):
<haiku_lchmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%eax
jne    1187 <haiku_lchmod+0x57>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1187 <haiku_lchmod+0x57>
xor    %ecx,%ecx
mov    0x2e78(%rip),%rax
sub    (%rax),%ecx
mov    0x2e5f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1196 <haiku_lchmod+0x66>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <chmod@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<haiku_lchmod>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <lstat@plt>
test   %eax,%eax
je     1154 <haiku_lchmod+0x24>
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <chmod@plt>
mov    (%rsp),%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
je     1144 <haiku_lchmod+0x14>
mov    0x2e80(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    0x2e65(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOSYS = global i32 0
@errno = global i32 0
define i32 @haiku_lchmod(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i32 @lstat(i8* %7, %struct.stat* %6)
%9 = icmp ne i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISLNK(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @ENOSYS
%17 = sub nsw i32 0, %16
store i32 %17, i32* @errno
store i32 -1, i32* %3
br label %22
18: ; preds = %10, %2
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = call i32 @chmod(i8* %19, i32 %20)
store i32 %21, i32* %3
br label %22
22: ; preds = %18, %15
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOSYS = global i32 0
@errno = global i32 0
define i32 @haiku_lchmod(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @lstat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i64 @S_ISLNK(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = load i32, i32* @ENOSYS
%14 = sub nsw i32 0, %13
store i32 %14, i32* @errno
br label %17
15: ; preds = %7, %2
%16 = tail call i32 @chmod(i8* %0, i32 %1)
br label %17
17: ; preds = %15, %12
%18 = phi i32 [ %16, %15 ], [ -1, %12 ]
ret i32 %18
}