#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*/ buffer;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ ProcessFile(char *, int /*<<< orphan*/ *, char *);
int chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strdup(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ unlink(char *);
void ProcessDir(char *dir) {
  char buffer[1025];
  char *here = strdup(getcwd(buffer, sizeof(buffer)));
  DIR *d;
  struct dirent *ent;
  FILE *output;
  if (chdir(dir) == -1) {
    fprintf(stderr, "Failed on %s\n", dir);
    exit(1);
  }
  d = opendir(".");
  if (d == NULL) {
    fprintf(stderr, "Failed on %s\n", dir);
    exit(1);
  }
  output = fopen("copyright.patch", "w");
  if (output == NULL) {
    fprintf(stderr, "Failed to create \"copyright.patch\" in %s\n", dir);
    exit(1);
  }
  while ((ent = readdir(d)) != NULL) {
    int len = strlen(ent->d_name);
    if (ent->d_name[len - 2] == '.' &&
        (ent->d_name[len - 1] == 'c' || ent->d_name[len - 1] == 'h'))
      ProcessFile(ent->d_name, output, dir);
  }
  if (ftell(output) == 0) {
    fprintf(stderr, "Nothing in %s\n", dir);
    fclose(output);
    unlink("copyright.patch");
  } else
    fclose(output);
  closedir(d);
  chdir(here);
  free(here);
}


x86 assembly (clang, -O0):
<ProcessDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x401,%esi
call   10a0 <getcwd@plt>
mov    %eax,%edi
call   1110 <strdup@plt>
mov    %rax,-0x418(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1253 <ProcessDir+0x63>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdbc(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xdb4(%rip),%rdi
call   1070 <opendir@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jne    1297 <ProcessDir+0xa7>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd78(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xd72(%rip),%rdi
lea    0xd7b(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    12e2 <ProcessDir+0xf2>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd4f(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
jmp    12e7 <ProcessDir+0xf7>
mov    -0x420(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     13a1 <ProcessDir+0x1b1>
mov    -0x428(%rbp),%rax
mov    (%rax),%rdi
call   1080 <strlen@plt>
mov    %eax,-0x434(%rbp)
mov    -0x428(%rbp),%rax
mov    (%rax),%rax
mov    -0x434(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    139c <ProcessDir+0x1ac>
mov    -0x428(%rbp),%rax
mov    (%rax),%rax
mov    -0x434(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x63,%eax
je     1382 <ProcessDir+0x192>
mov    -0x428(%rbp),%rax
mov    (%rax),%rax
mov    -0x434(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x68,%eax
jne    139c <ProcessDir+0x1ac>
mov    -0x428(%rbp),%rax
mov    (%rax),%rdi
mov    -0x430(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1050 <ProcessFile@plt>
jmp    12e7 <ProcessDir+0xf7>
mov    -0x430(%rbp),%rdi
call   10d0 <ftell@plt>
cmp    $0x0,%rax
jne    13ed <ProcessDir+0x1fd>
mov    0x2c22(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xc81(%rip),%rsi
call   10c0 <fprintf@plt>
mov    -0x430(%rbp),%rdi
call   1060 <fclose@plt>
lea    0xc2d(%rip),%rdi
call   1040 <unlink@plt>
jmp    13f9 <ProcessDir+0x209>
mov    -0x430(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x420(%rbp),%rdi
call   10b0 <closedir@plt>
mov    -0x418(%rbp),%rdi
call   1090 <chdir@plt>
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ProcessDir>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x401,%esi
call   10a0 <getcwd@plt>
mov    %eax,%edi
call   1110 <strdup@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1090 <chdir@plt>
cmp    $0xffffffff,%eax
je     132a <ProcessDir+0x13a>
lea    0xddc(%rip),%rdi
call   1070 <opendir@plt>
test   %rax,%rax
je     132a <ProcessDir+0x13a>
mov    %rax,%r13
lea    0xdc6(%rip),%rdi
lea    0xdcf(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
je     133c <ProcessDir+0x14c>
mov    %rax,%r12
mov    %r13,%rdi
call   10e0 <readdir@plt>
test   %rax,%rax
je     12be <ProcessDir+0xce>
mov    %rax,%rbx
jmp    129b <ProcessDir+0xab>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rsi
mov    %r15,%rdx
call   1050 <ProcessFile@plt>
mov    %r13,%rdi
call   10e0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12be <ProcessDir+0xce>
mov    (%rbx),%rdi
call   1080 <strlen@plt>
mov    (%rbx),%rdi
cltq
cmpb   $0x2e,-0x2(%rax,%rdi,1)
jne    128b <ProcessDir+0x9b>
movzbl -0x1(%rax,%rdi,1),%eax
cmp    $0x68,%al
je     1280 <ProcessDir+0x90>
cmp    $0x63,%al
je     1280 <ProcessDir+0x90>
jmp    128b <ProcessDir+0x9b>
mov    %r12,%rdi
call   10d0 <ftell@plt>
test   %rax,%rax
je     12d5 <ProcessDir+0xe5>
mov    %r12,%rdi
call   1060 <fclose@plt>
jmp    1301 <ProcessDir+0x111>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
mov    %r15,%rdx
call   10c0 <fprintf@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
lea    0xd14(%rip),%rdi
call   1040 <unlink@plt>
mov    %r13,%rdi
call   10b0 <closedir@plt>
mov    %r14,%rdi
call   1090 <chdir@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
lea    0xcc6(%rip),%rsi
jmp    134c <ProcessDir+0x15c>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
lea    0xcd6(%rip),%rsi
mov    %r15,%rdx
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [14 x i8] c"Failed on %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [16 x i8] c"copyright.patch\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [42 x i8] c"Failed to create \22copyright.patch\22 in %s\0A\00"
@.str.5 = [15 x i8] c"Nothing in %s\0A\00"
define void @ProcessDir(i8* %0) {
%2 = alloca i8*
%3 = alloca [1025 x i8]
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %2
%9 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%10 = call i32 @getcwd(i8* %9, i32 1025)
%11 = call i8* @strdup(i32 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %2
%13 = call i32 @chdir(i8* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %2
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %5
%22 = load i32*, i32** %5
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %2
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %30, i32** %7
%31 = load i32*, i32** %7
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %2
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %29
br label %39
39: ; preds = %87, %38
%40 = load i32*, i32** %5
%41 = call %struct.dirent* @readdir(i32* %40)
store %struct.dirent* %41, %struct.dirent** %6
%42 = icmp ne %struct.dirent* %41, null
br i1 %42, label %43, label %88
43: ; preds = %39
%44 = load %struct.dirent*, %struct.dirent** %6
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i32 @strlen(i8* %46)
store i32 %47, i32* %8
%48 = load %struct.dirent*, %struct.dirent** %6
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = load i32, i32* %8
%52 = sub nsw i32 %51, 2
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 46
br i1 %57, label %58, label %87
58: ; preds = %43
%59 = load %struct.dirent*, %struct.dirent** %6
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = load i32, i32* %8
%63 = sub nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %61, i64 %64
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 99
br i1 %68, label %80, label %69
69: ; preds = %58
%70 = load %struct.dirent*, %struct.dirent** %6
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 0
%72 = load i8*, i8** %71
%73 = load i32, i32* %8
%74 = sub nsw i32 %73, 1
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %72, i64 %75
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = icmp eq i32 %78, 104
br i1 %79, label %80, label %87
80: ; preds = %69, %58
%81 = load %struct.dirent*, %struct.dirent** %6
%82 = getelementptr inbounds %struct.dirent, %struct.dirent* %81, i32 0, i32 0
%83 = load i8*, i8** %82
%84 = load i32*, i32** %7
%85 = load i8*, i8** %2
%86 = call i32 @ProcessFile(i8* %83, i32* %84, i8* %85)
br label %87
87: ; preds = %80, %69, %43
br label %39
88: ; preds = %39
%89 = load i32*, i32** %7
%90 = call i64 @ftell(i32* %89)
%91 = icmp eq i64 %90, 0
br i1 %91, label %92, label %99
92: ; preds = %88
%93 = load i32, i32* @stderr
%94 = load i8*, i8** %2
%95 = call i32 @fprintf(i32 %93, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %94)
%96 = load i32*, i32** %7
%97 = call i32 @fclose(i32* %96)
%98 = call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %102
99: ; preds = %88
%100 = load i32*, i32** %7
%101 = call i32 @fclose(i32* %100)
br label %102
102: ; preds = %99, %92
%103 = load i32*, i32** %5
%104 = call i32 @closedir(i32* %103)
%105 = load i8*, i8** %4
%106 = call i32 @chdir(i8* %105)
%107 = load i8*, i8** %4
%108 = call i32 @free(i8* %107)
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [14 x i8] c"Failed on %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [16 x i8] c"copyright.patch\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [42 x i8] c"Failed to create \22copyright.patch\22 in %s\0A\00"
@.str.5 = [15 x i8] c"Nothing in %s\0A\00"
define void @ProcessDir(i8* %0) {
%2 = alloca [1025 x i8]
%3 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%4 = call i32 @getcwd(i8* %3, i32 1025)
%5 = call i8* @strdup(i32 %4)
%6 = call i32 @chdir(i8* %0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = call %struct.dirent* @readdir(i32* %13)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %50, label %29
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22, %47
%30 = phi %struct.dirent* [ %48, %47 ], [ %23, %22 ]
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i64 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @strlen(i8* %32)
%34 = load i8*, i8** %31
%35 = add nsw i32 %33, -2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 46
br i1 %39, label %40, label %47
40: ; preds = %29
%41 = add nsw i32 %33, -1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %34, i64 %42
%44 = load i8, i8* %43
switch i8 %44, label %47 [
i8 99, label %45
i8 104, label %45
]
45: ; preds = %40, %40
%46 = call i32 @ProcessFile(i8* %34, i32* %20, i8* %0)
br label %47
47: ; preds = %40, %45, %29
%48 = call %struct.dirent* @readdir(i32* %13)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %50, label %29
50: ; preds = %47, %22
%51 = call i64 @ftell(i32* %20)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%56 = call i32 @fclose(i32* %20)
%57 = call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %60
58: ; preds = %50
%59 = call i32 @fclose(i32* %20)
br label %60
60: ; preds = %58, %53
%61 = call i32 @closedir(i32* %13)
%62 = call i32 @chdir(i8* %5)
%63 = call i32 @free(i8* %5)
ret void
}