#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int /*<<< orphan*/ send(int, unsigned char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
void sendbinfiletosock(int sock, char *filename) {
  FILE *fd;
  unsigned char *buff;
  size_t bytesread;
  int buff_len = 1024 * 1024;
  buff = malloc(buff_len);
  if (buff == NULL) {
    syslog(LOG_WARNING,
           "ERROR: Out of memory while trying to allocate buffer for file");
    return;
  }
  fd = fopen(filename, "rb");
  if (fd == NULL) {
    syslog(LOG_WARNING, "ERROR: File '%s' could not be opened", filename);
    free(buff);
    return;
  }
  for (;;) {
    bytesread = fread(buff, 1, buff_len, fd);
    if (bytesread <= 0)
      break;
    send(sock, buff, bytesread, 0);
  }
  free(buff);
  fclose(fd);
}


x86 assembly (clang, -O0):
<sendbinfiletosock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x100000,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b9 <sendbinfiletosock+0x49>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
jmp    1258 <sendbinfiletosock+0xe8>
mov    -0x10(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1201 <sendbinfiletosock+0x91>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1258 <sendbinfiletosock+0xe8>
jmp    1206 <sendbinfiletosock+0x96>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
ja     122f <sendbinfiletosock+0xbf>
jmp    1246 <sendbinfiletosock+0xd6>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    1206 <sendbinfiletosock+0x96>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendbinfiletosock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
mov    $0x100000,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f6 <sendbinfiletosock+0x86>
mov    %rax,%rbx
lea    0xea8(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1217 <sendbinfiletosock+0xa7>
mov    %rax,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x100000,%edx
mov    %rbp,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11dc <sendbinfiletosock+0x6c>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    11b0 <sendbinfiletosock+0x40>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <syslog@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@LOG_WARNING = global i32 0
@.str = [62 x i8] c"ERROR: Out of memory while trying to allocate buffer for file\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [37 x i8] c"ERROR: File '%s' could not be opened\00"
define void @sendbinfiletosock(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 1048576, i32* %8
%9 = load i32, i32* %8
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @LOG_WARNING
%15 = call i32 (i32, i8*, ...) @syslog(i32 %14, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
br label %46
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %5
%19 = load i32*, i32** %5
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i32, i32* @LOG_WARNING
%23 = load i8*, i8** %4
%24 = call i32 (i32, i8*, ...) @syslog(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
br label %46
27: ; preds = %16
br label %28
28: ; preds = %36, %27
%29 = load i8*, i8** %6
%30 = load i32, i32* %8
%31 = load i32*, i32** %5
%32 = call i64 @fread(i8* %29, i32 1, i32 %30, i32* %31)
store i64 %32, i64* %7
%33 = load i64, i64* %7
%34 = icmp ule i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %28
br label %41
36: ; preds = %28
%37 = load i32, i32* %3
%38 = load i8*, i8** %6
%39 = load i64, i64* %7
%40 = call i32 @send(i32 %37, i8* %38, i64 %39, i32 0)
br label %28
41: ; preds = %35
%42 = load i8*, i8** %6
%43 = call i32 @free(i8* %42)
%44 = load i32*, i32** %5
%45 = call i32 @fclose(i32* %44)
br label %46
46: ; preds = %41, %21, %13
ret void
}

LLVM IR (-O2):
@LOG_WARNING = global i32 0
@.str = [62 x i8] c"ERROR: Out of memory while trying to allocate buffer for file\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [37 x i8] c"ERROR: File '%s' could not be opened\00"
define void @sendbinfiletosock(i32 %0, i8* %1) {
%3 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @LOG_WARNING
%7 = tail call i32 (i32, i8*, ...) @syslog(i32 %6, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
br label %26
8: ; preds = %2
%9 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i64 @fread(i8* %3, i32 1, i32 1048576, i32* %9)
%13 = icmp eq i64 %12, 0
br i1 %13, label %23, label %18
14: ; preds = %8
%15 = load i32, i32* @LOG_WARNING
%16 = tail call i32 (i32, i8*, ...) @syslog(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%17 = tail call i32 @free(i8* %3)
br label %26
18: ; preds = %11, %18
%19 = phi i64 [ %21, %18 ], [ %12, %11 ]
%20 = tail call i32 @send(i32 %0, i8* %3, i64 %19, i32 0)
%21 = tail call i64 @fread(i8* %3, i32 1, i32 1048576, i32* %9)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %18
23: ; preds = %18, %11
%24 = tail call i32 @free(i8* %3)
%25 = tail call i32 @fclose(i32* %9)
br label %26
26: ; preds = %23, %14, %5
ret void
}