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

/* Variables and functions */
scalar_t__ DT_REG;
int /*<<< orphan*/ read_and_print(char const *, int /*<<< orphan*/ *);
struct dirent *readdir(int /*<<< orphan*/ *);
unsigned int strlen(int /*<<< orphan*/ *);
scalar_t__ strncmp(int /*<<< orphan*/ *, char *, int);
void find_txt_in_dir(const char *dirname, DIR *d) {
  struct dirent *dir_entry = NULL;
  while (dir_entry = readdir(d)) {
    if (dir_entry->d_type == DT_REG) {
      unsigned int len = strlen(dir_entry->d_name);
      if (len >= 4 && strncmp(&dir_entry->d_name[len - 4], ".txt", 4) == 0) {
        read_and_print(dirname, dir_entry->d_name);
      }
    }
  }
}


x86 assembly (clang, -O0):
<find_txt_in_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11ed <find_txt_in_dir+0xad>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jne    11e8 <find_txt_in_dir+0xa8>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jb     11e3 <find_txt_in_dir+0xa3>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
mov    %eax,%eax
shl    $0x2,%rax
add    %rax,%rdi
lea    0xe42(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11e3 <find_txt_in_dir+0xa3>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <read_and_print@plt>
jmp    11e8 <find_txt_in_dir+0xa8>
jmp    1158 <find_txt_in_dir+0x18>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_txt_in_dir>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11c5 <find_txt_in_dir+0x85>
mov    %rax,%rbx
mov    0x2e7a(%rip),%r13
lea    0xe93(%rip),%r15
jmp    1180 <find_txt_in_dir+0x40>
nop
mov    %r12,%rdi
call   1060 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c5 <find_txt_in_dir+0x85>
mov    (%rbx),%rax
cmp    0x0(%r13),%rax
jne    1170 <find_txt_in_dir+0x30>
mov    0x8(%rbx),%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jb     1170 <find_txt_in_dir+0x30>
add    $0xfffffffc,%eax
shl    $0x2,%rax
add    0x8(%rbx),%rax
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1170 <find_txt_in_dir+0x30>
mov    0x8(%rbx),%rsi
mov    %r14,%rdi
call   1040 <read_and_print@plt>
jmp    1170 <find_txt_in_dir+0x30>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i32* }
@DT_REG = global i64 0
@.str = [5 x i8] c".txt\00"
define void @find_txt_in_dir(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store %struct.dirent* null, %struct.dirent** %5
br label %7
7: ; preds = %41, %2
%8 = load i32*, i32** %4
%9 = call %struct.dirent* @readdir(i32* %8)
store %struct.dirent* %9, %struct.dirent** %5
%10 = icmp ne %struct.dirent* %9, null
br i1 %10, label %11, label %42
11: ; preds = %7
%12 = load %struct.dirent*, %struct.dirent** %5
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i32 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @DT_REG
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %41
17: ; preds = %11
%18 = load %struct.dirent*, %struct.dirent** %5
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 1
%20 = load i32*, i32** %19
%21 = call i32 @strlen(i32* %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp uge i32 %22, 4
br i1 %23, label %24, label %40
24: ; preds = %17
%25 = load %struct.dirent*, %struct.dirent** %5
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i32 0, i32 1
%27 = load i32*, i32** %26
%28 = load i32, i32* %6
%29 = sub i32 %28, 4
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %27, i64 %30
%32 = call i64 @strncmp(i32* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %24
%35 = load i8*, i8** %3
%36 = load %struct.dirent*, %struct.dirent** %5
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 1
%38 = load i32*, i32** %37
%39 = call i32 @read_and_print(i8* %35, i32* %38)
br label %40
40: ; preds = %34, %24, %17
br label %41
41: ; preds = %40, %11
br label %7
42: ; preds = %7
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i64, i32* }
@DT_REG = global i64 0
@.str = [5 x i8] c".txt\00"
define void @find_txt_in_dir(i8* %0, i32* %1) {
%3 = tail call %struct.dirent* @readdir(i32* %1)
%4 = icmp eq %struct.dirent* %3, null
br i1 %4, label %29, label %5
5: ; preds = %2, %26
%6 = phi %struct.dirent* [ %27, %26 ], [ %3, %2 ]
%7 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i64 0, i32 0
%8 = load i64, i64* %7
%9 = load i64, i64* @DT_REG
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %26
11: ; preds = %5
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %6, i64 0, i32 1
%13 = load i32*, i32** %12
%14 = tail call i32 @strlen(i32* %13)
%15 = icmp ugt i32 %14, 3
br i1 %15, label %16, label %26
16: ; preds = %11
%17 = load i32*, i32** %12
%18 = add i32 %14, -4
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = tail call i64 @strncmp(i32* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32*, i32** %12
%25 = tail call i32 @read_and_print(i8* %0, i32* %24)
br label %26
26: ; preds = %11, %16, %23, %5
%27 = tail call %struct.dirent* @readdir(i32* %1)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %29, label %5
29: ; preds = %26, %2
ret void
}