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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int date_to_epoch(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int get_next_file(char *dir_path, time_t target, char *filename) {
  struct dirent *dp;
  time_t epoch, min;
  min = 9999999;
  *filename = 0;
  fprintf(stderr, "get_next_file %s\t%d\n", dir_path, target);
  DIR *dir = opendir(dir_path);
  if (dir == NULL) {
    fprintf(stderr, "dir is null\n");
    return 0;
  }
  while ((dp = readdir(dir)) != NULL) {
    epoch = date_to_epoch(dp->d_name);
    int diff = target - epoch;
    if (diff > 0 && diff < min) {
      min = diff;
      strcpy(filename, dp->d_name);
    }
  }
  fprintf(stderr, "get_next_file finished - new file: %s\n", filename);
  closedir(dir);
  if (min == 9999999)
    return -1;
  return min;
}


x86 assembly (clang, -O0):
<get_next_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x98967f,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11da <get_next_file+0x7a>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1280 <get_next_file+0x120>
jmp    11df <get_next_file+0x7f>
mov    -0x38(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     123d <get_next_file+0xdd>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <date_to_epoch@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    1238 <get_next_file+0xd8>
mov    -0x3c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1238 <get_next_file+0xd8>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
jmp    11df <get_next_file+0x7f>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1060 <closedir@plt>
cmpl   $0x98967f,-0x30(%rbp)
jne    127a <get_next_file+0x11a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <get_next_file+0x120>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_next_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbx
movb   $0x0,(%rdx)
mov    0x2e62(%rip),%r15
mov    (%r15),%edi
lea    0xe78(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     11f5 <get_next_file+0x95>
mov    %rax,%r13
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1208 <get_next_file+0xa8>
mov    %rax,%rbp
mov    $0x98967f,%ebx
jmp    11d0 <get_next_file+0x70>
xchg   %ax,%ax
mov    %r13,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     120d <get_next_file+0xad>
mov    0x0(%rbp),%edi
call   1040 <date_to_epoch@plt>
mov    %r12d,%r15d
sub    %eax,%r15d
jle    11c0 <get_next_file+0x60>
cmp    %ebx,%r15d
jge    11c0 <get_next_file+0x60>
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r15d,%ebx
jmp    11c0 <get_next_file+0x60>
mov    (%r15),%edi
lea    0xe16(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    123d <get_next_file+0xdd>
mov    $0x98967f,%ebx
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r13,%rdi
call   1060 <closedir@plt>
cmp    $0x98967f,%ebx
mov    $0xffffffff,%ebp
cmovne %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"get_next_file %s\09%d\0A\00"
@.str.1 = [13 x i8] c"dir is null\0A\00"
@.str.2 = [39 x i8] c"get_next_file finished - new file: %s\0A\00"
define i32 @get_next_file(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.dirent*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 9999999, i32* %10
%13 = load i8*, i8** %7
store i8 0, i8* %13
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %5
%16 = load i32, i32* %6
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16)
%18 = load i8*, i8** %5
%19 = call i32* @opendir(i8* %18)
store i32* %19, i32** %11
%20 = load i32*, i32** %11
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %3
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %63
25: ; preds = %3
br label %26
26: ; preds = %51, %25
%27 = load i32*, i32** %11
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %8
%29 = icmp ne %struct.dirent* %28, null
br i1 %29, label %30, label %52
30: ; preds = %26
%31 = load %struct.dirent*, %struct.dirent** %8
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @date_to_epoch(i32 %33)
store i32 %34, i32* %9
%35 = load i32, i32* %6
%36 = load i32, i32* %9
%37 = sub nsw i32 %35, %36
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %51
40: ; preds = %30
%41 = load i32, i32* %12
%42 = load i32, i32* %10
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %51
44: ; preds = %40
%45 = load i32, i32* %12
store i32 %45, i32* %10
%46 = load i8*, i8** %7
%47 = load %struct.dirent*, %struct.dirent** %8
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @strcpy(i8* %46, i32 %49)
br label %51
51: ; preds = %44, %40, %30
br label %26
52: ; preds = %26
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %7
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %54)
%56 = load i32*, i32** %11
%57 = call i32 @closedir(i32* %56)
%58 = load i32, i32* %10
%59 = icmp eq i32 %58, 9999999
br i1 %59, label %60, label %61
60: ; preds = %52
store i32 -1, i32* %4
br label %63
61: ; preds = %52
%62 = load i32, i32* %10
store i32 %62, i32* %4
br label %63
63: ; preds = %61, %60, %22
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"get_next_file %s\09%d\0A\00"
@.str.1 = [13 x i8] c"dir is null\0A\00"
@.str.2 = [39 x i8] c"get_next_file finished - new file: %s\0A\00"
define i32 @get_next_file(i8* %0, i32 %1, i8* %2) {
store i8 0, i8* %2
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%6 = tail call i32* @opendir(i8* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = tail call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %31, label %14
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %38
14: ; preds = %8, %27
%15 = phi %struct.dirent* [ %29, %27 ], [ %9, %8 ]
%16 = phi i32 [ %28, %27 ], [ 9999999, %8 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @date_to_epoch(i32 %18)
%20 = sub nsw i32 %1, %19
%21 = icmp sgt i32 %20, 0
%22 = icmp slt i32 %20, %16
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = load i32, i32* %17
%26 = tail call i32 @strcpy(i8* %2, i32 %25)
br label %27
27: ; preds = %24, %14
%28 = phi i32 [ %20, %24 ], [ %16, %14 ]
%29 = tail call %struct.dirent* @readdir(i32* %6)
%30 = icmp eq %struct.dirent* %29, null
br i1 %30, label %31, label %14
31: ; preds = %27, %8
%32 = phi i32 [ 9999999, %8 ], [ %28, %27 ]
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%35 = tail call i32 @closedir(i32* %6)
%36 = icmp eq i32 %32, 9999999
%37 = select i1 %36, i32 -1, i32 %32
br label %38
38: ; preds = %31, %11
%39 = phi i32 [ 0, %11 ], [ %37, %31 ]
ret i32 %39
}