#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(size_t);
size_t read(int, ...);
size_t receive_msg(char **msg, int fd) {
  size_t len = 0;
  if (read(fd, &len, sizeof(size_t)) != sizeof(size_t)) {
    return 0;
  }
  (*msg) = malloc((len + 1) * sizeof(char));
  (*msg)[len] = 0;
  if (read(fd, (*msg), len) != len) {
    free(*msg);
    (*msg) = 0;
    return 0;
  }
  return len;
}


x86 assembly (clang, -O0):
<receive_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1040 <read@plt>
cmp    $0x8,%rax
je     1171 <receive_msg+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11e6 <receive_msg+0xb6>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1040 <read@plt>
cmp    -0x20(%rbp),%rax
je     11de <receive_msg+0xae>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11e6 <receive_msg+0xb6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_msg>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <read@plt>
cmp    $0x8,%rax
jne    11a0 <receive_msg+0x70>
mov    0x8(%rsp),%rbx
lea    0x1(%rbx),%rdi
call   1050 <malloc@plt>
mov    %rax,(%r14)
movb   $0x0,(%rax,%rbx,1)
mov    %ebp,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <read@plt>
mov    %rax,%rbx
cmp    0x8(%rsp),%rax
je     11a0 <receive_msg+0x70>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i64 @receive_msg(i8** %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i64
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i64 0, i64* %6
%7 = load i32, i32* %5
%8 = call i64 (i32, ...) @read(i32 %7, i64* %6, i64 8)
%9 = icmp ne i64 %8, 8
br i1 %9, label %10, label %11
10: ; preds = %2
store i64 0, i64* %3
br label %35
11: ; preds = %2
%12 = load i64, i64* %6
%13 = add i64 %12, 1
%14 = mul i64 %13, 1
%15 = call i8* @malloc(i64 %14)
%16 = load i8**, i8*** %4
store i8* %15, i8** %16
%17 = load i8**, i8*** %4
%18 = load i8*, i8** %17
%19 = load i64, i64* %6
%20 = getelementptr inbounds i8, i8* %18, i64 %19
store i8 0, i8* %20
%21 = load i32, i32* %5
%22 = load i8**, i8*** %4
%23 = load i8*, i8** %22
%24 = load i64, i64* %6
%25 = call i64 (i32, ...) @read(i32 %21, i8* %23, i64 %24)
%26 = load i64, i64* %6
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %11
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
%31 = call i32 @free(i8* %30)
%32 = load i8**, i8*** %4
store i8* null, i8** %32
store i64 0, i64* %3
br label %35
33: ; preds = %11
%34 = load i64, i64* %6
store i64 %34, i64* %3
br label %35
35: ; preds = %33, %28, %10
%36 = load i64, i64* %3
ret i64 %36
}

LLVM IR (-O2):
define i64 @receive_msg(i8** nocapture %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%5 = call i64 (i32, ...) @read(i32 %1, i64* %3, i64 8)
%6 = icmp eq i64 %5, 8
br i1 %6, label %7, label %18
7: ; preds = %2
%8 = load i64, i64* %3
%9 = add i64 %8, 1
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %0
%11 = getelementptr inbounds i8, i8* %10, i64 %8
store i8 0, i8* %11
%12 = call i64 (i32, ...) @read(i32 %1, i8* %10, i64 %8)
%13 = load i64, i64* %3
%14 = icmp eq i64 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %7
%16 = load i8*, i8** %0
%17 = call i32 @free(i8* %16)
store i8* null, i8** %0
br label %18
18: ; preds = %7, %2, %15
%19 = phi i64 [ 0, %15 ], [ 0, %2 ], [ %12, %7 ]
ret i64 %19
}