#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);
int timeoutRead(int, int, char *, int);
char *x_malloc(int, char *, int);
char *readMsg(int fd) {
  int bufcount = 1024;
  int bytes = 0;
  int rc = -1;
  static char *mg_msg, *mg_msg2;
  mg_msg = x_malloc(
      bufcount,
      "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
      "2022-01-22/02-19-57/repos/rootfs/lessfs/refs/heads/master/lib_net.c",
      147);
  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
lea    0xe96(%rip),%rsi
mov    $0x93,%edx
call   1050 <x_malloc@plt>
mov    %rax,0x2ecd(%rip)
mov    0x2ec6(%rip),%rax
mov    %rax,0x2ec7(%rip)
mov    -0xc(%rbp),%esi
mov    0x2ebd(%rip),%rdx
mov    $0x1e,%edi
mov    $0x1,%ecx
call   1030 <timeoutRead@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bc <readMsg+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    123c <readMsg+0xfc>
cmpl   $0x0,-0x18(%rbp)
jne    11cb <readMsg+0x8b>
jmp    1222 <readMsg+0xe2>
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    0x2e64(%rip),%rdi
mov    -0x10(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    %rcx,0x2e52(%rip)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1208 <readMsg+0xc8>
mov    $0x0,%al
call   1040 <die_nomem@plt>
mov    0x2e39(%rip),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
mov    %rax,0x2e33(%rip)
jmp    1189 <readMsg+0x49>
mov    0x2e1f(%rip),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e10(%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
lea    0xeaf(%rip),%rsi
mov    $0x400,%ebp
mov    $0x400,%edi
mov    $0x93,%edx
call   1050 <x_malloc@plt>
mov    %rax,0x2edc(%rip)
mov    %rax,0x2edd(%rip)
mov    $0x1e,%edi
mov    %r15d,%esi
mov    %rax,%rdx
mov    $0x1,%ecx
call   1030 <timeoutRead@plt>
xor    %r14d,%r14d
test   %eax,%eax
js     1204 <readMsg+0xc4>
xor    %ebx,%ebx
jmp    11c3 <readMsg+0x83>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %ebx,%rdx
add    %rax,%rdx
mov    %rdx,0x2ea3(%rip)
mov    $0x1e,%edi
mov    %r15d,%esi
mov    $0x1,%ecx
call   1030 <timeoutRead@plt>
test   %eax,%eax
js     1204 <readMsg+0xc4>
test   %eax,%eax
je     11f5 <readMsg+0xb5>
add    %eax,%ebx
add    %eax,%ebp
mov    0x2e76(%rip),%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,0x2e68(%rip)
test   %rax,%rax
jne    11a0 <readMsg+0x60>
xor    %eax,%eax
call   1040 <die_nomem@plt>
mov    0x2e55(%rip),%rax
jmp    11a0 <readMsg+0x60>
mov    0x2e4c(%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
@.str = [132 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/rootfs/lessfs/refs/heads/master/lib_net.c\00"
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* @x_malloc(i32 %7, i8* getelementptr inbounds ([132 x i8], [132 x i8]* @.str, i64 0, i64 0), i32 147)
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
@.str = [132 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/rootfs/lessfs/refs/heads/master/lib_net.c\00"
define i8* @readMsg(i32 %0) {
%2 = tail call i8* @x_malloc(i32 1024, i8* getelementptr inbounds ([132 x i8], [132 x i8]* @.str, i64 0, i64 0), i32 147)
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
}