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

/* Variables and functions */
int chmod(char const *, int /*<<< orphan*/);
int errno;
int fchmodat(int, char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int chmod_helper(int dirfd, const char *name, mode_t val) {
  int ret;
  if (dirfd >= 0) {
    ret = fchmodat(dirfd, name, val, 0);
  } else {
    ret = chmod(name, val);
  }
  if (ret >= 0) {
    return 0;
  }
  return errno;
}


x86 assembly (clang, -O0):
<chmod_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1155 <chmod_helper+0x35>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <fchmodat@plt>
mov    %eax,-0x18(%rbp)
jmp    1164 <chmod_helper+0x44>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <chmod@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     117a <chmod_helper+0x5a>
movl   $0x0,-0x4(%rbp)
jmp    1186 <chmod_helper+0x66>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chmod_helper>:
push   %rax
test   %edi,%edi
js     112e <chmod_helper+0xe>
xor    %ecx,%ecx
call   1030 <fchmodat@plt>
jmp    1138 <chmod_helper+0x18>
mov    %rsi,%rdi
mov    %edx,%esi
call   1040 <chmod@plt>
mov    0x2ea1(%rip),%rcx
sar    $0x1f,%eax
and    (%rcx),%eax
pop    %rcx
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @chmod_helper(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %3
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = load i32, i32* %7
%15 = call i32 @fchmodat(i32 %12, i8* %13, i32 %14, i32 0)
store i32 %15, i32* %8
br label %20
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = call i32 @chmod(i8* %17, i32 %18)
store i32 %19, i32* %8
br label %20
20: ; preds = %16, %11
%21 = load i32, i32* %8
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 0, i32* %4
br label %26
24: ; preds = %20
%25 = load i32, i32* @errno
store i32 %25, i32* %4
br label %26
26: ; preds = %24, %23
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @chmod_helper(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %0, -1
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @fchmodat(i32 %0, i8* %1, i32 %2, i32 0)
br label %9
7: ; preds = %3
%8 = tail call i32 @chmod(i8* %1, i32 %2)
br label %9
9: ; preds = %7, %5
%10 = phi i32 [ %6, %5 ], [ %8, %7 ]
%11 = load i32, i32* @errno
%12 = icmp slt i32 %10, 0
%13 = select i1 %12, i32 %11, i32 0
ret i32 %13
}