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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int telldir(int /*<<< orphan*/ *);
int main(void) {
  DIR *dir;
  struct dirent *d_ptr;
  off_t offset;
  dir = opendir("./");
  if (dir == NULL) {
    perror("fail to open dir");
    exit(1);
  }
  while ((d_ptr = readdir(dir)) != NULL) {
    offset = telldir(dir);
    if (offset == -1) {
      perror("fail to get offset");
      exit(1);
    }
    printf("dir name is : %s offset is :%d\n", d_ptr->d_name, (int)offset);
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b0 <main+0x40>
lea    0xe62(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11b5 <main+0x45>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1213 <main+0xa3>
mov    -0x10(%rbp),%rdi
call   1070 <telldir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f8 <main+0x88>
lea    0xe2b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
jmp    11b5 <main+0x45>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11f0 <main+0x80>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11ca <main+0x5a>
mov    %rax,%rbx
lea    0xe87(%rip),%r14
mov    %r15,%rdi
call   1070 <telldir@plt>
cmp    $0xffffffff,%eax
je     11da <main+0x6a>
mov    (%rbx),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1040 <printf@plt>
mov    %r15,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a0 <main+0x30>
mov    %r15,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe33(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe0c(%rip),%rdi
jmp    11e1 <main+0x71>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"./\00"
@.str.1 = [17 x i8] c"fail to open dir\00"
@.str.2 = [19 x i8] c"fail to get offset\00"
@.str.3 = [32 x i8] c"dir name is : %s offset is :%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.dirent*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
br label %12
12: ; preds = %24, %11
%13 = load i32*, i32** %2
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %3
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = load i32*, i32** %2
%18 = call i32 @telldir(i32* %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %16
%25 = load %struct.dirent*, %struct.dirent** %3
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = load i32, i32* %4
%29 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %27, i32 %28)
br label %12
30: ; preds = %12
%31 = load i32*, i32** %2
%32 = call i32 @closedir(i32* %31)
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [3 x i8] c"./\00"
@.str.1 = [17 x i8] c"fail to open dir\00"
@.str.2 = [19 x i8] c"fail to get offset\00"
@.str.3 = [32 x i8] c"dir name is : %s offset is :%d\0A\00"
define i32 @main() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %22, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3, %16
%10 = phi %struct.dirent* [ %20, %16 ], [ %4, %3 ]
%11 = tail call i32 @telldir(i32* %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %10, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %18, i32 %11)
%20 = tail call %struct.dirent* @readdir(i32* %1)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %22, label %9
22: ; preds = %16, %3
%23 = tail call i32 @closedir(i32* %1)
ret i32 0
}