#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ append_file(int /*<<< orphan*/, int, char *, int);
char *basename(char *);
int /*<<< orphan*/ exit(int);
int open_archive(char *, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int stat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
int /*<<< orphan*/ unlink(char *);
void append_all(int index, int argc, char **argv, int verbose) {
  DIR *dir;
  struct dirent *ent;
  struct stat st;
  dir = opendir(".");
  if (dir == NULL) {
    perror("Error opening current directory");
    exit(-1);
  }
  char *archive_name = argv[index];
  int ar_fd = open_archive(archive_name, 1);
  if (ar_fd == -1) {
    perror("Error opening file");
  }
  while ((ent = readdir(dir)) != NULL) {
    if (!strcmp(ent->d_name, archive_name) ||
        !strcmp(ent->d_name, basename(argv[0]))) {
      ;
      continue;
    }
    if (stat(ent->d_name, &st) == -1) {
      perror("Error stating file");
      unlink(archive_name);
      exit(-1);
    }
    if (S_ISREG(st.st_mode)) {
      append_file(ent->d_name, ar_fd, archive_name, verbose);
    }
  }
}


x86 assembly (clang, -O0):
<append_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    0xe34(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f6 <append_all+0x46>
lea    0xe1b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   10b0 <open_archive@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    122d <append_all+0x7d>
lea    0xdfa(%rip),%rdi
call   10c0 <perror@plt>
jmp    1232 <append_all+0x82>
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12f2 <append_all+0x142>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
je     128a <append_all+0xda>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <basename@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    128f <append_all+0xdf>
jmp    1232 <append_all+0x82>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    12c6 <append_all+0x116>
lea    0xd87(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     12ed <append_all+0x13d>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1050 <append_file@plt>
jmp    1232 <append_all+0x82>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,0x4(%rsp)
mov    %rdx,%r15
mov    %edi,%ebp
lea    0xe35(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12b8 <append_all+0x108>
mov    %rax,%rbx
movslq %ebp,%rax
mov    (%r15,%rax,8),%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
call   10b0 <open_archive@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     1289 <append_all+0xd9>
mov    %rbx,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     127a <append_all+0xca>
mov    %rax,%r14
jmp    1220 <append_all+0x70>
xchg   %ax,%ax
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     127a <append_all+0xca>
mov    (%r14),%edi
mov    %rbp,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1210 <append_all+0x60>
mov    (%r14),%r13d
mov    (%r15),%rdi
call   1070 <basename@plt>
mov    %r13d,%edi
mov    %rax,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1210 <append_all+0x60>
mov    (%r14),%edi
mov    %rsp,%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     129a <append_all+0xea>
mov    (%rsp),%edi
call   1090 <S_ISREG@plt>
test   %rax,%rax
je     1210 <append_all+0x60>
mov    (%r14),%edi
mov    %r12d,%esi
mov    %rbp,%rdx
mov    0x4(%rsp),%ecx
call   1050 <append_file@plt>
jmp    1210 <append_all+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd92(%rip),%rdi
call   10c0 <perror@plt>
jmp    11fc <append_all+0x4c>
lea    0xd94(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xd43(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [32 x i8] c"Error opening current directory\00"
@.str.2 = [19 x i8] c"Error opening file\00"
@.str.3 = [19 x i8] c"Error stating file\00"
define void @append_all(i32 %0, i32 %1, i8** %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca %struct.stat
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i32 %3, i32* %8
%14 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %9
%15 = load i32*, i32** %9
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %4
%21 = load i8**, i8*** %7
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
%25 = load i8*, i8** %24
store i8* %25, i8** %12
%26 = load i8*, i8** %12
%27 = call i32 @open_archive(i8* %26, i32 1)
store i32 %27, i32* %13
%28 = load i32, i32* %13
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %20
br label %33
33: ; preds = %79, %54, %32
%34 = load i32*, i32** %9
%35 = call %struct.dirent* @readdir(i32* %34)
store %struct.dirent* %35, %struct.dirent** %10
%36 = icmp ne %struct.dirent* %35, null
br i1 %36, label %37, label %80
37: ; preds = %33
%38 = load %struct.dirent*, %struct.dirent** %10
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i8*, i8** %12
%42 = call i32 @strcmp(i32 %40, i8* %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %37
%45 = load %struct.dirent*, %struct.dirent** %10
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load i8**, i8*** %7
%49 = getelementptr inbounds i8*, i8** %48, i64 0
%50 = load i8*, i8** %49
%51 = call i8* @basename(i8* %50)
%52 = call i32 @strcmp(i32 %47, i8* %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %55, label %54
54: ; preds = %44, %37
br label %33
55: ; preds = %44
%56 = load %struct.dirent*, %struct.dirent** %10
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @stat(i32 %58, %struct.stat* %11)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %66
61: ; preds = %55
%62 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%63 = load i8*, i8** %12
%64 = call i32 @unlink(i8* %63)
%65 = call i32 @exit(i32 -1)
unreachable
66: ; preds = %55
%67 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i64 @S_ISREG(i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %79
71: ; preds = %66
%72 = load %struct.dirent*, %struct.dirent** %10
%73 = getelementptr inbounds %struct.dirent, %struct.dirent* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = load i32, i32* %13
%76 = load i8*, i8** %12
%77 = load i32, i32* %8
%78 = call i32 @append_file(i32 %74, i32 %75, i8* %76, i32 %77)
br label %79
79: ; preds = %71, %66
br label %33
80: ; preds = %33
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [32 x i8] c"Error opening current directory\00"
@.str.2 = [19 x i8] c"Error opening file\00"
@.str.3 = [19 x i8] c"Error stating file\00"
define void @append_all(i32 %0, i32 %1, i8** nocapture %2, i32 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %4
%13 = sext i32 %0 to i64
%14 = getelementptr inbounds i8*, i8** %2, i64 %13
%15 = load i8*, i8** %14
%16 = tail call i32 @open_archive(i8* %15, i32 1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = call %struct.dirent* @readdir(i32* %7)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %55, label %23
23: ; preds = %20
%24 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %25
25: ; preds = %23, %37
%26 = phi %struct.dirent* [ %21, %23 ], [ %38, %37 ]
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @strcmp(i32 %28, i8* %15)
%30 = icmp eq i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %25
%32 = load i32, i32* %27
%33 = load i8*, i8** %2
%34 = call i8* @basename(i8* %33)
%35 = call i32 @strcmp(i32 %32, i8* %34)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %48, %52, %25, %31
%38 = call %struct.dirent* @readdir(i32* %7)
%39 = icmp eq %struct.dirent* %38, null
br i1 %39, label %55, label %25
40: ; preds = %31
%41 = load i32, i32* %27
%42 = call i32 @stat(i32 %41, %struct.stat* %5)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @unlink(i8* %15)
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %40
%49 = load i32, i32* %24
%50 = call i64 @S_ISREG(i32 %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %37, label %52
52: ; preds = %48
%53 = load i32, i32* %27
%54 = call i32 @append_file(i32 %53, i32 %16, i8* %15, i32 %3)
br label %37
55: ; preds = %37, %20
ret void
}