#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 ar_hdr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, struct ar_hdr *, int);
int write_file(int, int, struct ar_hdr, char *);
int copy_file(int new_fd, int old_fd, struct ar_hdr header, char *file_name) {
  int total_written = 0, num_written = 0;
  total_written = write(new_fd, &header, sizeof(struct ar_hdr));
  if (total_written == -1) {
    perror("Error write header when making new file");
    exit(-1);
  }
  num_written = write_file(old_fd, new_fd, header, file_name);
  if (num_written == 0)
    return 0;
  else
    total_written += num_written;
  return total_written;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edx,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1197 <copy_file+0x57>
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%edx
call   1040 <write_file@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11c2 <copy_file+0x82>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <copy_file+0x91>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %edx,%r12d
mov    %esi,%r15d
mov    %edi,%ebx
mov    %edx,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1195 <copy_file+0x55>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %ebx,%esi
mov    %r12d,%edx
mov    %r14,%rcx
call   1040 <write_file@plt>
add    %eax,%ebp
test   %eax,%eax
cmovne %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe64(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.ar_hdr = type { i32 }
@.str = [40 x i8] c"Error write header when making new file\00"
define i32 @copy_file(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.ar_hdr
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %6, i32 0, i32 0
store i32 %2, i32* %12
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %10
store i32 0, i32* %11
%13 = load i32, i32* %7
%14 = call i32 @write(i32 %13, %struct.ar_hdr* %6, i32 4)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %4
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = load i8*, i8** %9
%24 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @write_file(i32 %21, i32 %22, i32 %25, i8* %23)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %20
store i32 0, i32* %5
br label %36
30: ; preds = %20
%31 = load i32, i32* %11
%32 = load i32, i32* %10
%33 = add nsw i32 %32, %31
store i32 %33, i32* %10
br label %34
34: ; preds = %30
%35 = load i32, i32* %10
store i32 %35, i32* %5
br label %36
36: ; preds = %34, %29
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
%struct.ar_hdr = type { i32 }
@.str = [40 x i8] c"Error write header when making new file\00"
define i32 @copy_file(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca %struct.ar_hdr
%6 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %5, i64 0, i32 0
store i32 %2, i32* %6
%7 = call i32 @write(i32 %0, %struct.ar_hdr* %5, i32 4)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %4
%13 = tail call i32 @write_file(i32 %1, i32 %0, i32 %2, i8* %3)
%14 = icmp eq i32 %13, 0
%15 = add nsw i32 %13, %7
%16 = select i1 %14, i32 0, i32 %15
ret i32 %16
}