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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int sendFileInChunks(int CSock, FILE *data) {
  char buffer[1024];
  int bytecount = 0;
  int i = 0;
  int c;
  int fd;
  memset(buffer, 0, sizeof(buffer));
  fd = fileno(data);
  while ((i = read(fd, buffer, 1024)) > 0) {
    for (c = 0; c < i; c++) {
      if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ ", buffer[c]) == NULL) {
        if (c == (i - 1) && buffer[c] == '\n') {
        } else {
          fprintf(stderr,
                  "ERROR: text file contains bad characters at byte %d\n",
                  bytecount + c);
          return (-1);
        }
      }
    }
    if (send(CSock, buffer, strlen(buffer), 0) != i)
      perror("send");
    memset(buffer, 0, sizeof(buffer));
    bytecount += i;
  }
  close(fd);
  strcpy(buffer, "\n");
  send(CSock, buffer, strlen(buffer), 0);
  return (bytecount);
}


x86 assembly (clang, -O0):
<sendFileInChunks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x414(%rbp)
movl   $0x0,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <fileno@plt>
mov    %eax,-0x420(%rbp)
mov    -0x420(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,-0x418(%rbp)
cmp    $0x0,%eax
jle    1349 <sendFileInChunks+0x1a9>
movl   $0x0,-0x41c(%rbp)
mov    -0x41c(%rbp),%eax
cmp    -0x418(%rbp),%eax
jge    12ce <sendFileInChunks+0x12e>
movslq -0x41c(%rbp),%rax
lea    0xdc8(%rip),%rdi
movsbl -0x410(%rbp,%rax,1),%esi
ff
call   1060 <strchr@plt>
cmp    $0x0,%rax
jne    12b5 <sendFileInChunks+0x115>
mov    -0x41c(%rbp),%eax
mov    -0x418(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1283 <sendFileInChunks+0xe3>
movslq -0x41c(%rbp),%rax
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1283 <sendFileInChunks+0xe3>
jmp    12b0 <sendFileInChunks+0x110>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    -0x414(%rbp),%edx
add    -0x41c(%rbp),%edx
lea    0xd7d(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a9 <sendFileInChunks+0x209>
jmp    12b5 <sendFileInChunks+0x115>
jmp    12ba <sendFileInChunks+0x11a>
mov    -0x41c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
jmp    1218 <sendFileInChunks+0x78>
mov    -0x8(%rbp),%eax
mov    %eax,-0x42c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    -0x418(%rbp),%eax
je     131f <sendFileInChunks+0x17f>
lea    0xd37(%rip),%rdi
call   10c0 <perror@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x418(%rbp),%eax
add    -0x414(%rbp),%eax
mov    %eax,-0x414(%rbp)
jmp    11e8 <sendFileInChunks+0x48>
mov    -0x420(%rbp),%edi
call   1080 <close@plt>
lea    -0x410(%rbp),%rdi
lea    0xced(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0x414(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendFileInChunks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbp
mov    %edi,0x8(%rsp)
lea    0x10(%rsp),%r12
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %rbp,%rdi
call   10b0 <fileno@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    12d3 <sendFileInChunks+0x133>
mov    %eax,%r13d
xor    %r15d,%r15d
lea    0xe03(%rip),%rbp
lea    0x10(%rsp),%r12
mov    %ebx,0xc(%rsp)
mov    %r12,%rbx
mov    %r13d,%r14d
add    $0xffffffff,%r13d
xor    %r12d,%r12d
jmp    1229 <sendFileInChunks+0x89>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r12
cmp    %r12,%r14
je     1250 <sendFileInChunks+0xb0>
movsbl 0x10(%rsp,%r12,1),%esi
mov    %rbp,%rdi
call   1060 <strchr@plt>
test   %rax,%rax
jne    1220 <sendFileInChunks+0x80>
cmp    %r12,%r13
jne    12b0 <sendFileInChunks+0x110>
cmpb   $0xa,0x10(%rsp,%r12,1)
je     1220 <sendFileInChunks+0x80>
jmp    12b0 <sendFileInChunks+0x110>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    %r14d,%eax
jne    12a2 <sendFileInChunks+0x102>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
add    %r14d,%r15d
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
jg     1206 <sendFileInChunks+0x66>
jmp    12d3 <sendFileInChunks+0x133>
lea    0xda8(%rip),%rdi
call   10c0 <perror@plt>
jmp    1270 <sendFileInChunks+0xd0>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
add    %r12d,%r15d
lea    0xd59(%rip),%rsi
mov    %r15d,%edx
call   10a0 <fprintf@plt>
mov    $0xffffffff,%r15d
jmp    1306 <sendFileInChunks+0x166>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xd6e(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    0x8(%rsp),%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %r15d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZ \00"
@stderr = global i32 0
@.str.1 = [53 x i8] c"ERROR: text file contains bad characters at byte %d\0A\00"
@.str.2 = [5 x i8] c"send\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @sendFileInChunks(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 0, i32* %7
store i32 0, i32* %8
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 1024)
%13 = load i32*, i32** %5
%14 = call i32 @fileno(i32* %13)
store i32 %14, i32* %10
br label %15
15: ; preds = %66, %2
%16 = load i32, i32* %10
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 1024)
store i32 %18, i32* %8
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %72
20: ; preds = %15
store i32 0, i32* %9
br label %21
21: ; preds = %53, %20
%22 = load i32, i32* %9
%23 = load i32, i32* %8
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %56
25: ; preds = %21
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = call i32* @strchr(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8 signext %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %52
32: ; preds = %25
%33 = load i32, i32* %9
%34 = load i32, i32* %8
%35 = sub nsw i32 %34, 1
%36 = icmp eq i32 %33, %35
br i1 %36, label %37, label %45
37: ; preds = %32
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 10
br i1 %43, label %44, label %45
44: ; preds = %37
br label %51
45: ; preds = %37, %32
%46 = load i32, i32* @stderr
%47 = load i32, i32* %7
%48 = load i32, i32* %9
%49 = add nsw i32 %47, %48
%50 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %49)
store i32 -1, i32* %3
br label %83
51: ; preds = %44
br label %52
52: ; preds = %51, %25
br label %53
53: ; preds = %52
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %21
56: ; preds = %21
%57 = load i32, i32* %4
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%60 = call i32 @strlen(i8* %59)
%61 = call i32 @send(i32 %57, i8* %58, i32 %60, i32 0)
%62 = load i32, i32* %8
%63 = icmp ne i32 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %56
%65 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %66
66: ; preds = %64, %56
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%68 = call i32 @memset(i8* %67, i32 0, i32 1024)
%69 = load i32, i32* %8
%70 = load i32, i32* %7
%71 = add nsw i32 %70, %69
store i32 %71, i32* %7
br label %15
72: ; preds = %15
%73 = load i32, i32* %10
%74 = call i32 @close(i32 %73)
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%76 = call i32 @strcpy(i8* %75, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%77 = load i32, i32* %4
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%79 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%80 = call i32 @strlen(i8* %79)
%81 = call i32 @send(i32 %77, i8* %78, i32 %80, i32 0)
%82 = load i32, i32* %7
store i32 %82, i32* %3
br label %83
83: ; preds = %72, %45
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@.str = [28 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZ \00"
@stderr = global i32 0
@.str.1 = [53 x i8] c"ERROR: text file contains bad characters at byte %d\0A\00"
@.str.2 = [5 x i8] c"send\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @sendFileInChunks(i32 %0, i32* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 1024)
%6 = call i32 @fileno(i32* %1)
%7 = call i32 @read(i32 %6, i8* %4, i32 1024)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %45
9: ; preds = %2, %40
%10 = phi i32 [ %43, %40 ], [ %7, %2 ]
%11 = phi i32 [ %42, %40 ], [ 0, %2 ]
%12 = add nsw i32 %10, -1
%13 = zext i32 %12 to i64
%14 = zext i32 %10 to i64
br label %15
15: ; preds = %9, %31
%16 = phi i64 [ 0, %9 ], [ %32, %31 ]
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = call i32* @strchr(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8 signext %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %31
21: ; preds = %15
%22 = icmp eq i64 %16, %13
br i1 %22, label %23, label %26
23: ; preds = %21
%24 = load i8, i8* %17
%25 = icmp eq i8 %24, 10
br i1 %25, label %31, label %26
26: ; preds = %23, %21
%27 = trunc i64 %16 to i32
%28 = load i32, i32* @stderr
%29 = add nsw i32 %11, %27
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %51
31: ; preds = %15, %23
%32 = add nuw nsw i64 %16, 1
%33 = icmp eq i64 %32, %14
br i1 %33, label %34, label %15
34: ; preds = %31
%35 = call i32 @strlen(i8* %4)
%36 = call i32 @send(i32 %0, i8* %4, i32 %35, i32 0)
%37 = icmp eq i32 %36, %10
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = call i32 @memset(i8* %4, i32 0, i32 1024)
%42 = add nsw i32 %10, %11
%43 = call i32 @read(i32 %6, i8* %4, i32 1024)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %9, label %45
45: ; preds = %40, %2
%46 = phi i32 [ 0, %2 ], [ %42, %40 ]
%47 = call i32 @close(i32 %6)
%48 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @strlen(i8* %4)
%50 = call i32 @send(i32 %0, i8* %4, i32 %49, i32 0)
br label %51
51: ; preds = %45, %26
%52 = phi i32 [ -1, %26 ], [ %46, %45 ]
ret i32 %52
}