#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 */
typedef int uint64_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ pad(int, int);
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char **, scalar_t__);
int /*<<< orphan*/ wtotal(int);
uint64_t write_file(int fd, uint64_t block_size, const char *path) {
  int in;
  ssize_t rb;
  ssize_t rv;
  long len = 0;
  uint64_t total = 0;
  char buf[block_size];
  in = open(path, O_RDONLY);
  if (in == -1) {
    fprintf(stderr, "Error reading '%s'\n", path);
    return 0;
  }
  printf("Adding file %s\n", path);
  while ((rb = read(in, buf, block_size)) > 0) {
    total += rb;
    rv = write(fd, &buf, rb);
  }
  wtotal(total);
  close(in);
  if ((total % block_size) > 0) {
    pad(fd, block_size - (total % block_size));
  }
  return total;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1212 <write_file+0x92>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe06(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12a7 <write_file+0x127>
mov    -0x18(%rbp),%rsi
lea    0xdf7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x60(%rbp),%rsi
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%edx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jle    1266 <write_file+0xe6>
mov    -0x60(%rbp),%rsi
mov    -0x28(%rbp),%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
jmp    1222 <write_file+0xa2>
mov    -0x3c(%rbp),%edi
call   10a0 <wtotal@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x3c(%rbp),%eax
cltd
idivl  -0xc(%rbp)
cmp    $0x0,%edx
jle    129a <write_file+0x11a>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x3c(%rbp),%eax
cltd
idivl  -0xc(%rbp)
sub    %edx,%esi
call   1080 <pad@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %edi,%r14d
mov    %esi,%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %rdx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     123c <write_file+0xbc>
mov    %eax,%r12d
lea    0xe4a(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
xor    %ebx,%ebx
test   %rax,%rax
jle    1213 <write_file+0x93>
nopw   0x0(%rax,%rax,1)
add    %eax,%ebx
mov    %r14d,%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
test   %rax,%rax
jg     11f0 <write_file+0x70>
mov    %ebx,%edi
call   10a0 <wtotal@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %ebx,%eax
cltd
idiv   %r15d
test   %edx,%edx
jle    1256 <write_file+0xd6>
sub    %edx,%r15d
mov    %r14d,%edi
mov    %r15d,%esi
call   1080 <pad@plt>
jmp    1256 <write_file+0xd6>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Error reading '%s'\0A\00"
@.str.1 = [16 x i8] c"Adding file %s\0A\00"
define i32 @write_file(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i64 0, i64* %11
store i32 0, i32* %12
%16 = load i32, i32* %6
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %13
%19 = alloca i8, i64 %17
store i64 %17, i64* %14
%20 = load i8*, i8** %7
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %3
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %7
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 0, i32* %4
store i32 1, i32* %15
br label %66
29: ; preds = %3
%30 = load i8*, i8** %7
%31 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %37, %29
%33 = load i32, i32* %8
%34 = load i32, i32* %6
%35 = call i64 @read(i32 %33, i8* %19, i32 %34)
store i64 %35, i64* %9
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %32
%38 = load i64, i64* %9
%39 = load i32, i32* %12
%40 = sext i32 %39 to i64
%41 = add nsw i64 %40, %38
%42 = trunc i64 %41 to i32
store i32 %42, i32* %12
%43 = load i32, i32* %5
%44 = bitcast i8* %19 to i8**
%45 = load i64, i64* %9
%46 = call i64 @write(i32 %43, i8** %44, i64 %45)
store i64 %46, i64* %10
br label %32
47: ; preds = %32
%48 = load i32, i32* %12
%49 = call i32 @wtotal(i32 %48)
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %12
%53 = load i32, i32* %6
%54 = srem i32 %52, %53
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %47
%57 = load i32, i32* %5
%58 = load i32, i32* %6
%59 = load i32, i32* %12
%60 = load i32, i32* %6
%61 = srem i32 %59, %60
%62 = sub nsw i32 %58, %61
%63 = call i32 @pad(i32 %57, i32 %62)
br label %64
64: ; preds = %56, %47
%65 = load i32, i32* %12
store i32 %65, i32* %4
store i32 1, i32* %15
br label %66
66: ; preds = %64, %25
%67 = load i8*, i8** %13
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Error reading '%s'\0A\00"
@.str.1 = [16 x i8] c"Adding file %s\0A\00"
define i32 @write_file(i32 %0, i32 %1, i8* %2) {
%4 = zext i32 %1 to i64
%5 = alloca i8, i64 %4
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %2, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %35
12: ; preds = %3
%13 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%14 = call i64 @read(i32 %7, i8* %5, i32 %1)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = bitcast i8* %5 to i8**
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ %14, %16 ], [ %24, %18 ]
%20 = phi i32 [ 0, %16 ], [ %22, %18 ]
%21 = trunc i64 %19 to i32
%22 = add i32 %20, %21
%23 = call i64 @write(i32 %0, i8** %17, i64 %19)
%24 = call i64 @read(i32 %7, i8* %5, i32 %1)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %18, label %26
26: ; preds = %18, %12
%27 = phi i32 [ 0, %12 ], [ %22, %18 ]
%28 = tail call i32 @wtotal(i32 %27)
%29 = tail call i32 @close(i32 %7)
%30 = srem i32 %27, %1
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = sub nsw i32 %1, %30
%34 = tail call i32 @pad(i32 %0, i32 %33)
br label %35
35: ; preds = %26, %32, %9
%36 = phi i32 [ 0, %9 ], [ %27, %32 ], [ %27, %26 ]
ret i32 %36
}