#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 *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, void *, size_t);
int /*<<< orphan*/ printf(char *, char *);
size_t recv_data(void *buffer, size_t sz) {
  char *data = (char *)malloc(sz + 1);
  if (NULL == data)
    return sz;
  memcpy(data, buffer, sz);
  data[sz] = '\0';
  printf("recv data: %s", data);
  free(data);
  return sz;
}


x86 assembly (clang, -O0):
<recv_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    117a <recv_data+0x3a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b8 <recv_data+0x78>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rsi
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_data>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x1(%rsi),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     1186 <recv_data+0x46>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%rbx,%r14,1)
lea    0xe8a(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"recv data: %s\00"
define i64 @recv_data(i8* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = add i64 %7, 1
%9 = call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* null, %11
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i64, i64* %5
store i64 %14, i64* %3
br label %28
15: ; preds = %2
%16 = load i8*, i8** %6
%17 = load i8*, i8** %4
%18 = load i64, i64* %5
%19 = call i32 @memcpy(i8* %16, i8* %17, i64 %18)
%20 = load i8*, i8** %6
%21 = load i64, i64* %5
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 0, i8* %22
%23 = load i8*, i8** %6
%24 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %6
%26 = call i32 @free(i8* %25)
%27 = load i64, i64* %5
store i64 %27, i64* %3
br label %28
28: ; preds = %15, %13
%29 = load i64, i64* %3
ret i64 %29
}

LLVM IR (-O2):
@.str = [14 x i8] c"recv data: %s\00"
define i64 @recv_data(i8* %0, i64 returned %1) {
%3 = add i64 %1, 1
%4 = tail call i64 @malloc(i64 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = tail call i32 @memcpy(i8* %5, i8* %0, i64 %1)
%9 = getelementptr inbounds i8, i8* %5, i64 %1
store i8 0, i8* %9
%10 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %5)
%11 = tail call i32 @free(i8* %5)
br label %12
12: ; preds = %2, %7
ret i64 %1
}