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


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

x86 assembly (clang, -O2):
<write_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xe7e(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %r13d,%ebp
lea    (%rax,%rbp,1),%edi
add    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,%rbp
lea    0xe40(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r12,%rcx
mov    %r15,%r8
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1060 <network_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    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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