#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;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ basename(char *);
int close(int);
int /*<<< orphan*/ exit(int);
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ unlink(char *);
scalar_t__ write_contents(int, int, struct stat, char *);
scalar_t__ write_header(int, struct stat, int /*<<< orphan*/);
int append_file(char *file_name, int ar_fd, char *archive_name, int verbose) {
  int in_fd = open(file_name, O_RDONLY);
  if (in_fd == -1) {
    perror("Error opening file");
    unlink(archive_name);
    exit(-1);
  }
  struct stat st;
  int num_written = 0;
  if (fstat(in_fd, &st) == -1) {
    perror("Unable to stat file");
    exit(-1);
  }
  if (!S_ISREG(st.st_mode)) {
    printf("%s: File format not recognized\n", file_name);
    unlink(archive_name);
    exit(-1);
  };
  ;
  num_written += write_header(ar_fd, st, basename(file_name));
  num_written += write_contents(ar_fd, in_fd, st, archive_name);
  if (close(in_fd) == -1) {
    perror("Unable to close input file");
    exit(-1);
  }
  if (verbose)
    printf("a - %s\n", file_name);
  return num_written;
}


x86 assembly (clang, -O0):
<append_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1204 <append_file+0x54>
lea    0xe14(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1040 <unlink@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1236 <append_file+0x86>
lea    0xdec(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%eax
jne    126a <append_file+0xba>
mov    -0x8(%rbp),%rsi
lea    0xdd5(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <unlink@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <basename@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
mov    -0x28(%rbp),%esi
call   10c0 <write_header@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%edx
call   1030 <write_contents@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    12d9 <append_file+0x129>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12f3 <append_file+0x143>
mov    -0x8(%rbp),%rsi
lea    0xd74(%rip),%rdi
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r12
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1266 <append_file+0xb6>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1274 <append_file+0xc4>
mov    %r14d,0x4(%rsp)
mov    (%rsp),%r14d
mov    %r14d,%edi
call   1080 <S_ISREG@plt>
test   %eax,%eax
je     127d <append_file+0xcd>
mov    %r12,%rdi
call   1070 <basename@plt>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %eax,%edx
call   10c0 <write_header@plt>
mov    %rax,%r13
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r14d,%edx
mov    %r15,%rcx
call   1030 <write_contents@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
je     129e <append_file+0xee>
cmpl   $0x0,0x4(%rsp)
je     1252 <append_file+0xa2>
lea    0xe18(%rip),%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
add    %r13d,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd93(%rip),%rdi
call   10a0 <perror@plt>
jmp    128c <append_file+0xdc>
lea    0xd98(%rip),%rdi
jmp    12a5 <append_file+0xf5>
lea    0xda3(%rip),%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
mov    %r15,%rdi
call   1040 <unlink@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xda2(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"Error opening file\00"
@.str.1 = [20 x i8] c"Unable to stat file\00"
@.str.2 = [32 x i8] c"%s: File format not recognized\0A\00"
@.str.3 = [27 x i8] c"Unable to close input file\00"
@.str.4 = [8 x i8] c"a - %s\0A\00"
define i32 @append_file(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%19 = load i8*, i8** %7
%20 = call i32 @unlink(i8* %19)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %4
store i32 0, i32* %11
%23 = load i32, i32* %9
%24 = call i32 @fstat(i32 %23, %struct.stat* %10)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %22
%30 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @S_ISREG(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %29
%35 = load i8*, i8** %5
%36 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %7
%38 = call i32 @unlink(i8* %37)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %29
%41 = load i32, i32* %6
%42 = load i8*, i8** %5
%43 = call i32 @basename(i8* %42)
%44 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @write_header(i32 %41, i32 %45, i32 %43)
%47 = load i32, i32* %11
%48 = sext i32 %47 to i64
%49 = add nsw i64 %48, %46
%50 = trunc i64 %49 to i32
store i32 %50, i32* %11
%51 = load i32, i32* %6
%52 = load i32, i32* %9
%53 = load i8*, i8** %7
%54 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i64 @write_contents(i32 %51, i32 %52, i32 %55, i8* %53)
%57 = load i32, i32* %11
%58 = sext i32 %57 to i64
%59 = add nsw i64 %58, %56
%60 = trunc i64 %59 to i32
store i32 %60, i32* %11
%61 = load i32, i32* %9
%62 = call i32 @close(i32 %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %67
64: ; preds = %40
%65 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%66 = call i32 @exit(i32 -1)
unreachable
67: ; preds = %40
%68 = load i32, i32* %8
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i8*, i8** %5
%72 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %71)
br label %73
73: ; preds = %70, %67
%74 = load i32, i32* %11
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"Error opening file\00"
@.str.1 = [20 x i8] c"Unable to stat file\00"
@.str.2 = [32 x i8] c"%s: File format not recognized\0A\00"
@.str.3 = [27 x i8] c"Unable to close input file\00"
@.str.4 = [8 x i8] c"a - %s\0A\00"
define i32 @append_file(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.stat
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @unlink(i8* %2)
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %4
%14 = bitcast %struct.stat* %5 to i8*
%15 = call i32 @fstat(i32 %7, %struct.stat* %5)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %13
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i32 @S_ISREG(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%27 = tail call i32 @unlink(i8* %2)
%28 = tail call i32 @exit(i32 -1)
unreachable
29: ; preds = %20
%30 = tail call i32 @basename(i8* %0)
%31 = tail call i64 @write_header(i32 %1, i32 %22, i32 %30)
%32 = tail call i64 @write_contents(i32 %1, i32 %7, i32 %22, i8* %2)
%33 = tail call i32 @close(i32 %7)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%37 = tail call i32 @exit(i32 -1)
unreachable
38: ; preds = %29
%39 = icmp eq i32 %3, 0
br i1 %39, label %42, label %40
40: ; preds = %38
%41 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %42
42: ; preds = %40, %38
%43 = add i64 %32, %31
%44 = trunc i64 %43 to i32
ret i32 %44
}