#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 */
typedef scalar_t__ off_t;

/* Variables and functions */
int O_DIRECTORY;
int O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ SYS_getdents;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int syscall(int /*<<< orphan*/, int, char *, int);
int main() {
  int fd = open(".", O_RDONLY | O_DIRECTORY);
  if (fd == -1) {
    perror("failed to open '.'");
    return 1;
  }
  char buf1[4096];
  char buf2[4096];
  memset(buf1, 0, 4096);
  memset(buf2, 0, 4096);
  int amt_read1 = syscall(SYS_getdents, fd, buf1, 4096);
  if (amt_read1 <= 0) {
    fprintf(stderr, "amt_read1=%d\n", amt_read1);
    close(fd);
    return 1;
  }
  off_t seek_res = lseek(fd, 0, SEEK_SET);
  if (seek_res == (off_t)-1) {
    perror("failed to lseek to 0");
    close(fd);
    return 1;
  }
  int amt_read2 = syscall(SYS_getdents, fd, buf2, 4096);
  if (amt_read2 <= 0) {
    fprintf(stderr, "amt_read2=%d\n", amt_read2);
    close(fd);
    return 1;
  }
  if (amt_read1 != amt_read2) {
    fprintf(stderr, "First read gave %d bytes, second read gave %d bytes.\n",
            amt_read1, amt_read2);
    close(fd);
    return 1;
  }
  if (memcmp(buf1, buf2, 4096) != 0) {
    fprintf(stderr, "First and second read results differ.\n");
    close(fd);
    return 1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
lea    0xe55(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d5 <main+0x55>
lea    0xe3e(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13aa <main+0x22a>
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
lea    -0x2010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
lea    -0x1010(%rbp),%rdx
mov    $0x1000,%ecx
call   1080 <syscall@plt>
mov    %eax,-0x2014(%rbp)
cmpl   $0x0,-0x2014(%rbp)
jg     125c <main+0xdc>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    -0x2014(%rbp),%edx
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13aa <main+0x22a>
mov    -0x8(%rbp),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,-0x2020(%rbp)
cmpq   $0xffffffffffffffff,-0x2020(%rbp)
ff
jne    12a4 <main+0x124>
lea    0xd98(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13aa <main+0x22a>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
lea    -0x2010(%rbp),%rdx
mov    $0x1000,%ecx
call   1080 <syscall@plt>
mov    %eax,-0x2024(%rbp)
cmpl   $0x0,-0x2024(%rbp)
jg     1305 <main+0x185>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
mov    -0x2024(%rbp),%edx
lea    0xd4e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13aa <main+0x22a>
mov    -0x2014(%rbp),%eax
cmp    -0x2024(%rbp),%eax
je     134e <main+0x1ce>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
mov    -0x2014(%rbp),%edx
mov    -0x2024(%rbp),%ecx
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13aa <main+0x22a>
lea    -0x1010(%rbp),%rdi
lea    -0x2010(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     139b <main+0x21b>
mov    0x2c59(%rip),%rax
mov    (%rax),%edi
lea    0xcfc(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13aa <main+0x22a>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2000,%rsp
mov    0x2e56(%rip),%rax
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12ca <main+0x14a>
mov    %eax,%ebx
lea    0x1000(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    0x2ddf(%rip),%r14
mov    (%r14),%edi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    $0x1000,%ecx
call   1080 <syscall@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1262 <main+0xe2>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     12dd <main+0x15d>
mov    (%r14),%edi
mov    %rsp,%rdx
mov    %ebx,%esi
mov    $0x1000,%ecx
call   1080 <syscall@plt>
test   %eax,%eax
jle    1276 <main+0xf6>
cmp    %eax,%ebp
jne    1291 <main+0x111>
lea    0x1000(%rsp),%rdi
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     12c6 <main+0x146>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12ac <main+0x12c>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
mov    %ebp,%edx
jmp    1288 <main+0x108>
mov    0x2d53(%rip),%rcx
mov    (%rcx),%edi
lea    0xdb2(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12ac <main+0x12c>
mov    0x2d38(%rip),%rcx
mov    (%rcx),%edi
lea    0xda5(%rip),%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ebp,%ebp
jmp    12b1 <main+0x131>
lea    0xd31(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
jmp    12b8 <main+0x138>
lea    0xd3f(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ac <main+0x12c>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@.str.1 = [19 x i8] c"failed to open '.'\00"
@SYS_getdents = global i32 0
@stderr = global i32 0
@.str.2 = [14 x i8] c"amt_read1=%d\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [21 x i8] c"failed to lseek to 0\00"
@.str.4 = [14 x i8] c"amt_read2=%d\0A\00"
@.str.5 = [54 x i8] c"First read gave %d bytes, second read gave %d bytes.\0A\00"
@.str.6 = [39 x i8] c"First and second read results differ.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_DIRECTORY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %80
16: ; preds = %0
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%18 = call i32 @memset(i8* %17, i32 0, i32 4096)
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 4096)
%21 = load i32, i32* @SYS_getdents
%22 = load i32, i32* %2
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%24 = call i32 @syscall(i32 %21, i32 %22, i8* %23, i32 4096)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %16
%28 = load i32, i32* @stderr
%29 = load i32, i32* %5
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
store i32 1, i32* %1
br label %80
33: ; preds = %16
%34 = load i32, i32* %2
%35 = load i32, i32* @SEEK_SET
%36 = call i64 @lseek(i32 %34, i32 0, i32 %35)
store i64 %36, i64* %6
%37 = load i64, i64* %6
%38 = icmp eq i64 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* %2
%42 = call i32 @close(i32 %41)
store i32 1, i32* %1
br label %80
43: ; preds = %33
%44 = load i32, i32* @SYS_getdents
%45 = load i32, i32* %2
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%47 = call i32 @syscall(i32 %44, i32 %45, i8* %46, i32 4096)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = icmp sle i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %43
%51 = load i32, i32* @stderr
%52 = load i32, i32* %7
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %2
%55 = call i32 @close(i32 %54)
store i32 1, i32* %1
br label %80
56: ; preds = %43
%57 = load i32, i32* %5
%58 = load i32, i32* %7
%59 = icmp ne i32 %57, %58
br i1 %59, label %60, label %67
60: ; preds = %56
%61 = load i32, i32* @stderr
%62 = load i32, i32* %5
%63 = load i32, i32* %7
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i32 %62, i32 %63)
%65 = load i32, i32* %2
%66 = call i32 @close(i32 %65)
store i32 1, i32* %1
br label %80
67: ; preds = %56
%68 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%70 = call i64 @memcmp(i8* %68, i8* %69, i32 4096)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %67
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
%75 = load i32, i32* %2
%76 = call i32 @close(i32 %75)
store i32 1, i32* %1
br label %80
77: ; preds = %67
%78 = load i32, i32* %2
%79 = call i32 @close(i32 %78)
store i32 0, i32* %1
br label %80
80: ; preds = %77, %72, %60, %50, %39, %27, %14
%81 = load i32, i32* %1
ret i32 %81
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@.str.1 = [19 x i8] c"failed to open '.'\00"
@SYS_getdents = global i32 0
@stderr = global i32 0
@.str.2 = [14 x i8] c"amt_read1=%d\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [21 x i8] c"failed to lseek to 0\00"
@.str.4 = [14 x i8] c"amt_read2=%d\0A\00"
@.str.5 = [54 x i8] c"First read gave %d bytes, second read gave %d bytes.\0A\00"
@.str.6 = [39 x i8] c"First and second read results differ.\0A\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = alloca [4096 x i8]
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_DIRECTORY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %48
10: ; preds = %0
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%13 = call i32 @memset(i8* %11, i32 0, i32 4096)
%14 = call i32 @memset(i8* %12, i32 0, i32 4096)
%15 = load i32, i32* @SYS_getdents
%16 = call i32 @syscall(i32 %15, i32 %6, i8* %11, i32 4096)
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %45
21: ; preds = %10
%22 = load i32, i32* @SEEK_SET
%23 = call i64 @lseek(i32 %6, i32 0, i32 %22)
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %45
27: ; preds = %21
%28 = load i32, i32* @SYS_getdents
%29 = call i32 @syscall(i32 %28, i32 %6, i8* %12, i32 4096)
%30 = icmp slt i32 %29, 1
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %45
34: ; preds = %27
%35 = icmp eq i32 %16, %29
br i1 %35, label %39, label %36
36: ; preds = %34
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i32 %16, i32 %29)
br label %45
39: ; preds = %34
%40 = call i64 @memcmp(i8* %11, i8* %12, i32 4096)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
br label %45
45: ; preds = %39, %25, %42, %36, %31, %18
%46 = phi i32 [ 1, %18 ], [ 1, %25 ], [ 1, %31 ], [ 1, %36 ], [ 1, %42 ], [ 0, %39 ]
%47 = call i32 @close(i32 %6)
br label %48
48: ; preds = %45, %8
%49 = phi i32 [ 1, %8 ], [ %46, %45 ]
ret i32 %49
}