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

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int write(int, ...);
void sendMsgToClient(int clientFD, char *str) {
  int numPacketsToSend = (strlen(str) - 1) / 512 + 1;
  int n = write(clientFD, &numPacketsToSend, sizeof(int));
  char *msgToSend = (char *)malloc(numPacketsToSend * 512);
  strcpy(msgToSend, str);
  int i;
  for (i = 0; i < numPacketsToSend; ++i) {
    int n = write(clientFD, msgToSend, 512);
    msgToSend += 512;
  }
}


x86 assembly (clang, -O0):
<sendMsgToClient>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
shl    $0x9,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e0 <sendMsgToClient+0xa0>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x200,%edx
mov    $0x0,%al
call   1040 <write@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a2 <sendMsgToClient+0x62>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendMsgToClient>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
call   1050 <strlen@plt>
lea    -0x1(%rax),%ecx
add    $0x1fe,%eax
test   %ecx,%ecx
cmovns %ecx,%eax
sar    $0x9,%eax
add    $0x1,%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <write@plt>
mov    0x4(%rsp),%edi
shl    $0x9,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
cmpl   $0x0,0x4(%rsp)
jle    11d2 <sendMsgToClient+0x92>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %eax,%eax
call   1040 <write@plt>
add    $0x200,%rbx
add    $0x1,%ebp
cmp    0x4(%rsp),%ebp
jl     11b0 <sendMsgToClient+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @sendMsgToClient(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = sub nsw i32 %11, 1
%13 = sdiv i32 %12, 512
%14 = add nsw i32 %13, 1
store i32 %14, i32* %5
%15 = load i32, i32* %3
%16 = call i32 (i32, ...) @write(i32 %15, i32* %5, i64 4)
store i32 %16, i32* %6
%17 = load i32, i32* %5
%18 = mul nsw i32 %17, 512
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = load i8*, i8** %4
%23 = call i32 @strcpy(i8* %21, i8* %22)
store i32 0, i32* %8
br label %24
24: ; preds = %34, %2
%25 = load i32, i32* %8
%26 = load i32, i32* %5
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load i32, i32* %3
%30 = load i8*, i8** %7
%31 = call i32 (i32, ...) @write(i32 %29, i8* %30, i32 512)
store i32 %31, i32* %9
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i64 512
store i8* %33, i8** %7
br label %34
34: ; preds = %28
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %24
37: ; preds = %24
ret void
}

LLVM IR (-O2):
define void @sendMsgToClient(i32 %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @strlen(i8* %1)
%6 = add nsw i32 %5, -1
%7 = sdiv i32 %6, 512
%8 = add nsw i32 %7, 1
store i32 %8, i32* %3
%9 = call i32 (i32, ...) @write(i32 %0, i32* %3, i64 4)
%10 = load i32, i32* %3
%11 = shl nsw i32 %10, 9
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = call i32 @strcpy(i8* %13, i8* %1)
%15 = load i32, i32* %3
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %2, %17
%18 = phi i8* [ %21, %17 ], [ %13, %2 ]
%19 = phi i32 [ %22, %17 ], [ 0, %2 ]
%20 = call i32 (i32, ...) @write(i32 %0, i8* %18, i32 512)
%21 = getelementptr inbounds i8, i8* %18, i64 512
%22 = add nuw nsw i32 %19, 1
%23 = load i32, i32* %3
%24 = icmp slt i32 %22, %23
br i1 %24, label %17, label %25
25: ; preds = %17, %2
ret void
}