#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int mkdir(char const *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int xmkdirat(int dirfd, const char *pathname, mode_t mode) {
  int cfd;
  int error;
  int ret;
  if (*pathname == '/') {
    return mkdir(pathname, mode);
  } else {
    cfd = open(".", O_RDONLY);
    if (cfd == -1)
      return -1;
    if (fchdir(dirfd) == -1) {
      error = errno;
      (void)close(cfd);
      errno = error;
      return -1;
    }
    ret = mkdir(pathname, mode);
    error = errno;
    fchdir(cfd);
    close(cfd);
    errno = error;
    return ret;
  }
}


x86 assembly (clang, -O0):
<xmkdirat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1176 <xmkdirat+0x36>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x4(%rbp)
jmp    121e <xmkdirat+0xde>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11a4 <xmkdirat+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <xmkdirat+0xde>
mov    -0x8(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11e1 <xmkdirat+0xa1>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2e05(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <xmkdirat+0xde>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x20(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <fchdir@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2dc2(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmkdirat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
cmpb   $0x2f,(%rsi)
jne    1167 <xmkdirat+0x27>
mov    %rbp,%rdi
mov    %r15d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <mkdir@plt>
mov    %edi,%ebx
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d3 <xmkdirat+0x93>
mov    %eax,%r14d
mov    %ebx,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11c0 <xmkdirat+0x80>
mov    %rbp,%rdi
mov    %r15d,%esi
call   1030 <mkdir@plt>
mov    %eax,%ebx
mov    0x2e32(%rip),%r15
mov    (%r15),%ebp
mov    %r14d,%edi
call   1050 <fchdir@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebp,(%r15)
mov    %ebx,%eax
jmp    11d8 <xmkdirat+0x98>
mov    0x2e11(%rip),%rbx
mov    (%rbx),%ebp
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebp,(%rbx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @xmkdirat(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %6
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 47
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = load i32, i32* %7
%18 = call i32 @mkdir(i8* %16, i32 %17)
store i32 %18, i32* %4
br label %45
19: ; preds = %3
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %4
br label %45
25: ; preds = %19
%26 = load i32, i32* %5
%27 = call i32 @fchdir(i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32, i32* @errno
store i32 %30, i32* %9
%31 = load i32, i32* %8
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %9
store i32 %33, i32* @errno
store i32 -1, i32* %4
br label %45
34: ; preds = %25
%35 = load i8*, i8** %6
%36 = load i32, i32* %7
%37 = call i32 @mkdir(i8* %35, i32 %36)
store i32 %37, i32* %10
%38 = load i32, i32* @errno
store i32 %38, i32* %9
%39 = load i32, i32* %8
%40 = call i32 @fchdir(i32 %39)
%41 = load i32, i32* %8
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %9
store i32 %43, i32* @errno
%44 = load i32, i32* %10
store i32 %44, i32* %4
br label %45
45: ; preds = %34, %29, %24, %15
%46 = load i32, i32* %4
ret i32 %46
}

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