#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 */

/* Variables and functions */
int /*<<< orphan*/ append_file(char *, int, char *, int);
int /*<<< orphan*/ check_args(int, int);
int close(int);
int /*<<< orphan*/ exit(int);
int open_archive(char *, int);
int /*<<< orphan*/ perror(char *);
int append(int index, int argc, char **argv, int verbose) {
  check_args(index, argc);
  int ar_fd;
  char *file_name;
  char *archive_name = argv[index];
  index++;
  ar_fd = open_archive(archive_name, 1);
  if (ar_fd == -1) {
    perror("Error opening file");
  }
  while (index < argc) {
    file_name = argv[index];
    append_file(file_name, ar_fd, archive_name, verbose);
    index++;
  }
  if (close(ar_fd) == -1) {
    perror("Unable to close archive file");
    exit(-1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <check_args@plt>
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1060 <open_archive@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c0 <append+0x60>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
jmp    11c5 <append+0x65>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1202 <append+0xa2>
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <append_file@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <append+0x65>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1229 <append+0xc9>
lea    0xdf9(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r13d
call   1040 <check_args@plt>
movslq %r13d,%rax
mov    (%rbx,%rax,8),%r15
mov    %r15,%rdi
mov    $0x1,%esi
call   1060 <open_archive@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     11e9 <append+0x89>
lea    0x1(%r13),%eax
cmp    %ebp,%eax
jge    11cb <append+0x6b>
cltq
lea    (%rbx,%rax,8),%rbx
not    %r13d
add    %ebp,%r13d
xor    %ebp,%ebp
nopl   (%rax)
mov    (%rbx,%rbp,8),%rdi
mov    %r12d,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <append_file@plt>
add    $0x1,%rbp
cmp    %ebp,%r13d
jne    11b0 <append+0x50>
mov    %r12d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     11f7 <append+0x97>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
jmp    1197 <append+0x37>
lea    0xe15(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Error opening file\00"
@.str.1 = [29 x i8] c"Unable to close archive file\00"
define i32 @append(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 i8*
%11 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i32 %3, i32* %8
%12 = load i32, i32* %5
%13 = load i32, i32* %6
%14 = call i32 @check_args(i32 %12, i32 %13)
%15 = load i8**, i8*** %7
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8*, i8** %15, i64 %17
%19 = load i8*, i8** %18
store i8* %19, i8** %11
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
store i32 %21, i32* %5
%22 = load i8*, i8** %11
%23 = call i32 @open_archive(i8* %22, i32 1)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %4
%27 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %4
br label %29
29: ; preds = %33, %28
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %46
33: ; preds = %29
%34 = load i8**, i8*** %7
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
%38 = load i8*, i8** %37
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = load i32, i32* %9
%41 = load i8*, i8** %11
%42 = load i32, i32* %8
%43 = call i32 @append_file(i8* %39, i32 %40, i8* %41, i32 %42)
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
br label %29
46: ; preds = %29
%47 = load i32, i32* %9
%48 = call i32 @close(i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %46
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"Error opening file\00"
@.str.1 = [29 x i8] c"Unable to close archive file\00"
define i32 @append(i32 %0, i32 %1, i8** nocapture %2, i32 %3) {
%5 = tail call i32 @check_args(i32 %0, i32 %1)
%6 = sext i32 %0 to i64
%7 = getelementptr inbounds i8*, i8** %2, i64 %6
%8 = load i8*, i8** %7
%9 = tail call i32 @open_archive(i8* %8, i32 1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %4
%12 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %4
%14 = add i32 %0, 1
%15 = icmp slt i32 %14, %1
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = sext i32 %14 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ %17, %16 ], [ %23, %18 ]
%20 = getelementptr inbounds i8*, i8** %2, i64 %19
%21 = load i8*, i8** %20
%22 = tail call i32 @append_file(i8* %21, i32 %9, i8* %8, i32 %3)
%23 = add nsw i64 %19, 1
%24 = trunc i64 %23 to i32
%25 = icmp eq i32 %24, %1
br i1 %25, label %26, label %18
26: ; preds = %18, %13
%27 = tail call i32 @close(i32 %9)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%31 = tail call i32 @exit(i32 -1)
unreachable
32: ; preds = %26
ret i32 0
}