#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*/ AT_FDCWD;
int /*<<< orphan*/ AT_SYMLINK_NOFOLLOW;
scalar_t__ S_ISLNK(int /*<<< orphan*/);
int fchmodat(int /*<<< orphan*/, char const *, int /*<<< orphan*/,
             int /*<<< orphan*/);
int lstat(char const *, struct stat *);
int lchmod(const char *path, mode_t mode) {
  struct stat st;
  int r = lstat(path, &st);
  if (r < 0) {
    return r;
  }
  if (S_ISLNK(st.st_mode)) {
    return 0;
  } else {
    return fchmodat(AT_FDCWD, path, mode, AT_SYMLINK_NOFOLLOW);
  }
}


x86 assembly (clang, -O0):
<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>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1164 <lchmod+0x34>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a3 <lchmod+0x73>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1182 <lchmod+0x52>
movl   $0x0,-0x4(%rbp)
jmp    11a3 <lchmod+0x73>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    0x2e47(%rip),%rax
mov    (%rax),%ecx
call   1050 <fchmodat@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lchmod>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <lstat@plt>
test   %eax,%eax
js     1156 <lchmod+0x26>
mov    (%rsp),%edi
call   1030 <S_ISLNK@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     115d <lchmod+0x2d>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e73(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rsi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <fchmodat@plt>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@AT_FDCWD = global i32 0
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @lchmod(i8* %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 slt i32 %5, 0
br i1 %6, label %16, label %7
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 %12, label %16
12: ; preds = %7
%13 = load i32, i32* @AT_FDCWD
%14 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%15 = tail call i32 @fchmodat(i32 %13, i8* %0, i32 %1, i32 %14)
br label %16
16: ; preds = %7, %2, %12
%17 = phi i32 [ %15, %12 ], [ %5, %2 ], [ 0, %7 ]
ret i32 %17
}