#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 */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ fdSet;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ fatalError(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int read(int, ...);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int write(int, int *, int);
char *readResponseFromServer(int socketFD) {
  int n = 1;
  int answerLength;
  int allGood = 0;
  char *answer;
  fd_set fdSet;
  struct timeval timeout;
  int rc, result;
  timeout.tv_sec = 1;
  timeout.tv_usec = 0;
  FD_ZERO(&fdSet);
  FD_SET(socketFD, &fdSet);
  rc = select(sizeof(fdSet) * 4, &fdSet, NULL, NULL, &timeout);
  if (rc == -1) {
    fatalError("Error esperando mensajes");
    return NULL;
  }
  result = 0;
  if (rc > 0 && FD_ISSET(socketFD, &fdSet)) {
    n = read(socketFD, &answerLength, 4);
    if (n != 4)
      fatalError("Response reading error 1");
    int b;
    if ((b = write(socketFD, &allGood, 4)) == -1)
      fatalError("Response reading error 2");
    if (answerLength != 0) {
      answer = (char *)malloc(answerLength * sizeof(char));
      if (answer == NULL)
        perror("malloc");
      n = read(socketFD, answer, answerLength);
      write(socketFD, &allGood, 4);
    }
    return answer;
  } else
    return NULL;
}


x86 assembly (clang, -O0):
<readResponseFromServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x24(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    $0x10,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    120c <readResponseFromServer+0x7c>
lea    0xe06(%rip),%rdi
call   1090 <fatalError@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f6 <readResponseFromServer+0x166>
movl   $0x0,-0x40(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    12ee <readResponseFromServer+0x15e>
mov    -0xc(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12ee <readResponseFromServer+0x15e>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
je     125f <readResponseFromServer+0xcf>
lea    0xdbf(%rip),%rdi
call   1090 <fatalError@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x44(%rbp)
cmp    $0xffffffff,%eax
jne    1288 <readResponseFromServer+0xf8>
lea    0xdaf(%rip),%rdi
call   1090 <fatalError@plt>
cmpl   $0x0,-0x14(%rbp)
je     12e1 <readResponseFromServer+0x151>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12bc <readResponseFromServer+0x12c>
lea    0xd94(%rip),%rdi
call   10a0 <perror@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f6 <readResponseFromServer+0x166>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readResponseFromServer>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebp
movl   $0x0,0xc(%rsp)
movl   $0x1,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
lea    0x18(%rsp),%r8
mov    $0x10,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
cmp    $0xffffffff,%eax
je     1282 <readResponseFromServer+0xf2>
test   %eax,%eax
jle    128e <readResponseFromServer+0xfe>
lea    0x14(%rsp),%rsi
mov    %ebp,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     128e <readResponseFromServer+0xfe>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <read@plt>
cmp    $0x4,%eax
je     1228 <readResponseFromServer+0x98>
lea    0xdf6(%rip),%rdi
call   1090 <fatalError@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    124a <readResponseFromServer+0xba>
lea    0xded(%rip),%rdi
call   1090 <fatalError@plt>
mov    0x10(%rsp),%edi
test   %edi,%edi
je     129a <readResponseFromServer+0x10a>
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     129c <readResponseFromServer+0x10c>
mov    0x10(%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <read@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    1290 <readResponseFromServer+0x100>
lea    0xd77(%rip),%rdi
call   1090 <fatalError@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
jmp    1290 <readResponseFromServer+0x100>
lea    0xda8(%rip),%rdi
call   10a0 <perror@plt>
jmp    125f <readResponseFromServer+0xcf>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [25 x i8] c"Error esperando mensajes\00"
@.str.1 = [25 x i8] c"Response reading error 1\00"
@.str.2 = [25 x i8] c"Response reading error 2\00"
@.str.3 = [7 x i8] c"malloc\00"
define i8* @readResponseFromServer(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %4
store i32 0, i32* %6
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1, i32* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %14
%15 = call i32 @FD_ZERO(i32* %8)
%16 = load i32, i32* %3
%17 = call i32 @FD_SET(i32 %16, i32* %8)
%18 = call i32 @select(i32 16, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %67
23: ; preds = %1
store i32 0, i32* %11
%24 = load i32, i32* %10
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %66
26: ; preds = %23
%27 = load i32, i32* %3
%28 = call i64 @FD_ISSET(i32 %27, i32* %8)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %66
30: ; preds = %26
%31 = load i32, i32* %3
%32 = call i32 (i32, ...) @read(i32 %31, i32* %5, i32 4)
store i32 %32, i32* %4
%33 = load i32, i32* %4
%34 = icmp ne i32 %33, 4
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %30
%38 = load i32, i32* %3
%39 = call i32 @write(i32 %38, i32* %6, i32 4)
store i32 %39, i32* %12
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = load i32, i32* %5
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %64
46: ; preds = %43
%47 = load i32, i32* %5
%48 = sext i32 %47 to i64
%49 = mul i64 %48, 1
%50 = trunc i64 %49 to i32
%51 = call i64 @malloc(i32 %50)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %7
%53 = load i8*, i8** %7
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %57
55: ; preds = %46
%56 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %57
57: ; preds = %55, %46
%58 = load i32, i32* %3
%59 = load i8*, i8** %7
%60 = load i32, i32* %5
%61 = call i32 (i32, ...) @read(i32 %58, i8* %59, i32 %60)
store i32 %61, i32* %4
%62 = load i32, i32* %3
%63 = call i32 @write(i32 %62, i32* %6, i32 4)
br label %64
64: ; preds = %57, %43
%65 = load i8*, i8** %7
store i8* %65, i8** %2
br label %67
66: ; preds = %26, %23
store i8* null, i8** %2
br label %67
67: ; preds = %66, %64, %21
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [25 x i8] c"Error esperando mensajes\00"
@.str.1 = [25 x i8] c"Response reading error 1\00"
@.str.2 = [25 x i8] c"Response reading error 2\00"
@.str.3 = [7 x i8] c"malloc\00"
define i8* @readResponseFromServer(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.timeval* %5 to i8*
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 1, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %11
%12 = call i32 @FD_ZERO(i32* %4)
%13 = call i32 @FD_SET(i32 %0, i32* %4)
%14 = call i32 @select(i32 16, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %46
18: ; preds = %1
%19 = icmp sgt i32 %14, 0
br i1 %19, label %20, label %46
20: ; preds = %18
%21 = call i64 @FD_ISSET(i32 %0, i32* %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %46, label %23
23: ; preds = %20
%24 = call i32 (i32, ...) @read(i32 %0, i32* %2, i32 4)
%25 = icmp eq i32 %24, 4
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = call i32 @write(i32 %0, i32* %3, i32 4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = load i32, i32* %2
%35 = icmp eq i32 %34, 0
br i1 %35, label %46, label %36
36: ; preds = %33
%37 = call i64 @malloc(i32 %34)
%38 = inttoptr i64 %37 to i8*
%39 = icmp eq i64 %37, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = load i32, i32* %2
%44 = call i32 (i32, ...) @read(i32 %0, i8* %38, i32 %43)
%45 = call i32 @write(i32 %0, i32* %3, i32 4)
br label %46
46: ; preds = %18, %20, %33, %42, %16
%47 = phi i8* [ null, %16 ], [ %38, %42 ], [ undef, %33 ], [ null, %20 ], [ null, %18 ]
ret i8* %47
}