#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 ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int smf_core_copy_to_fd(const char *source, int dest) {
  char buf[512];
  int in_fd;
  ssize_t nread;
  size_t nbytes = 0;
  if ((in_fd = open(source, O_RDONLY)) == -1)
    return -1;
  while ((nread = read(in_fd, buf, sizeof(buf))) != 0) {
    ssize_t nwritten = 0;
    if (nread == -1) {
      close(in_fd);
      return -1;
    }
    while (nwritten < nread) {
      ssize_t n;
      if ((n = write(dest, buf, nread)) == -1) {
        close(in_fd);
        return -1;
      }
      nwritten += n;
      nbytes += n;
    }
  }
  close(in_fd);
  return nbytes;
}


x86 assembly (clang, -O0):
<smf_core_copy_to_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x230(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x224(%rbp)
cmp    $0xffffffff,%eax
jne    118a <smf_core_copy_to_fd+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <smf_core_copy_to_fd+0x13b>
jmp    118f <smf_core_copy_to_fd+0x4f>
mov    -0x224(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x228(%rbp)
cmp    $0x0,%eax
je     1266 <smf_core_copy_to_fd+0x126>
movl   $0x0,-0x234(%rbp)
cmpl   $0xffffffff,-0x228(%rbp)
jne    11e3 <smf_core_copy_to_fd+0xa3>
mov    -0x224(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <smf_core_copy_to_fd+0x13b>
jmp    11e8 <smf_core_copy_to_fd+0xa8>
mov    -0x234(%rbp),%eax
cmp    -0x228(%rbp),%eax
jge    1261 <smf_core_copy_to_fd+0x121>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x228(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x238(%rbp)
cmp    $0xffffffff,%eax
jne    1235 <smf_core_copy_to_fd+0xf5>
mov    -0x224(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <smf_core_copy_to_fd+0x13b>
mov    -0x238(%rbp),%eax
add    -0x234(%rbp),%eax
mov    %eax,-0x234(%rbp)
movslq -0x238(%rbp),%rax
add    -0x230(%rbp),%rax
mov    %rax,-0x230(%rbp)
jmp    11e8 <smf_core_copy_to_fd+0xa8>
jmp    118f <smf_core_copy_to_fd+0x4f>
mov    -0x224(%rbp),%edi
call   1040 <close@plt>
mov    -0x230(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smf_core_copy_to_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %esi,%r15d
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%r13d
mov    $0xffffffff,%eax
cmp    $0xffffffff,%r13d
je     11da <smf_core_copy_to_fd+0x9a>
xor    %ebx,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11c1 <smf_core_copy_to_fd+0x81>
mov    %eax,%ebp
test   %eax,%eax
je     11d0 <smf_core_copy_to_fd+0x90>
jle    1180 <smf_core_copy_to_fd+0x40>
xor    %r14d,%r14d
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11c1 <smf_core_copy_to_fd+0x81>
add    %eax,%r14d
mov    %eax,%eax
add    %rax,%rbx
cmp    %ebp,%r14d
jl     11a0 <smf_core_copy_to_fd+0x60>
jmp    1180 <smf_core_copy_to_fd+0x40>
mov    %r13d,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
jmp    11da <smf_core_copy_to_fd+0x9a>
mov    %r13d,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @smf_core_copy_to_fd(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 0, i64* %9
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %57
17: ; preds = %2
br label %18
18: ; preds = %51, %17
%19 = load i32, i32* %7
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 512)
store i32 %21, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %52
23: ; preds = %18
store i32 0, i32* %10
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %7
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %3
br label %57
29: ; preds = %23
br label %30
30: ; preds = %43, %29
%31 = load i32, i32* %10
%32 = load i32, i32* %8
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %51
34: ; preds = %30
%35 = load i32, i32* %5
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%37 = load i32, i32* %8
%38 = call i32 @write(i32 %35, i8* %36, i32 %37)
store i32 %38, i32* %11
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %3
br label %57
43: ; preds = %34
%44 = load i32, i32* %11
%45 = load i32, i32* %10
%46 = add nsw i32 %45, %44
store i32 %46, i32* %10
%47 = load i32, i32* %11
%48 = sext i32 %47 to i64
%49 = load i64, i64* %9
%50 = add i64 %49, %48
store i64 %50, i64* %9
br label %30
51: ; preds = %30
br label %18
52: ; preds = %18
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
%55 = load i64, i64* %9
%56 = trunc i64 %55 to i32
store i32 %56, i32* %3
br label %57
57: ; preds = %52, %40, %26, %16
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @smf_core_copy_to_fd(i8* nocapture %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %31, label %8
8: ; preds = %23, %2
%9 = phi i64 [ 0, %2 ], [ %26, %23 ]
br label %10
10: ; preds = %8, %12
%11 = call i32 @read(i32 %6, i8* %4, i32 512)
switch i32 %11, label %12 [
i32 0, label %28
i32 -1, label %14
]
12: ; preds = %10
%13 = icmp sgt i32 %11, 0
br i1 %13, label %16, label %10
14: ; preds = %10
%15 = tail call i32 @close(i32 %6)
br label %31
16: ; preds = %12, %23
%17 = phi i32 [ %24, %23 ], [ 0, %12 ]
%18 = phi i64 [ %26, %23 ], [ %9, %12 ]
%19 = call i32 @write(i32 %1, i8* %4, i32 %11)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @close(i32 %6)
br label %31
23: ; preds = %16
%24 = add nsw i32 %19, %17
%25 = zext i32 %19 to i64
%26 = add i64 %18, %25
%27 = icmp slt i32 %24, %11
br i1 %27, label %16, label %8
28: ; preds = %10
%29 = tail call i32 @close(i32 %6)
%30 = trunc i64 %9 to i32
br label %31
31: ; preds = %21, %14, %2, %28
%32 = phi i32 [ %30, %28 ], [ -1, %2 ], [ -1, %14 ], [ -1, %21 ]
ret i32 %32
}