#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 */
unsigned int strlen(char *);
int /*<<< orphan*/ warn(char *, ...);
scalar_t__ write(int, ...);
int netSendLongString(int sd, char *s) {
  unsigned length = strlen(s);
  unsigned char b[2];
  if (length >= 64 * 1024) {
    warn("Trying to send a string longer than 64k bytes (%d bytes)", length);
    return 0;
  }
  b[0] = (length >> 8);
  b[1] = (length & 0xff);
  if (write(sd, b, 2) < 0) {
    warn("Couldn't send long string to socket");
    return 0;
  }
  if (write(sd, s, length) < 0) {
    warn("Couldn't send long string to socket");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<netSendLongString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x10000,-0x14(%rbp)
jb     1175 <netSendLongString+0x45>
mov    -0x14(%rbp),%esi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fc <netSendLongString+0xcc>
mov    -0x14(%rbp),%eax
shr    $0x8,%eax
mov    %al,-0x16(%rbp)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x15(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1040 <write@plt>
cmp    $0x0,%rax
jge    11c0 <netSendLongString+0x90>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fc <netSendLongString+0xcc>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1040 <write@plt>
cmp    $0x0,%rax
jge    11f5 <netSendLongString+0xc5>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fc <netSendLongString+0xcc>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netSendLongString>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
cmp    $0x10000,%eax
jb     1163 <netSendLongString+0x33>
lea    0xeab(%rip),%rdi
xor    %r14d,%r14d
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <warn@plt>
jmp    11c5 <netSendLongString+0x95>
mov    %ebp,%eax
rol    $0x8,%ax
mov    %ax,0x6(%rsp)
xor    %r14d,%r14d
lea    0x6(%rsp),%rsi
mov    %r15d,%edi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <write@plt>
test   %rax,%rax
js     11b7 <netSendLongString+0x87>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <write@plt>
mov    $0x1,%r14d
test   %rax,%rax
jns    11c5 <netSendLongString+0x95>
lea    0xe8e(%rip),%rdi
xor    %eax,%eax
call   1030 <warn@plt>
xor    %r14d,%r14d
jmp    11c5 <netSendLongString+0x95>
lea    0xe7b(%rip),%rdi
xor    %eax,%eax
call   1030 <warn@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [57 x i8] c"Trying to send a string longer than 64k bytes (%d bytes)\00"
@.str.1 = [36 x i8] c"Couldn't send long string to socket\00"
define i32 @netSendLongString(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [2 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp uge i32 %10, 65536
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* %6
%14 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 0, i32* %3
br label %39
15: ; preds = %2
%16 = load i32, i32* %6
%17 = lshr i32 %16, 8
%18 = trunc i32 %17 to i8
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
store i8 %18, i8* %19
%20 = load i32, i32* %6
%21 = and i32 %20, 255
%22 = trunc i32 %21 to i8
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
store i8 %22, i8* %23
%24 = load i32, i32* %4
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%26 = call i64 (i32, ...) @write(i32 %24, i8* %25, i32 2)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %15
%29 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %39
30: ; preds = %15
%31 = load i32, i32* %4
%32 = load i8*, i8** %5
%33 = load i32, i32* %6
%34 = call i64 (i32, ...) @write(i32 %31, i8* %32, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %39
38: ; preds = %30
store i32 1, i32* %3
br label %39
39: ; preds = %38, %36, %28, %12
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [57 x i8] c"Trying to send a string longer than 64k bytes (%d bytes)\00"
@.str.1 = [36 x i8] c"Couldn't send long string to socket\00"
define i32 @netSendLongString(i32 %0, i8* %1) {
%3 = alloca [2 x i8]
%4 = tail call i32 @strlen(i8* %1)
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%6 = icmp ugt i32 %4, 65535
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %23
9: ; preds = %2
%10 = lshr i32 %4, 8
%11 = trunc i32 %10 to i8
store i8 %11, i8* %5
%12 = trunc i32 %4 to i8
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
store i8 %12, i8* %13
%14 = call i64 (i32, ...) @write(i32 %0, i8* %5, i32 2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %23
18: ; preds = %9
%19 = call i64 (i32, ...) @write(i32 %0, i8* %1, i32 %4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %18, %21, %16, %7
%24 = phi i32 [ 0, %7 ], [ 0, %16 ], [ 0, %21 ], [ 1, %18 ]
ret i32 %24
}