#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 */
int strlen(char *);
int write(int, ...);
int net_send(int fd, char *data) {
  unsigned char msb, lsb;
  int len;
  len = strlen(data);
  if (len <= 0 || len > 1024)
    return (0);
  msb = len / 256;
  lsb = len % 256;
  if (write(fd, &msb, 1) != 1)
    return (0);
  if (write(fd, &lsb, 1) != 1)
    return (0);
  if (write(fd, data, len) != len)
    return (0);
  return (len);
}


x86 assembly (clang, -O0):
<net_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1152 <net_send+0x32>
cmpl   $0x400,-0x18(%rbp)
jle    115e <net_send+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <net_send+0xd8>
mov    -0x18(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %al,-0x11(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x12(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
cmp    $0x1,%eax
je     11a4 <net_send+0x84>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <net_send+0xd8>
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
cmp    $0x1,%eax
je     11cc <net_send+0xac>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <net_send+0xd8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     11f2 <net_send+0xd2>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <net_send+0xd8>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_send>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    -0x401(%rbx),%eax
xor    %ebp,%ebp
cmp    $0xfffffc00,%eax
jb     119a <net_send+0x7a>
mov    %bh,0x7(%rsp)
mov    %bl,0x6(%rsp)
xor    %ebp,%ebp
lea    0x7(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x1,%eax
jne    119a <net_send+0x7a>
xor    %ebp,%ebp
lea    0x6(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x1,%eax
jne    119a <net_send+0x7a>
xor    %ebp,%ebp
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    %ebx,%eax
cmove  %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @net_send(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp sle i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %8
%15 = icmp sgt i32 %14, 1024
br i1 %15, label %16, label %17
16: ; preds = %13, %2
store i32 0, i32* %3
br label %43
17: ; preds = %13
%18 = load i32, i32* %8
%19 = sdiv i32 %18, 256
%20 = trunc i32 %19 to i8
store i8 %20, i8* %6
%21 = load i32, i32* %8
%22 = srem i32 %21, 256
%23 = trunc i32 %22 to i8
store i8 %23, i8* %7
%24 = load i32, i32* %4
%25 = call i32 (i32, ...) @write(i32 %24, i8* %6, i32 1)
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %17
store i32 0, i32* %3
br label %43
28: ; preds = %17
%29 = load i32, i32* %4
%30 = call i32 (i32, ...) @write(i32 %29, i8* %7, i32 1)
%31 = icmp ne i32 %30, 1
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 0, i32* %3
br label %43
33: ; preds = %28
%34 = load i32, i32* %4
%35 = load i8*, i8** %5
%36 = load i32, i32* %8
%37 = call i32 (i32, ...) @write(i32 %34, i8* %35, i32 %36)
%38 = load i32, i32* %8
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 0, i32* %3
br label %43
41: ; preds = %33
%42 = load i32, i32* %8
store i32 %42, i32* %3
br label %43
43: ; preds = %41, %40, %32, %27, %16
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
define i32 @net_send(i32 %0, i8* %1) {
%3 = alloca i8
%4 = alloca i8
%5 = tail call i32 @strlen(i8* %1)
%6 = add i32 %5, -1025
%7 = icmp ult i32 %6, -1024
br i1 %7, label %21, label %8
8: ; preds = %2
%9 = lshr i32 %5, 8
%10 = trunc i32 %9 to i8
store i8 %10, i8* %3
%11 = trunc i32 %5 to i8
store i8 %11, i8* %4
%12 = call i32 (i32, ...) @write(i32 %0, i8* %3, i32 1)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %21
14: ; preds = %8
%15 = call i32 (i32, ...) @write(i32 %0, i8* %4, i32 1)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i32 (i32, ...) @write(i32 %0, i8* %1, i32 %5)
%19 = icmp eq i32 %18, %5
%20 = select i1 %19, i32 %5, i32 0
br label %21
21: ; preds = %17, %14, %8, %2
%22 = phi i32 [ 0, %2 ], [ 0, %8 ], [ 0, %14 ], [ %20, %17 ]
ret i32 %22
}