#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;
typedef int /*<<< orphan*/ dev_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int fchdir(int);
int mknod(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int xmknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev) {
  int cfd, ret, error;
  cfd = open(".", O_RDONLY);
  if (cfd == -1)
    return (-1);
  if (fchdir(dirfd) == -1) {
    error = errno;
    (void)close(cfd);
    errno = error;
    return (-1);
  }
  ret = mknod(pathname, mode, dev);
  error = errno;
  (void)fchdir(cfd);
  (void)close(cfd);
  errno = error;
  return ret;
}


x86 assembly (clang, -O0):
<xmknodat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1183 <xmknodat+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <xmknodat+0xc0>
mov    -0x8(%rbp),%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    11c0 <xmknodat+0x80>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e26(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <xmknodat+0xc0>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <mknod@plt>
mov    %eax,-0x20(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <fchdir@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2de0(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmknodat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     119e <xmknodat+0x5e>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <fchdir@plt>
cmp    $0xffffffff,%eax
je     11a5 <xmknodat+0x65>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1040 <mknod@plt>
mov    %eax,%ebp
mov    0x2e46(%rip),%rax
mov    (%rax),%r14d
mov    %ebx,%edi
call   1050 <fchdir@plt>
jmp    11b4 <xmknodat+0x74>
mov    $0xffffffff,%ebp
jmp    11c5 <xmknodat+0x85>
mov    0x2e2c(%rip),%rax
mov    (%rax),%r14d
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2e16(%rip),%rax
mov    %r14d,(%rax)
mov    %ebp,%eax
pop    %rbx
pop    %r12
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 @xmknodat(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %39
18: ; preds = %4
%19 = load i32, i32* %6
%20 = call i32 @fchdir(i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i32, i32* @errno
store i32 %23, i32* %12
%24 = load i32, i32* %10
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %12
store i32 %26, i32* @errno
store i32 -1, i32* %5
br label %39
27: ; preds = %18
%28 = load i8*, i8** %7
%29 = load i32, i32* %8
%30 = load i32, i32* %9
%31 = call i32 @mknod(i8* %28, i32 %29, i32 %30)
store i32 %31, i32* %11
%32 = load i32, i32* @errno
store i32 %32, i32* %12
%33 = load i32, i32* %10
%34 = call i32 @fchdir(i32 %33)
%35 = load i32, i32* %10
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %12
store i32 %37, i32* @errno
%38 = load i32, i32* %11
store i32 %38, i32* %5
br label %39
39: ; preds = %27, %22, %17
%40 = load i32, i32* %5
ret i32 %40
}

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