#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 */
int errno;
struct dirent *readdir(int /*<<< orphan*/ *);
int readdir_r(DIR *__restrict dirp, struct dirent *__restrict entry,
              struct dirent **__restrict result) {
  *result = readdir(dirp);
  if (*result)
    *entry = **result;
  return *result ? 0 : errno;
}


x86 assembly (clang, -O0):
<readdir_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <readdir@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     1154 <readdir_r+0x44>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     116c <readdir_r+0x5c>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    1178 <readdir_r+0x68>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readdir_r>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
call   1030 <readdir@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     112c <readdir_r+0x1c>
mov    (%rax),%ecx
mov    %ecx,(%r14)
xor    %ecx,%ecx
test   %rax,%rax
jne    113c <readdir_r+0x2c>
mov    0x2ea6(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@errno = global i32 0
define i32 @readdir_r(i32* noalias %0, %struct.dirent* noalias %1, %struct.dirent** noalias %2) {
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca %struct.dirent**
store i32* %0, i32** %4
store %struct.dirent* %1, %struct.dirent** %5
store %struct.dirent** %2, %struct.dirent*** %6
%7 = load i32*, i32** %4
%8 = call %struct.dirent* @readdir(i32* %7)
%9 = load %struct.dirent**, %struct.dirent*** %6
store %struct.dirent* %8, %struct.dirent** %9
%10 = load %struct.dirent**, %struct.dirent*** %6
%11 = load %struct.dirent*, %struct.dirent** %10
%12 = icmp ne %struct.dirent* %11, null
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load %struct.dirent*, %struct.dirent** %5
%15 = load %struct.dirent**, %struct.dirent*** %6
%16 = load %struct.dirent*, %struct.dirent** %15
%17 = bitcast %struct.dirent* %14 to i8*
%18 = bitcast %struct.dirent* %16 to i8*
br label %19
19: ; preds = %13, %3
%20 = load %struct.dirent**, %struct.dirent*** %6
%21 = load %struct.dirent*, %struct.dirent** %20
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %24
23: ; preds = %19
br label %26
24: ; preds = %19
%25 = load i32, i32* @errno
br label %26
26: ; preds = %24, %23
%27 = phi i32 [ 0, %23 ], [ %25, %24 ]
ret i32 %27
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@errno = global i32 0
define i32 @readdir_r(i32* noalias %0, %struct.dirent* noalias nocapture writeonly %1, %struct.dirent** noalias nocapture %2) {
%4 = tail call %struct.dirent* @readdir(i32* %0)
store %struct.dirent* %4, %struct.dirent** %2
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = getelementptr %struct.dirent, %struct.dirent* %4, i64 0, i32 0
%8 = getelementptr %struct.dirent, %struct.dirent* %1, i64 0, i32 0
%9 = load i32, i32* %7
store i32 %9, i32* %8
br label %10
10: ; preds = %6, %3
%11 = load i32, i32* @errno
%12 = select i1 %5, i32 %11, i32 0
ret i32 %12
}