#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 */
struct sockaddr_in6 {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ dieError(char *, int);
int /*<<< orphan*/ *fopen(char *, char *);
char *get_data_from_file(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strlen(char *);
void send_to_client(int sock, struct sockaddr_in6 *client_addr) {
  FILE *fp;
  char *msg;
  int numChunk = 0;
  int count;
  fp = fopen("output.txt", "r");
  msg = get_data_from_file(fp, 10);
  numChunk = (int)strlen(msg) / 1024;
  for (count = 0; count <= numChunk; count++) {
    char sentMsg[1024];
    int numBytesCpy = ((int)strlen(msg) + numChunk) - (1024 * count);
    if (numBytesCpy >= 1024)
      numBytesCpy = 1023;
    memset(sentMsg, 0, 1024);
    sprintf(sentMsg, "%x", (numChunk - count));
    memcpy(sentMsg + 1, msg + (1023 * count), numBytesCpy);
    sentMsg[1024] = '\0';
    if (sendto(sock, sentMsg, numBytesCpy + 1, 0,
               (struct sockaddr *)client_addr, sizeof(*client_addr)) < 0) {
      dieError("sendto() failed", sock);
    }
  }
}


x86 assembly (clang, -O0):
<send_to_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xe60(%rip),%rdi
lea    0xe64(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0xa,%esi
call   1030 <get_data_from_file@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     12c3 <send_to_client+0x143>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x24(%rbp),%eax
mov    -0x28(%rbp),%ecx
shl    $0xa,%ecx
sub    %ecx,%eax
mov    %eax,-0x434(%rbp)
cmpl   $0x400,-0x434(%rbp)
jl     121d <send_to_client+0x9d>
movl   $0x3ff,-0x434(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0x430(%rbp),%rdi
mov    -0x24(%rbp),%edx
sub    -0x28(%rbp),%edx
lea    0xdc9(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x430(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rsi
imul   $0x3ff,-0x28(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x434(%rbp),%edx
call   1070 <memcpy@plt>
movb   $0x0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x434(%rbp),%edx
add    $0x1,%edx
mov    -0x10(%rbp),%r8
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1060 <sendto@plt>
cmp    $0x0,%rax
jge    12b0 <send_to_client+0x130>
mov    -0x4(%rbp),%esi
lea    0xd65(%rip),%rdi
call   1080 <dieError@plt>
jmp    12b5 <send_to_client+0x135>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11dd <send_to_client+0x5d>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_to_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,0x18(%rsp)
mov    %edi,0xc(%rsp)
lea    0xe5f(%rip),%rdi
lea    0xe63(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rdi
mov    $0xa,%esi
call   1030 <get_data_from_file@plt>
mov    %rax,0x10(%rsp)
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    0x3ff(%rax),%r15d
test   %eax,%eax
cmovns %eax,%r15d
cmp    $0xfffffc01,%eax
jge    11ed <send_to_client+0x6d>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
sar    $0xa,%r15d
lea    0x1(%r15),%ebp
lea    0x20(%rsp),%rbx
mov    0x10(%rsp),%r12
mov    %r15d,%r13d
jmp    1228 <send_to_client+0xa8>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0xffffffff,%r13d
add    $0xfffffc00,%r15d
add    $0x3ff,%r12
add    $0xffffffffffffffff,%rbp
je     11db <send_to_client+0x5b>
mov    0x10(%rsp),%rdi
call   1040 <strlen@plt>
lea    (%rax,%r15,1),%r14d
cmp    $0x3ff,%r14d
mov    $0x3ff,%eax
cmovge %eax,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
lea    0xdae(%rip),%rsi
mov    %r13d,%edx
call   10a0 <sprintf@plt>
lea    0x21(%rsp),%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1070 <memcpy@plt>
movb   $0x0,0x420(%rsp)
add    $0x1,%r14d
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    0x18(%rsp),%r8
mov    $0x4,%r9d
call   1060 <sendto@plt>
test   %rax,%rax
jns    1210 <send_to_client+0x90>
lea    0xd61(%rip),%rdi
mov    0xc(%rsp),%esi
call   1080 <dieError@plt>
jmp    1210 <send_to_client+0x90>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"output.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%x\00"
@.str.3 = [16 x i8] c"sendto() failed\00"
define void @send_to_client(i32 %0, %struct.sockaddr_in6* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in6*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32
store i32 %0, i32* %3
store %struct.sockaddr_in6* %1, %struct.sockaddr_in6** %4
store i32 0, i32* %7
%11 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = call i8* @get_data_from_file(i32* %12, i32 10)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = call i64 @strlen(i8* %14)
%16 = trunc i64 %15 to i32
%17 = sdiv i32 %16, 1024
store i32 %17, i32* %7
store i32 0, i32* %8
br label %18
18: ; preds = %64, %2
%19 = load i32, i32* %8
%20 = load i32, i32* %7
%21 = icmp sle i32 %19, %20
br i1 %21, label %22, label %67
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = call i64 @strlen(i8* %23)
%25 = trunc i64 %24 to i32
%26 = load i32, i32* %7
%27 = add nsw i32 %25, %26
%28 = load i32, i32* %8
%29 = mul nsw i32 1024, %28
%30 = sub nsw i32 %27, %29
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp sge i32 %31, 1024
br i1 %32, label %33, label %34
33: ; preds = %22
store i32 1023, i32* %10
br label %34
34: ; preds = %33, %22
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%36 = call i32 @memset(i8* %35, i32 0, i32 1024)
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = sub nsw i32 %38, %39
%41 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = load i8*, i8** %6
%45 = load i32, i32* %8
%46 = mul nsw i32 1023, %45
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
%49 = load i32, i32* %10
%50 = call i32 @memcpy(i8* %43, i8* %48, i32 %49)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 1024
store i8 0, i8* %51
%52 = load i32, i32* %3
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%54 = load i32, i32* %10
%55 = add nsw i32 %54, 1
%56 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %4
%57 = bitcast %struct.sockaddr_in6* %56 to %struct.sockaddr*
%58 = call i64 @sendto(i32 %52, i8* %53, i32 %55, i32 0, %struct.sockaddr* %57, i32 4)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %34
%61 = load i32, i32* %3
%62 = call i32 @dieError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %60, %34
br label %64
64: ; preds = %63
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %18
67: ; preds = %18
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"output.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%x\00"
@.str.3 = [16 x i8] c"sendto() failed\00"
define void @send_to_client(i32 %0, %struct.sockaddr_in6* %1) {
%3 = alloca [1024 x i8]
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i8* @get_data_from_file(i32* %4, i32 10)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %5)
%7 = trunc i64 %6 to i32
%8 = sdiv i32 %7, 1024
%9 = icmp slt i32 %7, -1023
br i1 %9, label %42, label %10
10: ; preds = %2
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1024
%14 = bitcast %struct.sockaddr_in6* %1 to %struct.sockaddr*
%15 = add nsw i32 %8, 1
%16 = zext i32 %15 to i64
br label %17
17: ; preds = %10, %39
%18 = phi i64 [ 0, %10 ], [ %40, %39 ]
%19 = call i64 @strlen(i8* dereferenceable(1) %5)
%20 = trunc i64 %19 to i32
%21 = trunc i64 %18 to i32
%22 = mul i32 %21, -1024
%23 = add i32 %22, %8
%24 = add i32 %23, %20
%25 = icmp slt i32 %24, 1023
%26 = select i1 %25, i32 %24, i32 1023
%27 = call i32 @memset(i8* %11, i32 0, i32 1024)
%28 = trunc i64 %18 to i32
%29 = sub i32 %8, %28
%30 = call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = mul nuw nsw i64 %18, 1023
%32 = getelementptr inbounds i8, i8* %5, i64 %31
%33 = call i32 @memcpy(i8* %12, i8* %32, i32 %26)
store i8 0, i8* %13
%34 = add nsw i32 %26, 1
%35 = call i64 @sendto(i32 %0, i8* %11, i32 %34, i32 0, %struct.sockaddr* %14, i32 4)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %17
%38 = call i32 @dieError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %39
39: ; preds = %37, %17
%40 = add nuw nsw i64 %18, 1
%41 = icmp eq i64 %40, %16
br i1 %41, label %42, label %17
42: ; preds = %39, %2
ret void
}