#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*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ sendline(int, char *);
scalar_t__ sockreadline(int /*<<< orphan*/, char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
void sendtxtfiletosock(int sock, char *filename) {
  FILE *fd;
  char *linebuff;
  int linebuff_len = 1024 * 1024;
  linebuff = malloc(linebuff_len);
  if (linebuff == 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(linebuff);
    return;
  }
  for (;;) {
    if (sockreadline(fileno(fd), linebuff, linebuff_len - 1, NULL) < 0)
      break;
    if ((linebuff[0] == '.') && (linebuff[1] == 0))
      sprintf(linebuff, ". ");
    sendline(sock, linebuff);
  }
  fclose(fd);
  free(linebuff);
}


x86 assembly (clang, -O0):
<sendtxtfiletosock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x100000,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d9 <sendtxtfiletosock+0x49>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
jmp    12a7 <sendtxtfiletosock+0x117>
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1221 <sendtxtfiletosock+0x91>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12a7 <sendtxtfiletosock+0x117>
jmp    1226 <sendtxtfiletosock+0x96>
mov    -0x18(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
sub    $0x1,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   10b0 <sockreadline@plt>
cmp    $0x0,%rax
jge    1253 <sendtxtfiletosock+0xc3>
jmp    1295 <sendtxtfiletosock+0x105>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1284 <sendtxtfiletosock+0xf4>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1284 <sendtxtfiletosock+0xf4>
mov    -0x20(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1040 <sendline@plt>
jmp    1226 <sendtxtfiletosock+0x96>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendtxtfiletosock>:
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     1209 <sendtxtfiletosock+0x89>
mov    %rax,%rbx
lea    0xe98(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     122a <sendtxtfiletosock+0xaa>
mov    %rax,%rbp
jmp    11cb <sendtxtfiletosock+0x4b>
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <sendline@plt>
mov    %rbp,%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0xfffff,%edx
xor    %ecx,%ecx
call   10a0 <sockreadline@plt>
test   %rax,%rax
js     11ff <sendtxtfiletosock+0x7f>
cmpb   $0x2e,(%rbx)
jne    11c0 <sendtxtfiletosock+0x40>
cmpb   $0x0,0x1(%rbx)
jne    11c0 <sendtxtfiletosock+0x40>
movb   $0x0,0x2(%rbx)
movw   $0x202e,(%rbx)
jmp    11c0 <sendtxtfiletosock+0x40>
mov    %rbp,%rdi
call   1050 <fclose@plt>
jmp    1244 <sendtxtfiletosock+0xc4>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <syslog@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%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"
@.str.3 = [3 x i8] c". \00"
define void @sendtxtfiletosock(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 1048576, i32* %7
%8 = load i32, i32* %7
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @LOG_WARNING
%14 = call i32 (i32, i8*, ...) @syslog(i32 %13, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
br label %60
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %5
%18 = load i32*, i32** %5
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i32, i32* @LOG_WARNING
%22 = load i8*, i8** %4
%23 = call i32 (i32, i8*, ...) @syslog(i32 %21, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %6
%25 = call i32 @free(i8* %24)
br label %60
26: ; preds = %15
br label %27
27: ; preds = %51, %26
%28 = load i32*, i32** %5
%29 = call i32 @fileno(i32* %28)
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = sub nsw i32 %31, 1
%33 = call i64 @sockreadline(i32 %29, i8* %30, i32 %32, i32* null)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %27
br label %55
36: ; preds = %27
%37 = load i8*, i8** %6
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 46
br i1 %41, label %42, label %51
42: ; preds = %36
%43 = load i8*, i8** %6
%44 = getelementptr inbounds i8, i8* %43, i64 1
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = load i8*, i8** %6
%50 = call i32 @sprintf(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %48, %42, %36
%52 = load i32, i32* %3
%53 = load i8*, i8** %6
%54 = call i32 @sendline(i32 %52, i8* %53)
br label %27
55: ; preds = %35
%56 = load i32*, i32** %5
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %6
%59 = call i32 @free(i8* %58)
br label %60
60: ; preds = %55, %20, %12
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"
@.str.3 = [3 x i8] c". \00"
define void @sendtxtfiletosock(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 %36
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 %17, label %11
11: ; preds = %8
%12 = tail call i32 @fileno(i32* %9)
%13 = tail call i64 @sockreadline(i32 %12, i8* %3, i32 1048575, i32* null)
%14 = icmp slt i64 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %11
%16 = getelementptr inbounds i8, i8* %3, i64 1
br label %21
17: ; preds = %8
%18 = load i32, i32* @LOG_WARNING
%19 = tail call i32 (i32, i8*, ...) @syslog(i32 %18, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%20 = tail call i32 @free(i8* %3)
br label %36
21: ; preds = %15, %28
%22 = load i8, i8* %3
%23 = icmp eq i8 %22, 46
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i8, i8* %16
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %3, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 3, i1 false)
br label %28
28: ; preds = %27, %24, %21
%29 = tail call i32 @sendline(i32 %0, i8* %3)
%30 = tail call i32 @fileno(i32* %9)
%31 = tail call i64 @sockreadline(i32 %30, i8* %3, i32 1048575, i32* null)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %21
33: ; preds = %28, %11
%34 = tail call i32 @fclose(i32* %9)
%35 = tail call i32 @free(i8* %3)
br label %36
36: ; preds = %33, %17, %5
ret void
}