#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 */
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
size_t strlen(char *);
unsigned char test(void) {
  DIR *dir;
  struct dirent *dp;
  unsigned char c;
  if ((dir = opendir(".")) == 0) {
    perror("open .");
    return 0;
  }
  c = 0;
  while ((dp = readdir(dir)) != 0) {
    size_t len;
    len = strlen(dp->d_name);
    if (len < 4)
      continue;
    if (strcmp(dp->d_name + len - 4, ".TST"))
      continue;
    if (len != 5) {
      fprintf(stderr, "warning: %s\n", dp->d_name);
      printf(" length");
      continue;
    }
    if (c)
      printf(" double%d\n", c);
    c = dp->d_name[0];
  }
  if (closedir(dir))
    perror("close .");
  return c;
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xea3(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11b7 <test+0x37>
lea    0xe57(%rip),%rdi
call   10a0 <perror@plt>
movb   $0x0,-0x1(%rbp)
jmp    12a8 <test+0x128>
movb   $0x0,-0x19(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1283 <test+0x103>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x4,-0x28(%rbp)
jae    11f2 <test+0x72>
jmp    11bb <test+0x3b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
add    -0x28(%rbp),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xdff(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     121c <test+0x9c>
jmp    11bb <test+0x3b>
cmpq   $0x5,-0x28(%rbp)
je     1256 <test+0xd6>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xdce(%rip),%rsi
call   1080 <fprintf@plt>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11bb <test+0x3b>
cmpb   $0x0,-0x19(%rbp)
je     1272 <test+0xf2>
movzbl -0x19(%rbp),%esi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
jmp    11bb <test+0x3b>
mov    -0x10(%rbp),%rdi
call   1060 <closedir@plt>
cmp    $0x0,%rax
je     12a2 <test+0x122>
lea    0xd8f(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x19(%rbp),%al
mov    %al,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xea0(%rip),%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     126e <test+0xee>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     124e <test+0xce>
mov    %rax,%r15
lea    0xe49(%rip),%r14
xor    %ebp,%ebp
jmp    1200 <test+0x80>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xe37(%rip),%rsi
call   1080 <fprintf@plt>
lea    0xe38(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1250 <test+0xd0>
mov    (%r15),%r13
mov    %r13,%rdi
call   1040 <strlen@plt>
cmp    $0x4,%rax
jb     11f0 <test+0x70>
mov    %rax,%r12
lea    (%rax,%r13,1),%rdi
add    $0xfffffffffffffffc,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    11f0 <test+0x70>
cmp    $0x5,%r12
jne    11c2 <test+0x42>
test   %bpl,%bpl
je     1246 <test+0xc6>
movzbl %bpl,%esi
lea    0xde2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%rax
movzbl (%rax),%ebp
jmp    11f0 <test+0x70>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1060 <closedir@plt>
test   %rax,%rax
jne    1279 <test+0xf9>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
xor    %ebp,%ebp
jmp    1280 <test+0x100>
lea    0xdac(%rip),%rdi
call   10a0 <perror@plt>
jmp    125d <test+0xdd>

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [7 x i8] c"open .\00"
@.str.2 = [5 x i8] c".TST\00"
@stderr = global i32 0
@.str.3 = [13 x i8] c"warning: %s\0A\00"
@.str.4 = [8 x i8] c" length\00"
@.str.5 = [11 x i8] c" double%d\0A\00"
@.str.6 = [8 x i8] c"close .\00"
define zeroext i8 @test() {
%1 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %41, label %3
3: ; preds = %0
%4 = tail call %struct.dirent* @readdir(i32* %1)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %37, label %6
6: ; preds = %3, %33
%7 = phi %struct.dirent* [ %35, %33 ], [ %4, %3 ]
%8 = phi i8 [ %34, %33 ], [ 0, %3 ]
%9 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = tail call i64 @strlen(i8* dereferenceable(1) %10)
%12 = icmp ult i64 %11, 4
br i1 %12, label %33, label %13
13: ; preds = %6
%14 = getelementptr inbounds i8, i8* %10, i64 -4
%15 = getelementptr inbounds i8, i8* %14, i64 %11
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %13
%19 = icmp eq i64 %11, 5
br i1 %19, label %25, label %20
20: ; preds = %18
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %9
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %33
25: ; preds = %18
%26 = icmp eq i8 %8, 0
br i1 %26, label %30, label %27
27: ; preds = %25
%28 = zext i8 %8 to i32
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %27, %25
%31 = load i8*, i8** %9
%32 = load i8, i8* %31
br label %33
33: ; preds = %13, %6, %30, %20
%34 = phi i8 [ %8, %20 ], [ %32, %30 ], [ %8, %6 ], [ %8, %13 ]
%35 = tail call %struct.dirent* @readdir(i32* %1)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %37, label %6
37: ; preds = %33, %3
%38 = phi i8 [ 0, %3 ], [ %34, %33 ]
%39 = tail call i64 @closedir(i32* %1)
%40 = icmp eq i64 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %37, %0
%42 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), %37 ]
%43 = phi i8 [ 0, %0 ], [ %38, %37 ]
%44 = tail call i32 @perror(i8* %42)
br label %45
45: ; preds = %41, %37
%46 = phi i8 [ %38, %37 ], [ %43, %41 ]
ret i8 %46
}