#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 {
  scalar_t__ d_type;
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ *fdopendir(int);
int streq(int /*<<< orphan*/, char *);
struct dirent *xreaddir(int /*<<< orphan*/ *);
void test_readdir(int fd) {
  DIR *d;
  struct dirent *de;
  d = fdopendir(fd);
  assert(d);
  de = xreaddir(d);
  assert(streq(de->d_name, "."));
  assert(de->d_type == DT_DIR);
  de = xreaddir(d);
  assert(streq(de->d_name, ".."));
  assert(de->d_type == DT_DIR);
  de = xreaddir(d);
  assert(!de);
}


x86 assembly (clang, -O0):
<test_readdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <fdopendir@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <xreaddir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xe84(%rip),%rsi
call   1040 <streq@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <xreaddir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xe3e(%rip),%rsi
call   1040 <streq@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <xreaddir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_readdir>:
push   %r15
push   %r14
push   %rbx
call   1060 <fdopendir@plt>
mov    %rax,%r14
mov    %r14d,%edi
call   1050 <assert@plt>
mov    %r14,%rdi
call   1030 <xreaddir@plt>
mov    %rax,%rbx
mov    0x8(%rax),%edi
lea    0xe97(%rip),%rsi
call   1040 <streq@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    (%rbx),%rax
mov    0x2e60(%rip),%r15
xor    %edi,%edi
cmp    (%r15),%rax
sete   %dil
call   1050 <assert@plt>
mov    %r14,%rdi
call   1030 <xreaddir@plt>
mov    %rax,%rbx
mov    0x8(%rax),%edi
lea    0xe5d(%rip),%rsi
call   1040 <streq@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    (%rbx),%rax
xor    %edi,%edi
cmp    (%r15),%rax
sete   %dil
call   1050 <assert@plt>
mov    %r14,%rdi
call   1030 <xreaddir@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <assert@plt>

LLVM IR (-O0):
%struct.dirent = type { i64, i32 }
@.str = [2 x i8] c".\00"
@DT_DIR = global i64 0
@.str.1 = [3 x i8] c"..\00"
define void @test_readdir(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.dirent*
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32* @fdopendir(i32 %5)
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = ptrtoint i32* %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i32*, i32** %3
%11 = call %struct.dirent* @xreaddir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %4
%12 = load %struct.dirent*, %struct.dirent** %4
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = call i32 @streq(i32 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @assert(i32 %15)
%17 = load %struct.dirent*, %struct.dirent** %4
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* @DT_DIR
%21 = icmp eq i64 %19, %20
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32*, i32** %3
%25 = call %struct.dirent* @xreaddir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %4
%26 = load %struct.dirent*, %struct.dirent** %4
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @streq(i32 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @assert(i32 %29)
%31 = load %struct.dirent*, %struct.dirent** %4
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @DT_DIR
%35 = icmp eq i64 %33, %34
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
%38 = load i32*, i32** %3
%39 = call %struct.dirent* @xreaddir(i32* %38)
store %struct.dirent* %39, %struct.dirent** %4
%40 = load %struct.dirent*, %struct.dirent** %4
%41 = icmp ne %struct.dirent* %40, null
%42 = xor i1 %41, true
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32 }
@.str = [2 x i8] c".\00"
@DT_DIR = global i64 0
@.str.1 = [3 x i8] c"..\00"
define void @test_readdir(i32 %0) {
%2 = tail call i32* @fdopendir(i32 %0)
%3 = ptrtoint i32* %2 to i64
%4 = trunc i64 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = tail call %struct.dirent* @xreaddir(i32* %2)
%7 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i64 0, i32 1
%8 = load i32, i32* %7
%9 = tail call i32 @streq(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @assert(i32 %9)
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i64 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @DT_DIR
%14 = icmp eq i64 %12, %13
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = tail call %struct.dirent* @xreaddir(i32* %2)
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 1
%19 = load i32, i32* %18
%20 = tail call i32 @streq(i32 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @assert(i32 %20)
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @DT_DIR
%25 = icmp eq i64 %23, %24
%26 = zext i1 %25 to i32
%27 = tail call i32 @assert(i32 %26)
%28 = tail call %struct.dirent* @xreaddir(i32* %2)
%29 = icmp eq %struct.dirent* %28, null
%30 = zext i1 %29 to i32
%31 = tail call i32 @assert(i32 %30)
ret void
}