#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 /*<<< orphan*/ EBADF;
int chdir(char const *);
int /*<<< orphan*/ errno;
int strlen(char const *);
char *win32_get_dirpath(int);
int fchdir(int fd) {
  const char *path;
  path = win32_get_dirpath(fd);
  if (path) {
    if (((path)[0] == '/') && strlen(path) == 2) {
      if (chdir(path) < 0)
        return -1;
      return chdir("\\");
    }
    return chdir(path);
  }
  errno = EBADF;
  return -1;
}


x86 assembly (clang, -O0):
<fchdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <win32_get_dirpath@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b7 <fchdir+0x87>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11a6 <fchdir+0x76>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x2,%eax
jne    11a6 <fchdir+0x76>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%eax
jge    1192 <fchdir+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <fchdir+0xa0>
lea    0xe67(%rip),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x4(%rbp)
jmp    11d0 <fchdir+0xa0>
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
mov    %eax,-0x4(%rbp)
jmp    11d0 <fchdir+0xa0>
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e19(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fchdir>:
push   %rbx
call   1050 <win32_get_dirpath@plt>
test   %rax,%rax
je     1172 <fchdir+0x42>
mov    %rax,%rbx
cmpb   $0x2f,(%rax)
jne    1169 <fchdir+0x39>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x2,%eax
jne    1169 <fchdir+0x39>
mov    %rbx,%rdi
call   1040 <chdir@plt>
test   %eax,%eax
js     1184 <fchdir+0x54>
lea    0xe9d(%rip),%rdi
pop    %rbx
jmp    1040 <chdir@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1040 <chdir@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    0x2e5e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\\\00"
@EBADF = global i32 0
@errno = global i32 0
define i32 @fchdir(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i8* @win32_get_dirpath(i32 %5)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %29
9: ; preds = %1
%10 = load i8*, i8** %4
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 47
br i1 %14, label %15, label %26
15: ; preds = %9
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = call i32 @chdir(i8* %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %2
br label %31
24: ; preds = %19
%25 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %25, i32* %2
br label %31
26: ; preds = %15, %9
%27 = load i8*, i8** %4
%28 = call i32 @chdir(i8* %27)
store i32 %28, i32* %2
br label %31
29: ; preds = %1
%30 = load i32, i32* @EBADF
store i32 %30, i32* @errno
store i32 -1, i32* %2
br label %31
31: ; preds = %29, %26, %24, %23
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@.str = [2 x i8] c"\\\00"
@EBADF = global i32 0
@errno = global i32 0
define i32 @fchdir(i32 %0) {
%2 = tail call i8* @win32_get_dirpath(i32 %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = load i8, i8* %2
%6 = icmp eq i8 %5, 47
br i1 %6, label %7, label %15
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %2)
%9 = icmp eq i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = tail call i32 @chdir(i8* %2)
%12 = icmp slt i32 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %19
15: ; preds = %7, %4
%16 = tail call i32 @chdir(i8* %2)
br label %19
17: ; preds = %1
%18 = load i32, i32* @EBADF
store i32 %18, i32* @errno
br label %19
19: ; preds = %10, %17, %15, %13
%20 = phi i32 [ %14, %13 ], [ %16, %15 ], [ -1, %17 ], [ -1, %10 ]
ret i32 %20
}