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

/* Variables and functions */
int errno;
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int readdir_loop(char *path) {
  struct dirent *dent;
  DIR *dir = opendir(path);
  if (!dir)
    return errno;
  errno = 0;
  while ((dent = readdir(dir)) != NULL) {
    printf("%s\n", dent->d_name);
  }
  if (errno != 0)
    return errno;
  return 0;
}


x86 assembly (clang, -O0):
<readdir_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1165 <readdir_loop+0x35>
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11c9 <readdir_loop+0x99>
mov    0x2e74(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11a1 <readdir_loop+0x71>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
jmp    1172 <readdir_loop+0x42>
mov    0x2e38(%rip),%rax
cmpl   $0x0,(%rax)
je     11c2 <readdir_loop+0x92>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11c9 <readdir_loop+0x99>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readdir_loop>:
push   %r14
push   %rbx
push   %rax
call   1030 <opendir@plt>
test   %rax,%rax
je     1188 <readdir_loop+0x58>
mov    %rax,%rbx
mov    0x2e98(%rip),%rax
movl   $0x0,(%rax)
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     1188 <readdir_loop+0x58>
lea    0xe9e(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rax),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
jne    1170 <readdir_loop+0x40>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@errno = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @readdir_loop(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.dirent*
%5 = alloca i32*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @opendir(i8* %6)
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = load i32, i32* @errno
store i32 %11, i32* %2
br label %28
12: ; preds = %1
store i32 0, i32* @errno
br label %13
13: ; preds = %17, %12
%14 = load i32*, i32** %5
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %4
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %4
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %13
22: ; preds = %13
%23 = load i32, i32* @errno
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = load i32, i32* @errno
store i32 %26, i32* %2
br label %28
27: ; preds = %22
store i32 0, i32* %2
br label %28
28: ; preds = %27, %25, %10
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@errno = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @readdir_loop(i8* nocapture %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %14, label %4
4: ; preds = %1
store i32 0, i32* @errno
%5 = tail call %struct.dirent* @readdir(i32* %2)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %14, label %7
7: ; preds = %4, %7
%8 = phi %struct.dirent* [ %12, %7 ], [ %5, %4 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call %struct.dirent* @readdir(i32* %2)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %14, label %7
14: ; preds = %7, %4, %1
%15 = load i32, i32* @errno
ret i32 %15
}