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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fdopendir(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int openat(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int main(int argc, char *argv[]) {
  int dir_fd, fd;
  DIR *dir;
  struct stat statbuf;
  struct dirent *dirp;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <dirname>\n", argv[0]);
    fprintf(stderr, "Find a filename larger than 1MB\n");
    exit(EXIT_FAILURE);
  }
  if ((dir = fdopendir((dir_fd = open(argv[1], O_RDONLY)))) == NULL) {
    fprintf(stderr, "Err.:%d open(); %s", errno, strerror(errno));
    exit(EXIT_FAILURE);
  }
  while ((dirp = readdir(dir)) != NULL) {
    if (dirp->d_name[0] == '.')
      continue;
    if ((fd = openat(dir_fd, dirp->d_name, O_RDONLY)) == -1) {
      fprintf(stderr, "Err.:%d openat(); %s", errno, strerror(errno));
      continue;
    }
    if (fstat(fd, &statbuf) == 0 && statbuf.st_size > (1024 * 1024)) {
      printf("%s: %jdK\n", dirp->d_name, (intmax_t)(statbuf.st_size / 1024));
    }
    close(fd);
  }
  closedir(dir);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1213 <main+0x63>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%edi
mov    %edi,-0x14(%rbp)
call   10a0 <fdopendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    128c <main+0xdc>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xdbf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    1291 <main+0xe1>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1376 <main+0x1c6>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    12c0 <main+0x110>
jmp    1291 <main+0xe1>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
call   1030 <openat@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1326 <main+0x176>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    %rax,%rcx
lea    0xd2f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1291 <main+0xe1>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0x0,%rax
jne    1369 <main+0x1b9>
cmpl   $0x100000,-0x28(%rbp)
jle    1369 <main+0x1b9>
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
lea    0xcfa(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    1291 <main+0xe1>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    0x2c3a(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    12c3 <main+0x113>
mov    0x8(%rsi),%rdi
mov    0x2e19(%rip),%r15
mov    (%r15),%esi
call   1090 <open@plt>
mov    %eax,%r13d
mov    %eax,%edi
call   10a0 <fdopendir@plt>
test   %rax,%rax
je     12ef <main+0x13f>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     12a3 <main+0xf3>
mov    %rax,%rbp
jmp    1250 <main+0xa0>
mov    0x2dce(%rip),%rax
mov    (%rax),%ebp
mov    0x2dbd(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10c0 <strerror@plt>
mov    %ebp,%edi
lea    0xe24(%rip),%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12a3 <main+0xf3>
mov    0x0(%rbp),%rsi
cmpb   $0x2e,(%rsi)
je     1240 <main+0x90>
mov    (%r15),%edx
mov    %r13d,%edi
call   1030 <openat@plt>
cmp    $0xffffffff,%eax
je     1203 <main+0x53>
mov    %eax,%r12d
mov    %eax,%edi
mov    %rsp,%rsi
call   10d0 <fstat@plt>
test   %rax,%rax
jne    1299 <main+0xe9>
mov    (%rsp),%edx
cmp    $0x100001,%edx
jl     1299 <main+0xe9>
mov    0x0(%rbp),%rsi
shr    $0xa,%edx
lea    0xdca(%rip),%rdi
call   1040 <printf@plt>
mov    %r12d,%edi
call   1050 <close@plt>
jmp    1240 <main+0x90>
mov    %r14,%rdi
call   1060 <closedir@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d0e(%rip),%rbx
mov    (%rbx),%edi
mov    (%rsi),%rdx
lea    0xd2a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd2f(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    131d <main+0x16d>
mov    0x2ce2(%rip),%rax
mov    (%rax),%ebx
mov    0x2cd1(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10c0 <strerror@plt>
lea    0xd27(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s <dirname>\0A\00"
@.str.1 = [33 x i8] c"Find a filename larger than 1MB\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [19 x i8] c"Err.:%d open(); %s\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"Err.:%d openat(); %s\00"
@.str.4 = [10 x i8] c"%s: %jdK\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.stat
%10 = alloca %struct.dirent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %23
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %6
%29 = call i32* @fdopendir(i32 %28)
store i32* %29, i32** %8
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %39
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %33, i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %23
br label %40
40: ; preds = %83, %61, %52, %39
%41 = load i32*, i32** %8
%42 = call %struct.dirent* @readdir(i32* %41)
store %struct.dirent* %42, %struct.dirent** %10
%43 = icmp ne %struct.dirent* %42, null
br i1 %43, label %44, label %86
44: ; preds = %40
%45 = load %struct.dirent*, %struct.dirent** %10
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = getelementptr inbounds i8, i8* %47, i64 0
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 46
br i1 %51, label %52, label %53
52: ; preds = %44
br label %40
53: ; preds = %44
%54 = load i32, i32* %6
%55 = load %struct.dirent*, %struct.dirent** %10
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i8*, i8** %56
%58 = load i32, i32* @O_RDONLY
%59 = call i32 @openat(i32 %54, i8* %57, i32 %58)
store i32 %59, i32* %7
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %67
61: ; preds = %53
%62 = load i32, i32* @stderr
%63 = load i32, i32* @errno
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %63, i8* %65)
br label %40
67: ; preds = %53
%68 = load i32, i32* %7
%69 = call i64 @fstat(i32 %68, %struct.stat* %9)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %83
71: ; preds = %67
%72 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%73 = load i32, i32* %72
%74 = icmp sgt i32 %73, 1048576
br i1 %74, label %75, label %83
75: ; preds = %71
%76 = load %struct.dirent*, %struct.dirent** %10
%77 = getelementptr inbounds %struct.dirent, %struct.dirent* %76, i32 0, i32 0
%78 = load i8*, i8** %77
%79 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%80 = load i32, i32* %79
%81 = sdiv i32 %80, 1024
%82 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %78, i32 %81)
br label %83
83: ; preds = %75, %71, %67
%84 = load i32, i32* %7
%85 = call i32 @close(i32 %84)
br label %40
86: ; preds = %40
%87 = load i32*, i32** %8
%88 = call i32 @closedir(i32* %87)
%89 = load i32, i32* @EXIT_SUCCESS
ret i32 %89
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s <dirname>\0A\00"
@.str.1 = [33 x i8] c"Find a filename larger than 1MB\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [19 x i8] c"Err.:%d open(); %s\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"Err.:%d openat(); %s\00"
@.str.4 = [10 x i8] c"%s: %jdK\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = tail call i32* @fdopendir(i32 %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %26, label %21
21: ; preds = %14
%22 = tail call %struct.dirent* @readdir(i32* %19)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %63, label %24
24: ; preds = %21
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %33
26: ; preds = %14
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %28, i8* %29)
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %24, %48
%34 = phi %struct.dirent* [ %22, %24 ], [ %49, %48 ]
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, 46
br i1 %38, label %48, label %39
39: ; preds = %33
%40 = load i32, i32* @O_RDONLY
%41 = tail call i32 @openat(i32 %18, i8* %36, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %45, i8* %46)
br label %48
48: ; preds = %43, %61, %33
%49 = tail call %struct.dirent* @readdir(i32* %19)
%50 = icmp eq %struct.dirent* %49, null
br i1 %50, label %63, label %33
51: ; preds = %39
%52 = call i64 @fstat(i32 %41, %struct.stat* %3)
%53 = icmp eq i64 %52, 0
%54 = load i32, i32* %25
%55 = icmp sgt i32 %54, 1048576
%56 = select i1 %53, i1 %55, i1 false
br i1 %56, label %57, label %61
57: ; preds = %51
%58 = load i8*, i8** %35
%59 = lshr i32 %54, 10
%60 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %58, i32 %59)
br label %61
61: ; preds = %57, %51
%62 = tail call i32 @close(i32 %41)
br label %48
63: ; preds = %48, %21
%64 = tail call i32 @closedir(i32* %19)
%65 = load i32, i32* @EXIT_SUCCESS
ret i32 %65
}