#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 EBADF;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ __procfdname(char *, int);
int __syscall(int, int, int /*<<< orphan*/);
int __syscall_ret(int);
int syscall(int, char *, int /*<<< orphan*/);
int fchmod(int fd, mode_t mode) {
  int ret = __syscall(94, fd, mode);
  if (ret != -EBADF || __syscall(55, fd, F_GETFD) < 0)
    return __syscall_ret(ret);
  char buf[15 + 3 * sizeof(int)];
  __procfdname(buf, fd);
  return syscall(15, buf, mode);
}


x86 assembly (clang, -O0):
<fchmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    $0x5e,%edi
call   1040 <__syscall@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e6b(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    1196 <fchmod+0x56>
mov    -0x8(%rbp),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    $0x37,%edi
call   1040 <__syscall@plt>
cmp    $0x0,%eax
jge    11a6 <fchmod+0x66>
mov    -0x10(%rbp),%edi
call   1060 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
jmp    11c6 <fchmod+0x86>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <__procfdname@plt>
lea    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0xf,%edi
call   1050 <syscall@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fchmod>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0x5e,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   1040 <__syscall@plt>
mov    %eax,%ebx
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
add    %ebx,%eax
jne    11aa <fchmod+0x6a>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
mov    $0x37,%edi
mov    %ebp,%esi
call   1040 <__syscall@plt>
test   %eax,%eax
js     11aa <fchmod+0x6a>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <__procfdname@plt>
mov    $0xf,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <syscall@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebx,%edi
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <__syscall_ret@plt>

LLVM IR (-O0):
@EBADF = global i32 0
@F_GETFD = global i32 0
define i32 @fchmod(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [27 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* %5
%10 = call i32 @__syscall(i32 94, i32 %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = load i32, i32* @EBADF
%13 = sub nsw i32 0, %12
%14 = icmp ne i32 %11, %13
br i1 %14, label %20, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load i32, i32* @F_GETFD
%18 = call i32 @__syscall(i32 55, i32 %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15, %2
%21 = load i32, i32* %6
%22 = call i32 @__syscall_ret(i32 %21)
store i32 %22, i32* %3
br label %30
23: ; preds = %15
%24 = getelementptr inbounds [27 x i8], [27 x i8]* %7, i64 0, i64 0
%25 = load i32, i32* %4
%26 = call i32 @__procfdname(i8* %24, i32 %25)
%27 = getelementptr inbounds [27 x i8], [27 x i8]* %7, i64 0, i64 0
%28 = load i32, i32* %5
%29 = call i32 @syscall(i32 15, i8* %27, i32 %28)
store i32 %29, i32* %3
br label %30
30: ; preds = %23, %20
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@EBADF = global i32 0
@F_GETFD = global i32 0
define i32 @fchmod(i32 %0, i32 %1) {
%3 = alloca [27 x i8]
%4 = tail call i32 @__syscall(i32 94, i32 %0, i32 %1)
%5 = load i32, i32* @EBADF
%6 = sub nsw i32 0, %5
%7 = icmp eq i32 %4, %6
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @F_GETFD
%10 = tail call i32 @__syscall(i32 55, i32 %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8, %2
%13 = tail call i32 @__syscall_ret(i32 %4)
br label %18
14: ; preds = %8
%15 = getelementptr inbounds [27 x i8], [27 x i8]* %3, i64 0, i64 0
%16 = call i32 @__procfdname(i8* %15, i32 %0)
%17 = call i32 @syscall(i32 15, i8* %15, i32 %1)
br label %18
18: ; preds = %14, %12
%19 = phi i32 [ %13, %12 ], [ %17, %14 ]
ret i32 %19
}