#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 *);
char *fetchMessagesFromServer(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");
    if (answerLength != 0) {
      answer = (char *)malloc(answerLength * sizeof(char));
      if (answer == NULL)
        perror("malloc");
      n = read(socketFD, answer, answerLength);
    }
    return answer;
  } else
    return NULL;
}


x86 assembly (clang, -O0):
<fetchMessagesFromServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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   1050 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    $0x10,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11fc <fetchMessagesFromServer+0x7c>
lea    0xe16(%rip),%rdi
call   1080 <fatalError@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ac <fetchMessagesFromServer+0x12c>
movl   $0x0,-0x40(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jle    12a4 <fetchMessagesFromServer+0x124>
mov    -0xc(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12a4 <fetchMessagesFromServer+0x124>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
je     124f <fetchMessagesFromServer+0xcf>
lea    0xdcf(%rip),%rdi
call   1080 <fatalError@plt>
cmpl   $0x0,-0x14(%rbp)
je     1297 <fetchMessagesFromServer+0x117>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1283 <fetchMessagesFromServer+0x103>
lea    0xdb4(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ac <fetchMessagesFromServer+0x12c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetchMessagesFromServer>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
lea    0x8(%rsp),%r8
mov    $0x10,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
cmp    $0xffffffff,%eax
je     1227 <fetchMessagesFromServer+0xa7>
test   %eax,%eax
jle    1233 <fetchMessagesFromServer+0xb3>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1233 <fetchMessagesFromServer+0xb3>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <read@plt>
cmp    $0x4,%eax
je     1202 <fetchMessagesFromServer+0x82>
lea    0xe1c(%rip),%rdi
call   1080 <fatalError@plt>
mov    (%rsp),%edi
test   %edi,%edi
je     123f <fetchMessagesFromServer+0xbf>
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1241 <fetchMessagesFromServer+0xc1>
mov    (%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <read@plt>
jmp    1235 <fetchMessagesFromServer+0xb5>
lea    0xdd2(%rip),%rdi
call   1080 <fatalError@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
jmp    1235 <fetchMessagesFromServer+0xb5>
lea    0xdea(%rip),%rdi
call   1090 <perror@plt>
jmp    1216 <fetchMessagesFromServer+0x96>

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 = [7 x i8] c"malloc\00"
define i8* @fetchMessagesFromServer(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
store i32 %0, i32* %3
store i32 1, i32* %4
store i32 0, i32* %6
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %13
%14 = call i32 @FD_ZERO(i32* %8)
%15 = load i32, i32* %3
%16 = call i32 @FD_SET(i32 %15, i32* %8)
%17 = call i32 @select(i32 16, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %58
22: ; preds = %1
store i32 0, i32* %11
%23 = load i32, i32* %10
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %57
25: ; preds = %22
%26 = load i32, i32* %3
%27 = call i64 @FD_ISSET(i32 %26, i32* %8)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %57
29: ; preds = %25
%30 = load i32, i32* %3
%31 = call i32 (i32, ...) @read(i32 %30, i32* %5, i32 4)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp ne i32 %32, 4
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = load i32, i32* %5
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %55
39: ; preds = %36
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = mul i64 %41, 1
%43 = trunc i64 %42 to i32
%44 = call i64 @malloc(i32 %43)
%45 = inttoptr i64 %44 to i8*
store i8* %45, i8** %7
%46 = load i8*, i8** %7
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %50
48: ; preds = %39
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %39
%51 = load i32, i32* %3
%52 = load i8*, i8** %7
%53 = load i32, i32* %5
%54 = call i32 (i32, ...) @read(i32 %51, i8* %52, i32 %53)
store i32 %54, i32* %4
br label %55
55: ; preds = %50, %36
%56 = load i8*, i8** %7
store i8* %56, i8** %2
br label %58
57: ; preds = %25, %22
store i8* null, i8** %2
br label %58
58: ; preds = %57, %55, %20
%59 = load i8*, i8** %2
ret i8* %59
}

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 = [7 x i8] c"malloc\00"
define i8* @fetchMessagesFromServer(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 1, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %9
%10 = call i32 @FD_ZERO(i32* %3)
%11 = call i32 @FD_SET(i32 %0, i32* %3)
%12 = call i32 @select(i32 16, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %38
16: ; preds = %1
%17 = icmp sgt i32 %12, 0
br i1 %17, label %18, label %38
18: ; preds = %16
%19 = call i64 @FD_ISSET(i32 %0, i32* %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %38, label %21
21: ; preds = %18
%22 = call i32 (i32, ...) @read(i32 %0, i32* %2, i32 4)
%23 = icmp eq i32 %22, 4
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @fatalError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* %2
%28 = icmp eq i32 %27, 0
br i1 %28, label %38, label %29
29: ; preds = %26
%30 = call i64 @malloc(i32 %27)
%31 = inttoptr i64 %30 to i8*
%32 = icmp eq i64 %30, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* %2
%37 = call i32 (i32, ...) @read(i32 %0, i8* %31, i32 %36)
br label %38
38: ; preds = %16, %18, %26, %35, %14
%39 = phi i8* [ null, %14 ], [ %31, %35 ], [ undef, %26 ], [ null, %18 ], [ null, %16 ]
ret i8* %39
}