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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ seekdir(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ telldir(int /*<<< orphan*/ *);
scalar_t__ unlink(char *);
int os2_delete(DIR *d) {
  off_t offsets[100];
  int i, j;
  struct dirent *de;
  char names[100][30];
  for (i = 0, de = readdir(d); de && i < 100; de = readdir(d), i++) {
    offsets[i] = telldir(d);
    strcpy(names[i], de->d_name);
  }
  if (i == 0) {
    return 0;
  }
  for (j = 0; j < ((i) < (4) ? (i) : (4)); j++) {
    char fname[40];
    sprintf(fname,
            "test.dir"
            "/%s",
            names[j]);
    unlink(fname) == 0 ||
        (fprintf(stderr, "Failed for %s - %s\n", "unlink", strerror(errno)),
         exit(1), 1);
  }
  seekdir(d, offsets[j - 1]);
  return j;
}


x86 assembly (clang, -O0):
<os2_delete>:
push   %rbp
mov    %rsp,%rbp
sub    $0xdb0,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1a4(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x1b0(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x1b0(%rbp)
mov    %al,-0xda1(%rbp)
je     11df <os2_delete+0x4f>
cmpl   $0x64,-0x1a4(%rbp)
setl   %al
mov    %al,-0xda1(%rbp)
mov    -0xda1(%rbp),%al
test   $0x1,%al
jne    11f2 <os2_delete+0x62>
jmp    1252 <os2_delete+0xc2>
mov    -0x10(%rbp),%rdi
call   1080 <telldir@plt>
mov    %eax,%ecx
movslq -0x1a4(%rbp),%rax
mov    %ecx,-0x1a0(%rbp,%rax,4)
movslq -0x1a4(%rbp),%rax
lea    -0xd70(%rbp),%rdi
imul   $0x1e,%rax,%rax
add    %rax,%rdi
mov    -0x1b0(%rbp),%rax
mov    (%rax),%esi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x1b0(%rbp)
mov    -0x1a4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1a4(%rbp)
jmp    11b9 <os2_delete+0x29>
cmpl   $0x0,-0x1a4(%rbp)
jne    126b <os2_delete+0xdb>
movl   $0x0,-0x4(%rbp)
jmp    137c <os2_delete+0x1ec>
movl   $0x0,-0x1a8(%rbp)
mov    -0x1a8(%rbp),%eax
mov    %eax,-0xda8(%rbp)
cmpl   $0x4,-0x1a4(%rbp)
jge    129f <os2_delete+0x10f>
mov    -0x1a4(%rbp),%eax
mov    %eax,-0xdac(%rbp)
jmp    12af <os2_delete+0x11f>
mov    $0x4,%eax
mov    %eax,-0xdac(%rbp)
jmp    12af <os2_delete+0x11f>
mov    -0xda8(%rbp),%eax
mov    -0xdac(%rbp),%ecx
cmp    %ecx,%eax
jge    1358 <os2_delete+0x1c8>
lea    -0xda0(%rbp),%rdi
movslq -0x1a8(%rbp),%rax
lea    -0xd70(%rbp),%rdx
imul   $0x1e,%rax,%rax
add    %rax,%rdx
lea    0xd1a(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0xda0(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     1344 <os2_delete+0x1b4>
mov    0x2cd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xdb0(%rbp)
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0xdb0(%rbp),%edi
mov    %rax,%rcx
lea    0xcde(%rip),%rsi
lea    0xceb(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1a8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1a8(%rbp)
jmp    1275 <os2_delete+0xe5>
mov    -0x10(%rbp),%rdi
mov    -0x1a8(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x1a0(%rbp,%rax,4),%esi
call   1070 <seekdir@plt>
mov    -0x1a8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xdb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os2_delete>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd78,%rsp
mov    %rdi,%r15
call   1060 <readdir@plt>
test   %rax,%rax
je     1261 <os2_delete+0xd1>
mov    %rax,%r14
lea    0x1c0(%rsp),%rbx
xor    %ebp,%ebp
nop
mov    %r15,%rdi
call   1080 <telldir@plt>
mov    %eax,0x30(%rsp,%rbp,4)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1060 <readdir@plt>
mov    %rax,%r14
lea    0x1(%rbp),%rax
test   %r14,%r14
je     11f8 <os2_delete+0x68>
add    $0x1e,%rbx
cmp    $0x63,%rbp
mov    %rax,%rbp
jb     11c0 <os2_delete+0x30>
cmp    $0x4,%eax
mov    $0x4,%r14d
cmovb  %eax,%r14d
mov    $0x1,%ebp
lea    0x1c0(%rsp),%rbx
lea    0xde7(%rip),%r12
mov    %rsp,%r13
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1090 <sprintf@plt>
mov    %r13,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jne    1279 <os2_delete+0xe9>
add    $0x1e,%rbx
lea    (%r14,%rbp,1),%rax
add    $0xffffffffffffffff,%rax
add    $0xffffffffffffffff,%rbp
cmp    $0x1,%rax
jne    1220 <os2_delete+0x90>
neg    %ebp
mov    0x30(%rsp,%rbp,4),%esi
mov    %r15,%rdi
call   1070 <seekdir@plt>
jmp    1264 <os2_delete+0xd4>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0xd78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d60(%rip),%rax
mov    (%rax),%ebx
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd75(%rip),%rsi
lea    0xd82(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [12 x i8] c"test.dir/%s\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Failed for %s - %s\0A\00"
@.str.2 = [7 x i8] c"unlink\00"
@errno = global i32 0
define i32 @os2_delete(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [100 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.dirent*
%8 = alloca [100 x [30 x i8]]
%9 = alloca [40 x i8]
store i32* %0, i32** %3
store i32 0, i32* %5
%10 = load i32*, i32** %3
%11 = call %struct.dirent* @readdir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %7
br label %12
12: ; preds = %34, %1
%13 = load %struct.dirent*, %struct.dirent** %7
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 100
br label %18
18: ; preds = %15, %12
%19 = phi i1 [ false, %12 ], [ %17, %15 ]
br i1 %19, label %20, label %39
20: ; preds = %18
%21 = load i32*, i32** %3
%22 = call i32 @telldir(i32* %21)
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [100 x i32], [100 x i32]* %4, i64 0, i64 %24
store i32 %22, i32* %25
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [100 x [30 x i8]], [100 x [30 x i8]]* %8, i64 0, i64 %27
%29 = getelementptr inbounds [30 x i8], [30 x i8]* %28, i64 0, i64 0
%30 = load %struct.dirent*, %struct.dirent** %7
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @strcpy(i8* %29, i32 %32)
br label %34
34: ; preds = %20
%35 = load i32*, i32** %3
%36 = call %struct.dirent* @readdir(i32* %35)
store %struct.dirent* %36, %struct.dirent** %7
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %12
39: ; preds = %18
%40 = load i32, i32* %5
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
store i32 0, i32* %2
br label %86
43: ; preds = %39
store i32 0, i32* %6
br label %44
44: ; preds = %74, %43
%45 = load i32, i32* %6
%46 = load i32, i32* %5
%47 = icmp slt i32 %46, 4
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = load i32, i32* %5
br label %51
50: ; preds = %44
br label %51
51: ; preds = %50, %48
%52 = phi i32 [ %49, %48 ], [ 4, %50 ]
%53 = icmp slt i32 %45, %52
br i1 %53, label %54, label %77
54: ; preds = %51
%55 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%56 = load i32, i32* %6
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [100 x [30 x i8]], [100 x [30 x i8]]* %8, i64 0, i64 %57
%59 = getelementptr inbounds [30 x i8], [30 x i8]* %58, i64 0, i64 0
%60 = call i32 @sprintf(i8* %55, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %59)
%61 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%62 = call i64 @unlink(i8* %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %71, label %64
64: ; preds = %54
%65 = load i32, i32* @stderr
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %67)
%69 = call i32 @exit(i32 1)
unreachable
70: ; No predecessors!
br label %71
71: ; preds = %70, %54
%72 = phi i1 [ true, %54 ], [ true, %70 ]
%73 = zext i1 %72 to i32
br label %74
74: ; preds = %71
%75 = load i32, i32* %6
%76 = add nsw i32 %75, 1
store i32 %76, i32* %6
br label %44
77: ; preds = %51
%78 = load i32*, i32** %3
%79 = load i32, i32* %6
%80 = sub nsw i32 %79, 1
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [100 x i32], [100 x i32]* %4, i64 0, i64 %81
%83 = load i32, i32* %82
%84 = call i32 @seekdir(i32* %78, i32 %83)
%85 = load i32, i32* %6
store i32 %85, i32* %2
br label %86
86: ; preds = %77, %42
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [12 x i8] c"test.dir/%s\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Failed for %s - %s\0A\00"
@.str.2 = [7 x i8] c"unlink\00"
@errno = global i32 0
define i32 @os2_delete(i32* %0) {
%2 = alloca [100 x i32]
%3 = alloca [100 x [30 x i8]]
%4 = alloca [40 x i8]
%5 = bitcast [100 x i32]* %2 to i8*
%6 = getelementptr inbounds [100 x [30 x i8]], [100 x [30 x i8]]* %3, i64 0, i64 0, i64 0
%7 = tail call %struct.dirent* @readdir(i32* %0)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %49, label %9
9: ; preds = %1, %9
%10 = phi i64 [ %19, %9 ], [ 0, %1 ]
%11 = phi %struct.dirent* [ %18, %9 ], [ %7, %1 ]
%12 = call i32 @telldir(i32* %0)
%13 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 %10
store i32 %12, i32* %13
%14 = getelementptr inbounds [100 x [30 x i8]], [100 x [30 x i8]]* %3, i64 0, i64 %10, i64 0
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strcpy(i8* %14, i32 %16)
%18 = call %struct.dirent* @readdir(i32* %0)
%19 = add nuw nsw i64 %10, 1
%20 = icmp ne %struct.dirent* %18, null
%21 = icmp ult i64 %10, 99
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %9, label %23
23: ; preds = %9
%24 = trunc i64 %19 to i32
%25 = icmp ult i32 %24, 4
%26 = select i1 %25, i32 %24, i32 4
%27 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%28 = zext i32 %26 to i64
br label %29
29: ; preds = %23, %41
%30 = phi i64 [ 0, %23 ], [ %42, %41 ]
%31 = getelementptr inbounds [100 x [30 x i8]], [100 x [30 x i8]]* %3, i64 0, i64 %30, i64 0
%32 = call i32 @sprintf(i8* %27, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = call i64 @unlink(i8* %27)
%34 = icmp eq i64 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %29
%42 = add nuw nsw i64 %30, 1
%43 = icmp eq i64 %42, %28
br i1 %43, label %44, label %29
44: ; preds = %41
%45 = and i64 %30, 4294967295
%46 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 %45
%47 = load i32, i32* %46
%48 = call i32 @seekdir(i32* %0, i32 %47)
br label %49
49: ; preds = %1, %44
%50 = phi i32 [ %26, %44 ], [ 0, %1 ]
ret i32 %50
}