#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 */
struct dirent {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ _DIRENT_SIZE(struct dirent *);
struct dirent *_readdir_unlocked(int /*<<< orphan*/ *, int);
int errno;
int /*<<< orphan*/ memcpy(struct dirent *, struct dirent *, size_t);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
  struct dirent *dp;
  int saved_errno;
  saved_errno = errno;
  errno = 0;
  if ((dp = _readdir_unlocked(dirp, 1)) != NULL)
    memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp));
  if (errno != 0) {
    if (dp == NULL)
      return (errno);
  } else
    errno = saved_errno;
  if (dp != NULL)
    *result = entry;
  else
    *result = NULL;
  return (0);
}


x86 assembly (clang, -O0):
<readdir_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e89(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1030 <_readdir_unlocked@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11a2 <readdir_r+0x72>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <_DIRENT_SIZE@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
mov    0x2e37(%rip),%rax
cmpl   $0x0,(%rax)
je     11d3 <readdir_r+0xa3>
cmpq   $0x0,-0x28(%rbp)
jne    11ce <readdir_r+0x9e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120c <readdir_r+0xdc>
jmp    11df <readdir_r+0xaf>
mov    -0x2c(%rbp),%ecx
mov    0x2e03(%rip),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     11fa <readdir_r+0xca>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    1205 <readdir_r+0xd5>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readdir_r>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e9b(%rip),%r12
mov    (%r12),%ebp
movl   $0x0,(%r12)
mov    $0x1,%esi
call   1030 <_readdir_unlocked@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1182 <readdir_r+0x52>
mov    %rbx,%rdi
call   1040 <_DIRENT_SIZE@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
cmpl   $0x0,(%r12)
jne    118e <readdir_r+0x5e>
jmp    118a <readdir_r+0x5a>
mov    (%r12),%eax
test   %eax,%eax
jne    119a <readdir_r+0x6a>
mov    %ebp,(%r12)
test   %rbx,%rbx
cmove  %rbx,%r15
mov    %r15,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@errno = global i32 0
define i32 @readdir_r(i32* %0, %struct.dirent* %1, %struct.dirent** %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.dirent**
%8 = alloca %struct.dirent*
%9 = alloca i32
store i32* %0, i32** %5
store %struct.dirent* %1, %struct.dirent** %6
store %struct.dirent** %2, %struct.dirent*** %7
%10 = load i32, i32* @errno
store i32 %10, i32* %9
store i32 0, i32* @errno
%11 = load i32*, i32** %5
%12 = call %struct.dirent* @_readdir_unlocked(i32* %11, i32 1)
store %struct.dirent* %12, %struct.dirent** %8
%13 = icmp ne %struct.dirent* %12, null
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load %struct.dirent*, %struct.dirent** %6
%16 = load %struct.dirent*, %struct.dirent** %8
%17 = load %struct.dirent*, %struct.dirent** %8
%18 = call i64 @_DIRENT_SIZE(%struct.dirent* %17)
%19 = call i32 @memcpy(%struct.dirent* %15, %struct.dirent* %16, i64 %18)
br label %20
20: ; preds = %14, %3
%21 = load i32, i32* @errno
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load %struct.dirent*, %struct.dirent** %8
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i32, i32* @errno
store i32 %27, i32* %4
br label %40
28: ; preds = %23
br label %31
29: ; preds = %20
%30 = load i32, i32* %9
store i32 %30, i32* @errno
br label %31
31: ; preds = %29, %28
%32 = load %struct.dirent*, %struct.dirent** %8
%33 = icmp ne %struct.dirent* %32, null
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load %struct.dirent*, %struct.dirent** %6
%36 = load %struct.dirent**, %struct.dirent*** %7
store %struct.dirent* %35, %struct.dirent** %36
br label %39
37: ; preds = %31
%38 = load %struct.dirent**, %struct.dirent*** %7
store %struct.dirent* null, %struct.dirent** %38
br label %39
39: ; preds = %37, %34
store i32 0, i32* %4
br label %40
40: ; preds = %39, %26
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@errno = global i32 0
define i32 @readdir_r(i32* %0, %struct.dirent* %1, %struct.dirent** nocapture writeonly %2) {
%4 = load i32, i32* @errno
store i32 0, i32* @errno
%5 = tail call %struct.dirent* @_readdir_unlocked(i32* %0, i32 1)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @errno
%9 = icmp eq i32 %8, 0
br i1 %9, label %15, label %18
10: ; preds = %3
%11 = tail call i64 @_DIRENT_SIZE(%struct.dirent* %5)
%12 = tail call i32 @memcpy(%struct.dirent* %1, %struct.dirent* %5, i64 %11)
%13 = load i32, i32* @errno
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10, %7
store i32 %4, i32* @errno
br label %16
16: ; preds = %10, %15
%17 = select i1 %6, %struct.dirent* null, %struct.dirent* %1
store %struct.dirent* %17, %struct.dirent** %2
br label %18
18: ; preds = %7, %16
%19 = phi i32 [ 0, %16 ], [ %8, %7 ]
ret i32 %19
}