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

/* Variables and functions */
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewinddir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strncmp(char *, char const *, unsigned int);
char next_prefix_char(DIR *d, const char *prefix, unsigned prefix_length) {
  struct dirent *entry;
  char nextch = '\0';
  rewinddir(d);
  for (entry = readdir(d); entry; entry = readdir(d)) {
    if (!strncmp(entry->d_name, prefix, prefix_length)) {
      if (nextch == '\0') {
        nextch = entry->d_name[prefix_length];
      } else if (nextch != entry->d_name[prefix_length]) {
        nextch = '\0';
        break;
      }
    }
  }
  return nextch;
}


x86 assembly (clang, -O0):
<next_prefix_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movb   $0x0,-0x21(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <rewinddir@plt>
mov    -0x8(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ea <next_prefix_char+0xba>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11d3 <next_prefix_char+0xa3>
movsbl -0x21(%rbp),%eax
cmp    $0x0,%eax
jne    11a6 <next_prefix_char+0x76>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x14(%rbp),%ecx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
jmp    11ce <next_prefix_char+0x9e>
movsbl -0x21(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
mov    -0x14(%rbp),%edx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
je     11c9 <next_prefix_char+0x99>
movb   $0x0,-0x21(%rbp)
jmp    11ea <next_prefix_char+0xba>
jmp    11ce <next_prefix_char+0x9e>
jmp    11d3 <next_prefix_char+0xa3>
jmp    11d8 <next_prefix_char+0xa8>
mov    -0x8(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x20(%rbp)
jmp    115d <next_prefix_char+0x2d>
movsbl -0x21(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_prefix_char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <rewinddir@plt>
mov    %rbx,%rdi
call   1050 <readdir@plt>
test   %rax,%rax
je     11a6 <next_prefix_char+0x76>
mov    %rax,%rbp
mov    %r12d,%r13d
xor    %r15d,%r15d
jmp    1180 <next_prefix_char+0x50>
mov    %eax,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11a9 <next_prefix_char+0x79>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1170 <next_prefix_char+0x40>
mov    0x0(%rbp),%rax
movzbl (%rax,%r13,1),%eax
test   %r15b,%r15b
je     1161 <next_prefix_char+0x31>
cmp    %al,%r15b
je     1170 <next_prefix_char+0x40>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
define signext i8 @next_prefix_char(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.dirent*
%8 = alloca i8
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i8 0, i8* %8
%9 = load i32*, i32** %4
%10 = call i32 @rewinddir(i32* %9)
%11 = load i32*, i32** %4
%12 = call %struct.dirent* @readdir(i32* %11)
store %struct.dirent* %12, %struct.dirent** %7
br label %13
13: ; preds = %52, %3
%14 = load %struct.dirent*, %struct.dirent** %7
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %55
16: ; preds = %13
%17 = load %struct.dirent*, %struct.dirent** %7
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = call i32 @strncmp(i8* %19, i8* %20, i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %51, label %24
24: ; preds = %16
%25 = load i8, i8* %8
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %24
%29 = load %struct.dirent*, %struct.dirent** %7
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = load i32, i32* %6
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
store i8 %35, i8* %8
br label %50
36: ; preds = %24
%37 = load i8, i8* %8
%38 = sext i8 %37 to i32
%39 = load %struct.dirent*, %struct.dirent** %7
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = load i32, i32* %6
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %38, %46
br i1 %47, label %48, label %49
48: ; preds = %36
store i8 0, i8* %8
br label %55
49: ; preds = %36
br label %50
50: ; preds = %49, %28
br label %51
51: ; preds = %50, %16
br label %52
52: ; preds = %51
%53 = load i32*, i32** %4
%54 = call %struct.dirent* @readdir(i32* %53)
store %struct.dirent* %54, %struct.dirent** %7
br label %13
55: ; preds = %48, %13
%56 = load i8, i8* %8
ret i8 %56
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
define signext i8 @next_prefix_char(i32* %0, i8* %1, i32 %2) {
%4 = tail call i32 @rewinddir(i32* %0)
%5 = tail call %struct.dirent* @readdir(i32* %0)
%6 = icmp eq %struct.dirent* %5, null
br i1 %6, label %27, label %7
7: ; preds = %3
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %23
%10 = phi i8 [ 0, %7 ], [ %24, %23 ]
%11 = phi %struct.dirent* [ %5, %7 ], [ %25, %23 ]
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = tail call i32 @strncmp(i8* %13, i8* %1, i32 %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %9
%17 = icmp eq i8 %10, 0
%18 = load i8*, i8** %12
%19 = getelementptr inbounds i8, i8* %18, i64 %8
%20 = load i8, i8* %19
br i1 %17, label %23, label %21
21: ; preds = %16
%22 = icmp eq i8 %10, %20
br i1 %22, label %23, label %27
23: ; preds = %16, %9, %21
%24 = phi i8 [ %10, %9 ], [ %10, %21 ], [ %20, %16 ]
%25 = tail call %struct.dirent* @readdir(i32* %0)
%26 = icmp eq %struct.dirent* %25, null
br i1 %26, label %27, label %9
27: ; preds = %23, %21, %3
%28 = phi i8 [ 0, %3 ], [ 0, %21 ], [ %24, %23 ]
ret i8 %28
}