#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*/ ParseJob(int, char *);
int ReadNBytes(int, char *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(int *, char *, int);
int ntohl(int);
int HandleRead(int fd) {
  int err = 0;
  char len[4];
  err = ReadNBytes(fd, len, 4);
  if (err != 0) {
    return err;
  }
  int msglen = 0;
  memcpy(&msglen, len, 4);
  msglen = ntohl(msglen);
  char *data = (char *)malloc(msglen + 1);
  bzero(data, msglen + 1);
  err = ReadNBytes(fd, data, msglen);
  if (err != 0) {
    free(data);
  } else {
    ParseJob(fd, data);
  }
  return err;
}


x86 assembly (clang, -O0):
<HandleRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <ReadNBytes@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11ab <HandleRead+0x3b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1229 <HandleRead+0xb9>
movl   $0x0,-0x14(%rbp)
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1090 <ntohl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1080 <bzero@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <ReadNBytes@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1217 <HandleRead+0xa7>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1223 <HandleRead+0xb3>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <ParseJob@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HandleRead>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1040 <ReadNBytes@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11fd <HandleRead+0x8d>
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    0x8(%rsp),%edi
call   1090 <ntohl@plt>
mov    %eax,0x8(%rsp)
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%esi
add    $0x1,%esi
mov    %rax,%rdi
call   1080 <bzero@plt>
mov    0x8(%rsp),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <ReadNBytes@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11f2 <HandleRead+0x82>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11fd <HandleRead+0x8d>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <ParseJob@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @HandleRead(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4 x i8]
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %3
store i32 0, i32* %4
%8 = load i32, i32* %3
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%10 = call i32 @ReadNBytes(i32 %8, i8* %9, i32 4)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* %4
store i32 %14, i32* %2
br label %43
15: ; preds = %1
store i32 0, i32* %6
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%17 = call i32 @memcpy(i32* %6, i8* %16, i32 4)
%18 = load i32, i32* %6
%19 = call i32 @ntohl(i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
%27 = call i32 @bzero(i8* %24, i32 %26)
%28 = load i32, i32* %3
%29 = load i8*, i8** %7
%30 = load i32, i32* %6
%31 = call i32 @ReadNBytes(i32 %28, i8* %29, i32 %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %15
%35 = load i8*, i8** %7
%36 = call i32 @free(i8* %35)
br label %41
37: ; preds = %15
%38 = load i32, i32* %3
%39 = load i8*, i8** %7
%40 = call i32 @ParseJob(i32 %38, i8* %39)
br label %41
41: ; preds = %37, %34
%42 = load i32, i32* %4
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %13
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
define i32 @HandleRead(i32 %0) {
%2 = alloca [4 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%5 = call i32 @ReadNBytes(i32 %0, i8* %4, i32 4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %26
7: ; preds = %1
%8 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%9 = call i32 @memcpy(i32* %3, i8* %4, i32 4)
%10 = load i32, i32* %3
%11 = call i32 @ntohl(i32 %10)
store i32 %11, i32* %3
%12 = add nsw i32 %11, 1
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
%17 = call i32 @bzero(i8* %14, i32 %16)
%18 = load i32, i32* %3
%19 = call i32 @ReadNBytes(i32 %0, i8* %14, i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %7
%22 = call i32 @free(i8* %14)
br label %25
23: ; preds = %7
%24 = call i32 @ParseJob(i32 %0, i8* %14)
br label %25
25: ; preds = %23, %21
br label %26
26: ; preds = %1, %25
%27 = phi i32 [ %19, %25 ], [ %5, %1 ]
ret i32 %27
}