#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 */

/* Variables and functions */
int EBADF;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ __procfdname(char *, int);
int __syscall(int, int, ...);
int __syscall_ret(int);
int syscall(int, char *);
int fchdir(int fd) {
  int ret = __syscall(133, fd);
  if (ret != -EBADF || __syscall(55, fd, F_GETFD) < 0)
    return __syscall_ret(ret);
  char buf[15 + 3 * sizeof(int)];
  __procfdname(buf, fd);
  return syscall(12, buf);
}


x86 assembly (clang, -O0):
<fchdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    $0x85,%edi
mov    $0x0,%al
call   1040 <__syscall@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e6f(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    1194 <fchdir+0x54>
mov    -0x8(%rbp),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    $0x37,%edi
mov    $0x0,%al
call   1040 <__syscall@plt>
cmp    $0x0,%eax
jge    11a4 <fchdir+0x64>
mov    -0xc(%rbp),%edi
call   1060 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
jmp    11c1 <fchdir+0x81>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <__procfdname@plt>
lea    -0x30(%rbp),%rsi
mov    $0xc,%edi
call   1050 <syscall@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fchdir>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
mov    $0x85,%edi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <__syscall@plt>
mov    %eax,%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
add    %ebp,%eax
jne    11a1 <fchdir+0x61>
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
mov    $0x37,%edi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <__syscall@plt>
test   %eax,%eax
js     11a1 <fchdir+0x61>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
call   1030 <__procfdname@plt>
mov    $0xc,%edi
mov    %rbp,%rsi
call   1050 <syscall@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
mov    %ebp,%edi
add    $0x28,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <__syscall_ret@plt>

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

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