#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*/ die_nomem();
char *realloc(char *, int);
char *s_malloc(int);
int timeoutRead(int, int, char *, int);
char *readMsg(int fd) {
  int bufcount = 1024;
  int bytes = 0;
  int rc = -1;
  static char *mg_msg, *mg_msg2;
  mg_msg = s_malloc(bufcount);
  mg_msg2 = mg_msg;
  while (1) {
    rc = timeoutRead(30, fd, mg_msg2, 1);
    if (rc < 0)
      return NULL;
    if (rc == 0)
      break;
    bytes = bytes + rc;
    bufcount = bufcount + rc;
    if (NULL == (mg_msg = realloc(mg_msg, bufcount)))
      die_nomem();
    mg_msg2 = mg_msg + bytes;
  }
  mg_msg[bytes] = 0;
  return mg_msg;
}


x86 assembly (clang, -O0):
<readMsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x400,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <s_malloc@plt>
mov    %rax,0x2ed9(%rip)
mov    0x2ed2(%rip),%rax
mov    %rax,0x2ed3(%rip)
mov    -0xc(%rbp),%esi
mov    0x2ec9(%rip),%rdx
mov    $0x1e,%edi
mov    $0x1,%ecx
call   1030 <timeoutRead@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b0 <readMsg+0x70>
movq   $0x0,-0x8(%rbp)
jmp    1230 <readMsg+0xf0>
cmpl   $0x0,-0x18(%rbp)
jne    11bf <readMsg+0x7f>
jmp    1216 <readMsg+0xd6>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e70(%rip),%rdi
mov    -0x10(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    %rcx,0x2e5e(%rip)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11fc <readMsg+0xbc>
mov    $0x0,%al
call   1050 <die_nomem@plt>
mov    0x2e45(%rip),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %rax,0x2e3f(%rip)
jmp    117d <readMsg+0x3d>
mov    0x2e2b(%rip),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e1c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMsg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
mov    $0x400,%ebp
mov    $0x400,%edi
call   1040 <s_malloc@plt>
mov    %rax,0x2ee8(%rip)
mov    %rax,0x2ee9(%rip)
mov    $0x1e,%edi
mov    %r15d,%esi
mov    %rax,%rdx
mov    $0x1,%ecx
call   1030 <timeoutRead@plt>
xor    %r14d,%r14d
test   %eax,%eax
js     11f4 <readMsg+0xb4>
xor    %ebx,%ebx
jmp    11b3 <readMsg+0x73>
nopw   0x0(%rax,%rax,1)
movslq %ebx,%rdx
add    %rax,%rdx
mov    %rdx,0x2eb3(%rip)
mov    $0x1e,%edi
mov    %r15d,%esi
mov    $0x1,%ecx
call   1030 <timeoutRead@plt>
test   %eax,%eax
js     11f4 <readMsg+0xb4>
test   %eax,%eax
je     11e5 <readMsg+0xa5>
add    %eax,%ebx
add    %eax,%ebp
mov    0x2e86(%rip),%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,0x2e78(%rip)
test   %rax,%rax
jne    1190 <readMsg+0x50>
xor    %eax,%eax
call   1050 <die_nomem@plt>
mov    0x2e65(%rip),%rax
jmp    1190 <readMsg+0x50>
mov    0x2e5c(%rip),%r14
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@readMsg.mg_msg = internal global i8* null
@readMsg.mg_msg2 = internal global i8* null
define i8* @readMsg(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 1024, i32* %4
store i32 0, i32* %5
store i32 -1, i32* %6
%7 = load i32, i32* %4
%8 = call i8* @s_malloc(i32 %7)
store i8* %8, i8** @readMsg.mg_msg
%9 = load i8*, i8** @readMsg.mg_msg
store i8* %9, i8** @readMsg.mg_msg2
br label %10
10: ; preds = %1, %34
%11 = load i32, i32* %3
%12 = load i8*, i8** @readMsg.mg_msg2
%13 = call i32 @timeoutRead(i32 30, i32 %11, i8* %12, i32 1)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %10
store i8* null, i8** %2
br label %45
17: ; preds = %10
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
br label %39
21: ; preds = %17
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = add nsw i32 %22, %23
store i32 %24, i32* %5
%25 = load i32, i32* %4
%26 = load i32, i32* %6
%27 = add nsw i32 %25, %26
store i32 %27, i32* %4
%28 = load i8*, i8** @readMsg.mg_msg
%29 = load i32, i32* %4
%30 = call i8* @realloc(i8* %28, i32 %29)
store i8* %30, i8** @readMsg.mg_msg
%31 = icmp eq i8* null, %30
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 (...) @die_nomem()
br label %34
34: ; preds = %32, %21
%35 = load i8*, i8** @readMsg.mg_msg
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8* %38, i8** @readMsg.mg_msg2
br label %10
39: ; preds = %20
%40 = load i8*, i8** @readMsg.mg_msg
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** @readMsg.mg_msg
store i8* %44, i8** %2
br label %45
45: ; preds = %39, %16
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@readMsg.mg_msg = internal unnamed_addr global i8* null
@readMsg.mg_msg2 = internal unnamed_addr global i8* null
define i8* @readMsg(i32 %0) {
%2 = tail call i8* @s_malloc(i32 1024)
store i8* %2, i8** @readMsg.mg_msg
store i8* %2, i8** @readMsg.mg_msg2
%3 = tail call i32 @timeoutRead(i32 30, i32 %0, i8* %2, i32 1)
%4 = icmp slt i32 %3, 0
br i1 %4, label %29, label %5
5: ; preds = %1, %19
%6 = phi i32 [ %23, %19 ], [ %3, %1 ]
%7 = phi i32 [ %11, %19 ], [ 0, %1 ]
%8 = phi i32 [ %12, %19 ], [ 1024, %1 ]
%9 = icmp eq i32 %6, 0
br i1 %9, label %25, label %10
10: ; preds = %5
%11 = add nsw i32 %6, %7
%12 = add nsw i32 %6, %8
%13 = load i8*, i8** @readMsg.mg_msg
%14 = tail call i8* @realloc(i8* %13, i32 %12)
store i8* %14, i8** @readMsg.mg_msg
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = tail call i32 (...) @die_nomem()
%18 = load i8*, i8** @readMsg.mg_msg
br label %19
19: ; preds = %16, %10
%20 = phi i8* [ %18, %16 ], [ %14, %10 ]
%21 = sext i32 %11 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8* %22, i8** @readMsg.mg_msg2
%23 = tail call i32 @timeoutRead(i32 30, i32 %0, i8* %22, i32 1)
%24 = icmp slt i32 %23, 0
br i1 %24, label %29, label %5
25: ; preds = %5
%26 = load i8*, i8** @readMsg.mg_msg
%27 = sext i32 %7 to i64
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %19, %1, %25
%30 = phi i8* [ %26, %25 ], [ null, %1 ], [ null, %19 ]
ret i8* %30
}