#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*/ uid_t;
typedef int /*<<< orphan*/ gid_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 /*<<< orphan*/);
int fchown(int fd, uid_t uid, gid_t gid) {
  int ret = __syscall(95, fd, uid, gid);
  if (ret != -EBADF || __syscall(55, fd, F_GETFD) < 0)
    return __syscall_ret(ret);
  char buf[15 + 3 * sizeof(int)];
  __procfdname(buf, fd);
  return syscall(182, buf, uid, gid);
}


x86 assembly (clang, -O0):
<fchown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    $0x5f,%edi
mov    $0x0,%al
call   1040 <__syscall@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e63(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    11a0 <fchown+0x60>
mov    -0x8(%rbp),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    $0x37,%edi
mov    $0x0,%al
call   1040 <__syscall@plt>
cmp    $0x0,%eax
jge    11b0 <fchown+0x70>
mov    -0x14(%rbp),%edi
call   1060 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
jmp    11d3 <fchown+0x93>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <__procfdname@plt>
lea    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    $0xb6,%edi
call   1050 <syscall@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fchown>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%ebx
mov    $0x5f,%edi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <__syscall@plt>
mov    %eax,%ebp
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
add    %ebp,%eax
jne    11bb <fchown+0x7b>
mov    0x2e64(%rip),%rax
mov    (%rax),%edx
mov    $0x37,%edi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <__syscall@plt>
test   %eax,%eax
js     11bb <fchown+0x7b>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <__procfdname@plt>
mov    $0xb6,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
call   1050 <syscall@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <__syscall_ret@plt>

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

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