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

/* Variables and functions */
int PIPE_BUF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, size_t);
int /*<<< orphan*/ printf(char *);
size_t send(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
size_t strlen(char *);
int send_file(int data_socket_id, char *from_file_name, char *to_file_name) {
  ssize_t message_size, written_size;
  size_t buffer_size = PIPE_BUF + 1;
  char file_buffer[buffer_size];
  FILE *file_id;
  size_t send_length;
  send_length = strlen(to_file_name);
  sprintf(file_buffer, "%d", (int)send_length);
  if (send(data_socket_id, file_buffer, strlen(file_buffer), 0) !=
      strlen(file_buffer)) {
    printf("send of file name length failed\n");
    return -1;
  }
  if (send(data_socket_id, to_file_name, send_length, 0) != send_length) {
    printf("send of file name failed\n");
    return -1;
  }
  file_id = fopen(from_file_name, "r");
  send_length = fread(file_buffer, 1, buffer_size - 1, file_id);
  while (send_length > 0) {
    if (send(data_socket_id, file_buffer, send_length, 0) != send_length) {
      printf("send of file data failed\n");
      fclose(file_id);
      return -1;
    }
    memset(file_buffer, '\0', buffer_size);
    send_length = fread(file_buffer, 1, buffer_size - 1, file_id);
  }
  fclose(file_id);
  return 0;
}


x86 assembly (clang, -O0):
<send_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%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,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %eax,%edx
lea    0xe16(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%eax
mov    %eax,-0x64(%rbp)
call   1050 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,-0x58(%rbp)
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
cmp    %rcx,%rax
je     124b <send_file+0xcb>
lea    0xdd0(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1350 <send_file+0x1d0>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    -0x48(%rbp),%rax
je     1286 <send_file+0x106>
lea    0xdb6(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1350 <send_file+0x1d0>
mov    -0x10(%rbp),%rdi
lea    0xdad(%rip),%rsi
call   1090 <fopen@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdx
sub    $0x1,%rdx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jbe    1339 <send_file+0x1b9>
mov    -0x60(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    -0x48(%rbp),%rax
je     1307 <send_file+0x187>
lea    0xd5a(%rip),%rdi
call   1070 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1350 <send_file+0x1d0>
mov    -0x60(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1080 <memset@plt>
mov    -0x60(%rbp),%rdi
mov    -0x28(%rbp),%rdx
sub    $0x1,%rdx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x48(%rbp)
jmp    12b8 <send_file+0x138>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x38(%rbp)
mov    %edi,%r13d
mov    0x2e41(%rip),%rax
movslq (%rax),%r15
mov    %rsp,%rbx
lea    0x10(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rdx,-0x30(%rbp)
mov    %rdx,%rdi
call   1060 <strlen@plt>
mov    %rax,%r12
lea    0xe37(%rip),%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <strlen@plt>
cmp    %rax,%r14
jne    12a9 <send_file+0x129>
mov    %r13d,%edi
mov    -0x30(%rbp),%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    %r12,%rax
jne    12b2 <send_file+0x132>
mov    %r13d,-0x30(%rbp)
add    $0x1,%r15
lea    0xdda(%rip),%rsi
mov    -0x38(%rbp),%rdi
call   1090 <fopen@plt>
mov    %rax,%r12
lea    -0x1(%r15),%r13
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r13,%rdx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     129d <send_file+0x11d>
mov    %rax,%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    -0x30(%rbp),%edi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    %r14,%rax
jne    12d2 <send_file+0x152>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1080 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r13,%rdx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1260 <send_file+0xe0>
mov    %r12,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    12c3 <send_file+0x143>
lea    0xd87(%rip),%rdi
jmp    12b9 <send_file+0x139>
lea    0xd65(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2c(%rip),%rdi
call   1030 <puts@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
jmp    12be <send_file+0x13e>

LLVM IR (-O0):
@PIPE_BUF = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [33 x i8] c"send of file name length failed\0A\00"
@.str.2 = [26 x i8] c"send of file name failed\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [26 x i8] c"send of file data failed\0A\00"
define i32 @send_file(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i64
%15 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%16 = load i32, i32* @PIPE_BUF
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
store i64 %18, i64* %10
%19 = load i64, i64* %10
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %11
%21 = alloca i8, i64 %19
store i64 %19, i64* %12
%22 = load i8*, i8** %7
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %14
%24 = load i64, i64* %14
%25 = trunc i64 %24 to i32
%26 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %5
%28 = call i64 @strlen(i8* %21)
%29 = call i64 @send(i32 %27, i8* %21, i64 %28, i32 0)
%30 = call i64 @strlen(i8* %21)
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %3
%33 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
store i32 1, i32* %15
br label %73
34: ; preds = %3
%35 = load i32, i32* %5
%36 = load i8*, i8** %7
%37 = load i64, i64* %14
%38 = call i64 @send(i32 %35, i8* %36, i64 %37, i32 0)
%39 = load i64, i64* %14
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
store i32 1, i32* %15
br label %73
43: ; preds = %34
%44 = load i8*, i8** %6
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %45, i32** %13
%46 = load i64, i64* %10
%47 = sub i64 %46, 1
%48 = load i32*, i32** %13
%49 = call i64 @fread(i8* %21, i32 1, i64 %47, i32* %48)
store i64 %49, i64* %14
br label %50
50: ; preds = %63, %43
%51 = load i64, i64* %14
%52 = icmp ugt i64 %51, 0
br i1 %52, label %53, label %70
53: ; preds = %50
%54 = load i32, i32* %5
%55 = load i64, i64* %14
%56 = call i64 @send(i32 %54, i8* %21, i64 %55, i32 0)
%57 = load i64, i64* %14
%58 = icmp ne i64 %56, %57
br i1 %58, label %59, label %63
59: ; preds = %53
%60 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%61 = load i32*, i32** %13
%62 = call i32 @fclose(i32* %61)
store i32 -1, i32* %4
store i32 1, i32* %15
br label %73
63: ; preds = %53
%64 = load i64, i64* %10
%65 = call i32 @memset(i8* %21, i8 signext 0, i64 %64)
%66 = load i64, i64* %10
%67 = sub i64 %66, 1
%68 = load i32*, i32** %13
%69 = call i64 @fread(i8* %21, i32 1, i64 %67, i32* %68)
store i64 %69, i64* %14
br label %50
70: ; preds = %50
%71 = load i32*, i32** %13
%72 = call i32 @fclose(i32* %71)
store i32 0, i32* %4
store i32 1, i32* %15
br label %73
73: ; preds = %70, %59, %41, %32
%74 = load i8*, i8** %11
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@PIPE_BUF = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.3 = [2 x i8] c"r\00"
@str = [25 x i8] c"send of file data failed\00"
@str.5 = [25 x i8] c"send of file name failed\00"
@str.6 = [32 x i8] c"send of file name length failed\00"
define i32 @send_file(i32 %0, i8* nocapture %1, i8* %2) {
%4 = load i32, i32* @PIPE_BUF
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%9 = trunc i64 %8 to i32
%10 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i64 @strlen(i8* %7)
%12 = call i64 @send(i32 %0, i8* %7, i64 %11, i32 0)
%13 = call i64 @strlen(i8* %7)
%14 = icmp eq i64 %12, %13
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
br label %40
17: ; preds = %3
%18 = call i64 @send(i32 %0, i8* %2, i64 %8, i32 0)
%19 = icmp eq i64 %18, %8
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
br label %40
22: ; preds = %17
%23 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = add nsw i64 %6, -1
%25 = call i64 @fread(i8* %7, i32 1, i64 %24, i32* %23)
%26 = icmp eq i64 %25, 0
br i1 %26, label %38, label %27
27: ; preds = %22, %34
%28 = phi i64 [ %36, %34 ], [ %25, %22 ]
%29 = call i64 @send(i32 %0, i8* %7, i64 %28, i32 0)
%30 = icmp eq i64 %29, %28
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%33 = call i32 @fclose(i32* %23)
br label %40
34: ; preds = %27
%35 = call i32 @memset(i8* %7, i8 signext 0, i64 %6)
%36 = call i64 @fread(i8* %7, i32 1, i64 %24, i32* %23)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %27
38: ; preds = %34, %22
%39 = call i32 @fclose(i32* %23)
br label %40
40: ; preds = %38, %31, %20, %15
%41 = phi i32 [ -1, %15 ], [ -1, %20 ], [ -1, %31 ], [ 0, %38 ]
ret i32 %41
}