#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int mpath_recv_reply_data(int, char *, scalar_t__, unsigned int);
scalar_t__ mpath_recv_reply_len(int, unsigned int);
int mpath_recv_reply(int fd, char **reply, unsigned int timeout) {
  int err;
  ssize_t len;
  *reply = NULL;
  len = mpath_recv_reply_len(fd, timeout);
  if (len <= 0)
    return len;
  *reply = malloc(len);
  if (!*reply)
    return -1;
  err = mpath_recv_reply_data(fd, *reply, len, timeout);
  if (err) {
    free(*reply);
    *reply = NULL;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mpath_recv_reply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1060 <mpath_recv_reply_len@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     1183 <mpath_recv_reply+0x43>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11fd <mpath_recv_reply+0xbd>
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11b0 <mpath_recv_reply+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <mpath_recv_reply+0xbd>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1040 <mpath_recv_reply_data@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11f6 <mpath_recv_reply+0xb6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <mpath_recv_reply+0xbd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpath_recv_reply>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%ebp
movq   $0x0,(%rsi)
mov    %edx,%esi
call   1060 <mpath_recv_reply_len@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    11a4 <mpath_recv_reply+0x64>
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,(%r12)
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11ac <mpath_recv_reply+0x6c>
mov    %ebp,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1040 <mpath_recv_reply_data@plt>
test   %eax,%eax
je     11a9 <mpath_recv_reply+0x69>
mov    (%r12),%rdi
call   1030 <free@plt>
movq   $0x0,(%r12)
jmp    11ac <mpath_recv_reply+0x6c>
mov    %ebx,%r14d
jmp    11ac <mpath_recv_reply+0x6c>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @mpath_recv_reply(i32 %0, i8** nocapture %1, i32 %2) {
store i8* null, i8** %1
%4 = tail call i64 @mpath_recv_reply_len(i32 %0, i32 %2)
%5 = icmp slt i64 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = trunc i64 %4 to i32
br label %17
8: ; preds = %3
%9 = tail call i8* @malloc(i64 %4)
store i8* %9, i8** %1
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = tail call i32 @mpath_recv_reply_data(i32 %0, i8* %9, i64 %4, i32 %2)
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i8*, i8** %1
%16 = tail call i32 @free(i8* %15)
store i8* null, i8** %1
br label %17
17: ; preds = %11, %8, %14, %6
%18 = phi i32 [ %7, %6 ], [ -1, %14 ], [ -1, %8 ], [ 0, %11 ]
ret i32 %18
}