#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 /*<<< orphan*/ ar_size;
};

/* Variables and functions */
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int);
int write_file(int in_fd, int out_fd, struct ar_hdr header, char *file_name) {
  int num_read = 0, num_written = 0, copied = 0;
  int size;
  char buffer[1];
  size = atoi(header.ar_size);
  ;
  while (copied < size) {
    num_read = read(in_fd, buffer, 1);
    if (num_read == 0)
      return 0;
    num_written = write(out_fd, buffer, 1);
    if (num_read != num_written) {
      perror("Error writing file");
      unlink(file_name);
      exit(-1);
    }
    copied += num_written;
  }
  return copied;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1218 <write_file+0xb8>
mov    -0xc(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11cb <write_file+0x6b>
movl   $0x0,-0x4(%rbp)
jmp    121e <write_file+0xbe>
mov    -0x10(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     120a <write_file+0xaa>
lea    0xe0e(%rip),%rdi
call   1060 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1195 <write_file+0x35>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %esi,%r15d
mov    %edi,%r12d
mov    %edx,%edi
call   1070 <atoi@plt>
test   %eax,%eax
jle    11c4 <write_file+0x64>
mov    %eax,%ebp
mov    %rbx,0x10(%rsp)
lea    0xf(%rsp),%r13
xor    %ebx,%ebx
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11c4 <write_file+0x64>
mov    %eax,%r14d
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    %eax,%r14d
jne    11d7 <write_file+0x77>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1190 <write_file+0x30>
jmp    11c6 <write_file+0x66>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.ar_hdr = type { i32 }
@.str = [19 x i8] c"Error writing file\00"
define i32 @write_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 = alloca i32
%13 = alloca i32
%14 = alloca [1 x i8]
%15 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %6, i32 0, i32 0
store i32 %2, i32* %15
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
store i32 0, i32* %12
%16 = getelementptr inbounds %struct.ar_hdr, %struct.ar_hdr* %6, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @atoi(i32 %17)
store i32 %18, i32* %13
br label %19
19: ; preds = %42, %4
%20 = load i32, i32* %12
%21 = load i32, i32* %13
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %46
23: ; preds = %19
%24 = load i32, i32* %7
%25 = getelementptr inbounds [1 x i8], [1 x i8]* %14, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 1)
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 0, i32* %5
br label %48
30: ; preds = %23
%31 = load i32, i32* %8
%32 = getelementptr inbounds [1 x i8], [1 x i8]* %14, i64 0, i64 0
%33 = call i32 @write(i32 %31, i8* %32, i32 1)
store i32 %33, i32* %11
%34 = load i32, i32* %10
%35 = load i32, i32* %11
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%39 = load i8*, i8** %9
%40 = call i32 @unlink(i8* %39)
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %30
%43 = load i32, i32* %11
%44 = load i32, i32* %12
%45 = add nsw i32 %44, %43
store i32 %45, i32* %12
br label %19
46: ; preds = %19
%47 = load i32, i32* %12
store i32 %47, i32* %5
br label %48
48: ; preds = %46, %29
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@.str = [19 x i8] c"Error writing file\00"
define i32 @write_file(i32 %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = alloca [1 x i8]
%6 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%7 = tail call i32 @atoi(i32 %2)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %11, label %23
9: ; preds = %15
%10 = icmp slt i32 %18, %7
br i1 %10, label %11, label %23
11: ; preds = %4, %9
%12 = phi i32 [ %18, %9 ], [ 0, %4 ]
%13 = call i32 @read(i32 %0, i8* %6, i32 1)
%14 = icmp eq i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %11
%16 = call i32 @write(i32 %1, i8* %6, i32 1)
%17 = icmp eq i32 %13, %16
%18 = add nsw i32 %16, %12
br i1 %17, label %9, label %19
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @unlink(i8* %3)
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %11, %9, %4
%24 = phi i32 [ 0, %4 ], [ %18, %9 ], [ 0, %11 ]
ret i32 %24
}