#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 {
  scalar_t__ st_size;
  scalar_t__ st_mode;
  scalar_t__ st_gid;
  scalar_t__ st_uid;
  scalar_t__ st_mtime;
};
struct ar_hdr {
  int /*<<< orphan*/ ar_fmag;
  int /*<<< orphan*/ ar_size;
  int /*<<< orphan*/ ar_mode;
  int /*<<< orphan*/ ar_gid;
  int /*<<< orphan*/ ar_uid;
  int /*<<< orphan*/ ar_date;
  int /*<<< orphan*/ ar_name;
};
typedef int /*<<< orphan*/ hdr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ ARFMAG;
int _ar_error(char const *, int);
int /*<<< orphan*/ basename(char *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
int fwrite(struct ar_hdr *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct ar_hdr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, unsigned int);
char *strdup(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, int /*<<< orphan*/, int);
int _append_header(char const *archive, FILE *fp, char const *filename,
                   FILE *fp2) {
  struct ar_hdr hdr;
  struct stat st;
  char *p;
  if (fstat(fileno(fp2), &st) != 0)
    return _ar_error(filename, 1);
  if ((p = strdup(filename)) == NULL)
    return _ar_error(filename, 1);
  memset(&hdr, 0, sizeof(hdr));
  strncpy(hdr.ar_name, basename(p), sizeof(hdr.ar_name) - 1);
  free(p);
  snprintf(hdr.ar_date, sizeof(hdr.ar_date), "%u", (unsigned)st.st_mtime);
  snprintf(hdr.ar_uid, sizeof(hdr.ar_uid), "%u", (unsigned)st.st_uid);
  snprintf(hdr.ar_gid, sizeof(hdr.ar_gid), "%u", (unsigned)st.st_gid);
  snprintf(hdr.ar_mode, sizeof(hdr.ar_mode), "%o", (unsigned)st.st_mode);
  snprintf(hdr.ar_size, sizeof(hdr.ar_size), "%u", (unsigned)st.st_size);
  strncpy(hdr.ar_fmag, ARFMAG, sizeof(hdr.ar_fmag));
  if (fwrite(&hdr, sizeof(hdr), 1, fp) != 1)
    return _ar_error(archive, 1);
  return 0;
}


x86 assembly (clang, -O0):
<_append_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x70(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
je     11ef <_append_header+0x4f>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   10c0 <_ar_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1328 <_append_header+0x188>
mov    -0x20(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x78(%rbp)
cmp    $0x0,%rax
jne    121c <_append_header+0x7c>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   10c0 <_ar_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1328 <_append_header+0x188>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1060 <memset@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x78(%rbp),%rdi
call   1070 <basename@plt>
mov    -0x7c(%rbp),%edi
mov    %eax,%esi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x78(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    %eax,%ecx
mov    $0x4,%esi
lea    0xd98(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x38(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    %eax,%ecx
mov    $0x4,%esi
lea    0xd7e(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x3c(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    %eax,%ecx
mov    $0x4,%esi
lea    0xd64(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x40(%rbp),%edi
mov    -0x68(%rbp),%rax
mov    %eax,%ecx
mov    $0x4,%esi
lea    0xd4d(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x44(%rbp),%edi
mov    -0x70(%rbp),%rax
mov    %eax,%ecx
mov    $0x4,%esi
lea    0xd30(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%edi
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rcx
lea    -0x48(%rbp),%rdi
mov    $0x1c,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     1321 <_append_header+0x181>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   10c0 <_ar_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1328 <_append_header+0x188>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_append_header>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%rbp
mov    %rsi,%r13
mov    %rdi,%r14
mov    %rcx,%rdi
call   1080 <fileno@plt>
lea    0x20(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
je     11eb <_append_header+0x4b>
mov    %rbp,%rdi
mov    $0x1,%esi
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <_ar_error@plt>
mov    %rbp,%rdi
call   10a0 <strdup@plt>
test   %rax,%rax
je     11d0 <_append_header+0x30>
mov    %rax,%rbx
mov    %rsp,%r15
xor    %r12d,%r12d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1060 <memset@plt>
mov    0x18(%rsp),%ebp
mov    %rbx,%rdi
call   1070 <basename@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x14(%rsp),%edi
mov    0x40(%rsp),%ecx
lea    0xdbf(%rip),%rbp
mov    $0x4,%esi
mov    %rbp,%rdx
call   1050 <snprintf@plt>
mov    0x10(%rsp),%edi
mov    0x38(%rsp),%ecx
mov    $0x4,%esi
mov    %rbp,%rdx
call   1050 <snprintf@plt>
mov    0xc(%rsp),%edi
mov    0x30(%rsp),%ecx
mov    $0x4,%esi
mov    %rbp,%rdx
call   1050 <snprintf@plt>
mov    0x8(%rsp),%edi
mov    0x28(%rsp),%ecx
lea    0xd7c(%rip),%rdx
mov    $0x4,%esi
call   1050 <snprintf@plt>
mov    0x4(%rsp),%edi
mov    0x20(%rsp),%ecx
mov    $0x4,%esi
mov    %rbp,%rdx
call   1050 <snprintf@plt>
mov    (%rsp),%edi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1040 <strncpy@plt>
mov    %r15,%rdi
mov    $0x1c,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     12e6 <_append_header+0x146>
mov    %r14,%rdi
mov    $0x1,%esi
call   10c0 <_ar_error@plt>
mov    %eax,%r12d
mov    %r12d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ar_hdr = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.stat = type { i64, i64, i64, i64, i64 }
@.str = [3 x i8] c"%u\00"
@.str.1 = [3 x i8] c"%o\00"
@ARFMAG = global i32 0
define i32 @_append_header(i8* %0, i32* %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca %struct.ar_hdr
%11 = alloca %struct.stat
%12 = alloca i8*
store i8* %0, i8** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i32* %3, i32** %9
%13 = load i32*, i32** %9
%14 = call i32 @fileno(i32* %13)
%15 = call i64 @fstat(i32 %14, %struct.stat* %11)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i8*, i8** %8
%19 = call i32 @_ar_error(i8* %18, i32 1)
store i32 %19, i32* %5
br label %77
20: ; preds = %4
%21 = load i8*, i8** %8
%22 = call i8* @strdup(i8* %21)
store i8* %22, i8** %12
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i8*, i8** %8
%26 = call i32 @_ar_error(i8* %25, i32 1)
store i32 %26, i32* %5
br label %77
27: ; preds = %20
%28 = call i32 @memset(%struct.ar_hdr* %10, i32 0, i32 28)
%29 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %10, i32 0, i32 6
%30 = load i32, i32* %29
%31 = load i8*, i8** %12
%32 = call i32 @basename(i8* %31)
%33 = call i32 @strncpy(i32 %30, i32 %32, i32 3)
%34 = load i8*, i8** %12
%35 = call i32 @free(i8* %34)
%36 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %10, i32 0, i32 5
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 4
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i32
%41 = call i32 @snprintf(i32 %37, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %40)
%42 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %10, i32 0, i32 4
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 3
%45 = load i64, i64* %44
%46 = trunc i64 %45 to i32
%47 = call i32 @snprintf(i32 %43, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %46)
%48 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %10, i32 0, i32 3
%49 = load i32, i32* %48
%50 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 2
%51 = load i64, i64* %50
%52 = trunc i64 %51 to i32
%53 = call i32 @snprintf(i32 %49, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %52)
%54 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %10, i32 0, i32 2
%55 = load i32, i32* %54
%56 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 1
%57 = load i64, i64* %56
%58 = trunc i64 %57 to i32
%59 = call i32 @snprintf(i32 %55, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %58)
%60 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %10, i32 0, i32 1
%61 = load i32, i32* %60
%62 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%63 = load i64, i64* %62
%64 = trunc i64 %63 to i32
%65 = call i32 @snprintf(i32 %61, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %64)
%66 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %10, i32 0, i32 0
%67 = load i32, i32* %66
%68 = load i32, i32* @ARFMAG
%69 = call i32 @strncpy(i32 %67, i32 %68, i32 4)
%70 = load i32*, i32** %7
%71 = call i32 @fwrite(%struct.ar_hdr* %10, i32 28, i32 1, i32* %70)
%72 = icmp ne i32 %71, 1
br i1 %72, label %73, label %76
73: ; preds = %27
%74 = load i8*, i8** %6
%75 = call i32 @_ar_error(i8* %74, i32 1)
store i32 %75, i32* %5
br label %77
76: ; preds = %27
store i32 0, i32* %5
br label %77
77: ; preds = %76, %73, %24, %17
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
%struct.ar_hdr = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.stat = type { i64, i64, i64, i64, i64 }
@.str = [3 x i8] c"%u\00"
@.str.1 = [3 x i8] c"%o\00"
@ARFMAG = global i32 0
define i32 @_append_header(i8* %0, i32* nocapture %1, i8* %2, i32* nocapture %3) {
%5 = alloca %struct.ar_hdr
%6 = alloca %struct.stat
%7 = bitcast %struct.ar_hdr* %5 to i8*
%8 = bitcast %struct.stat* %6 to i8*
%9 = tail call i32 @fileno(i32* %3)
%10 = call i64 @fstat(i32 %9, %struct.stat* %6)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %4
%13 = tail call i32 @_ar_error(i8* %2, i32 1)
br label %64
14: ; preds = %4
%15 = tail call i8* @strdup(i8* %2)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @_ar_error(i8* %2, i32 1)
br label %64
19: ; preds = %14
%20 = call i32 @memset(%struct.ar_hdr* %5, i32 0, i32 28)
%21 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %5, i64 0, i32 6
%22 = load i32, i32* %21
%23 = call i32 @basename(i8* %15)
%24 = call i32 @strncpy(i32 %22, i32 %23, i32 3)
%25 = call i32 @free(i8* %15)
%26 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %5, i64 0, i32 5
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 4
%29 = load i64, i64* %28
%30 = trunc i64 %29 to i32
%31 = call i32 @snprintf(i32 %27, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %5, i64 0, i32 4
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 3
%35 = load i64, i64* %34
%36 = trunc i64 %35 to i32
%37 = call i32 @snprintf(i32 %33, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %5, i64 0, i32 3
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 2
%41 = load i64, i64* %40
%42 = trunc i64 %41 to i32
%43 = call i32 @snprintf(i32 %39, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %5, i64 0, i32 2
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 1
%47 = load i64, i64* %46
%48 = trunc i64 %47 to i32
%49 = call i32 @snprintf(i32 %45, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %5, i64 0, i32 1
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%53 = load i64, i64* %52
%54 = trunc i64 %53 to i32
%55 = call i32 @snprintf(i32 %51, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %54)
%56 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %5, i64 0, i32 0
%57 = load i32, i32* %56
%58 = load i32, i32* @ARFMAG
%59 = call i32 @strncpy(i32 %57, i32 %58, i32 4)
%60 = call i32 @fwrite(%struct.ar_hdr* %5, i32 28, i32 1, i32* %1)
%61 = icmp eq i32 %60, 1
br i1 %61, label %64, label %62
62: ; preds = %19
%63 = call i32 @_ar_error(i8* %0, i32 1)
br label %64
64: ; preds = %19, %62, %17, %12
%65 = phi i32 [ %13, %12 ], [ %18, %17 ], [ %63, %62 ], [ 0, %19 ]
ret i32 %65
}