#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 /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
int write(int, char *, int);
int network_write(int socket, char *data) {
  int r, l;
  char *lstr;
  char *str;
  l = strlen(data);
  lstr = malloc(20 + l);
  sprintf(lstr, "%i", l);
  str = malloc(strlen(lstr) + 1 + strlen(data) + 1);
  sprintf(str, "%s:%s", lstr, data);
  r = write(socket, str, strlen(str));
  free(str);
  free(lstr);
  return r;
}


x86 assembly (clang, -O0):
<network_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
add    $0x14,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<network_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
lea    0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe87(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
lea    0xe57(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%i\00"
@.str.1 = [6 x i8] c"%s:%s\00"
define i32 @network_write(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = add nsw i32 20, %11
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = load i32, i32* %6
%16 = call i32 (i8*, i8*, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i8*, i8** %7
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 1
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %19, %21
%23 = add nsw i32 %22, 1
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = load i8*, i8** %7
%27 = load i8*, i8** %4
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %27)
%29 = load i32, i32* %3
%30 = load i8*, i8** %8
%31 = load i8*, i8** %8
%32 = call i32 @strlen(i8* %31)
%33 = call i32 @write(i32 %29, i8* %30, i32 %32)
store i32 %33, i32* %5
%34 = load i8*, i8** %8
%35 = call i32 @free(i8* %34)
%36 = load i8*, i8** %7
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
@.str = [3 x i8] c"%i\00"
@.str.1 = [6 x i8] c"%s:%s\00"
define i32 @network_write(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = add nsw i32 %3, 20
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %3)
%7 = tail call i32 @strlen(i8* %5)
%8 = tail call i32 @strlen(i8* %1)
%9 = add i32 %7, 2
%10 = add i32 %9, %8
%11 = tail call i8* @malloc(i32 %10)
%12 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %1)
%13 = tail call i32 @strlen(i8* %11)
%14 = tail call i32 @write(i32 %0, i8* %11, i32 %13)
%15 = tail call i32 @free(i8* %11)
%16 = tail call i32 @free(i8* %5)
ret i32 %14
}