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

/* Variables and functions */
scalar_t__ check_byte_alignment(int, int, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int);
int write_contents(int ar_fd, int in_fd, struct stat st, char *file_name) {
  int total_read = 0, total_written = 0, num_written = 0, num_read = 0;
  char buf[1];
  while (total_read < st.st_size) {
    num_read = read(in_fd, buf, 1);
    num_written = write(ar_fd, buf, 1);
    if (num_read != num_written) {
      perror("Error writing file");
      unlink(file_name);
      exit(-1);
    }
    total_read += num_read;
    total_written += num_written;
  }
  total_written += check_byte_alignment(ar_fd, total_written, file_name);
  return total_written;
}


x86 assembly (clang, -O0):
<write_contents>:
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)
movl   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1207 <write_contents+0xa7>
mov    -0x10(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     11f0 <write_contents+0x90>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1191 <write_contents+0x31>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <check_byte_alignment@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_contents>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
mov    %edi,0xc(%rsp)
test   %edx,%edx
jle    11c6 <write_contents+0x66>
mov    %edx,%r13d
mov    %esi,%r12d
xor    %ebp,%ebp
lea    0xb(%rsp),%rbx
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%r14d
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    %eax,%r14d
jne    11e9 <write_contents+0x89>
add    %r14d,%r15d
add    %r14d,%ebp
cmp    %r13d,%r15d
jl     1190 <write_contents+0x30>
jmp    11c8 <write_contents+0x68>
xor    %ebp,%ebp
mov    0xc(%rsp),%edi
mov    %ebp,%esi
mov    0x10(%rsp),%rdx
call   1050 <check_byte_alignment@plt>
add    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
mov    0x10(%rsp),%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

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

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