#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 int /*<<< orphan*/ longitud;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ lee(int, char *, int, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
char *lee_mensaje(int s_cliente, int segundos, int *out_longitud) {
  int longitud = 0;
  char *buf = NULL;
  if (lee(s_cliente, (char *)&longitud, sizeof(longitud), segundos) &&
      longitud > 0) {
    *out_longitud = longitud;
    buf = (char *)malloc(longitud);
    if (NULL != buf) {
      if (!lee(s_cliente, buf, longitud, segundos)) {
        fprintf(stderr, "Error recibiendo datos.\n");
        free(buf);
        buf = NULL;
      }
    } else
      fprintf(stderr, "No hay memoria.\n");
  }
  return buf;
}


x86 assembly (clang, -O0):
<lee_mensaje>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    -0x8(%rbp),%ecx
mov    $0x4,%edx
call   1050 <lee@plt>
cmp    $0x0,%rax
je     120b <lee_mensaje+0xcb>
cmpl   $0x0,-0x14(%rbp)
jle    120b <lee_mensaje+0xcb>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     11f1 <lee_mensaje+0xb1>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%ecx
call   1050 <lee@plt>
cmp    $0x0,%rax
jne    11ec <lee_mensaje+0xac>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
jmp    1206 <lee_mensaje+0xc6>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1040 <fprintf@plt>
jmp    120b <lee_mensaje+0xcb>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lee_mensaje>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%r14d
mov    %edi,%ebp
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
mov    %r14d,%ecx
call   1050 <lee@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11d4 <lee_mensaje+0x94>
mov    0x4(%rsp),%edi
test   %edi,%edi
jle    11d4 <lee_mensaje+0x94>
mov    %edi,(%r15)
call   1060 <malloc@plt>
test   %rax,%rax
je     11bd <lee_mensaje+0x7d>
mov    %rax,%rbx
mov    0x4(%rsp),%edx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14d,%ecx
call   1050 <lee@plt>
test   %rax,%rax
jne    11d4 <lee_mensaje+0x94>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11d2 <lee_mensaje+0x92>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1040 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"Error recibiendo datos.\0A\00"
@.str.1 = [17 x i8] c"No hay memoria.\0A\00"
define i8* @lee_mensaje(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %7
store i8* null, i8** %8
%9 = load i32, i32* %4
%10 = bitcast i32* %7 to i8*
%11 = load i32, i32* %5
%12 = call i64 @lee(i32 %9, i8* %10, i32 4, i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %42
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %42
17: ; preds = %14
%18 = load i32, i32* %7
%19 = load i32*, i32** %6
store i32 %18, i32* %19
%20 = load i32, i32* %7
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = icmp ne i8* null, %23
br i1 %24, label %25, label %38
25: ; preds = %17
%26 = load i32, i32* %4
%27 = load i8*, i8** %8
%28 = load i32, i32* %7
%29 = load i32, i32* %5
%30 = call i64 @lee(i32 %26, i8* %27, i32 %28, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%35 = load i8*, i8** %8
%36 = call i32 @free(i8* %35)
store i8* null, i8** %8
br label %37
37: ; preds = %32, %25
br label %41
38: ; preds = %17
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %38, %37
br label %42
42: ; preds = %41, %14, %3
%43 = load i8*, i8** %8
ret i8* %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"Error recibiendo datos.\0A\00"
@.str.1 = [17 x i8] c"No hay memoria.\0A\00"
define i8* @lee_mensaje(i32 %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%6 = call i64 @lee(i32 %0, i8* %5, i32 4, i32 %1)
%7 = icmp ne i64 %6, 0
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 0
%10 = select i1 %7, i1 %9, i1 false
br i1 %10, label %11, label %26
11: ; preds = %3
store i32 %8, i32* %2
%12 = call i64 @malloc(i32 %8)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %23, label %15
15: ; preds = %11
%16 = load i32, i32* %4
%17 = call i64 @lee(i32 %0, i8* %13, i32 %16, i32 %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @free(i8* %13)
br label %26
23: ; preds = %11
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %23, %19, %15, %3
%27 = phi i8* [ %13, %15 ], [ null, %19 ], [ null, %23 ], [ null, %3 ]
ret i8* %27
}