#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 */
typedef int uint32_t;
typedef int ssize_t;
typedef int /*<<< orphan*/ size;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int read(int, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *read_response(int fd) {
  uint32_t size;
  if (read(fd, &size, sizeof(size)) != sizeof(size)) {
    fprintf(stderr,
            "line %d: "
            "failed to read msg length"
            "\n",
            93);
    return NULL;
  }
  char *out = malloc(size + 1);
  if (!out) {
    fprintf(stderr,
            "line %d: "
            "failed to allocate msg buffer"
            "\n",
            100);
    return NULL;
  }
  out[size] = '\0';
  uint32_t offset = 0;
  while (offset != size) {
    ssize_t bytes;
    if ((bytes = read(fd, &out[offset], size - offset)) < 0) {
      fprintf(stderr,
              "line %d: "
              "failed to read msg: %s"
              "\n",
              112, strerror(errno));
      free(out);
      return NULL;
    }
    offset += bytes;
  }
  return out;
}


x86 assembly (clang, -O0):
<read_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <read@plt>
cltq
cmp    $0x4,%rax
je     11a3 <read_response+0x53>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
mov    $0x5d,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    128b <read_response+0x13b>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e6 <read_response+0x96>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
mov    $0x64,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    128b <read_response+0x13b>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     1283 <read_response+0x133>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x10(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1275 <read_response+0x125>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rcx
lea    0xdf9(%rip),%rsi
mov    $0x70,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    128b <read_response+0x13b>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11f9 <read_response+0xa9>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_response>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <read@plt>
cmp    $0x4,%eax
jne    11c5 <read_response+0x75>
mov    0x4(%rsp),%ebx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11df <read_response+0x8f>
mov    %rax,%r14
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
test   %ebx,%ebx
je     11fe <read_response+0xae>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
sub    %eax,%ebx
mov    %r15d,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <read@plt>
test   %eax,%eax
js     120c <read_response+0xbc>
add    %eax,%ebp
mov    0x4(%rsp),%ebx
cmp    %ebp,%ebx
jne    11a0 <read_response+0x50>
jmp    11fe <read_response+0xae>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
xor    %r14d,%r14d
mov    $0x5d,%edx
jmp    11f7 <read_response+0xa7>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
xor    %r14d,%r14d
mov    $0x64,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dcd(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe22(%rip),%rsi
mov    %ebx,%edi
mov    $0x70,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    11fe <read_response+0xae>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"line %d: failed to read msg length\0A\00"
@.str.1 = [40 x i8] c"line %d: failed to allocate msg buffer\0A\00"
@.str.2 = [33 x i8] c"line %d: failed to read msg: %s\0A\00"
@errno = global i32 0
define i8* @read_response(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 (i32, ...) @read(i32 %8, i32* %4, i64 4)
%10 = sext i32 %9 to i64
%11 = icmp ne i64 %10, 4
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 93)
store i8* null, i8** %2
br label %57
15: ; preds = %1
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %24, label %21
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 100)
store i8* null, i8** %2
br label %57
24: ; preds = %15
%25 = load i8*, i8** %5
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
store i32 0, i32* %6
br label %29
29: ; preds = %51, %24
%30 = load i32, i32* %6
%31 = load i32, i32* %4
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %55
33: ; preds = %29
%34 = load i32, i32* %3
%35 = load i8*, i8** %5
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i32, i32* %4
%40 = load i32, i32* %6
%41 = sub nsw i32 %39, %40
%42 = call i32 (i32, ...) @read(i32 %34, i8* %38, i32 %41)
store i32 %42, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %51
44: ; preds = %33
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 112, i8* %47)
%49 = load i8*, i8** %5
%50 = call i32 @free(i8* %49)
store i8* null, i8** %2
br label %57
51: ; preds = %33
%52 = load i32, i32* %7
%53 = load i32, i32* %6
%54 = add nsw i32 %53, %52
store i32 %54, i32* %6
br label %29
55: ; preds = %29
%56 = load i8*, i8** %5
store i8* %56, i8** %2
br label %57
57: ; preds = %55, %44, %21, %12
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"line %d: failed to read msg length\0A\00"
@.str.1 = [40 x i8] c"line %d: failed to allocate msg buffer\0A\00"
@.str.2 = [33 x i8] c"line %d: failed to read msg: %s\0A\00"
@errno = global i32 0
define i8* @read_response(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 (i32, ...) @read(i32 %0, i32* %2, i64 4)
%5 = icmp eq i32 %4, 4
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 93)
br label %39
9: ; preds = %1
%10 = load i32, i32* %2
%11 = add nsw i32 %10, 1
%12 = call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 100)
br label %39
17: ; preds = %9
%18 = sext i32 %10 to i64
%19 = getelementptr inbounds i8, i8* %12, i64 %18
store i8 0, i8* %19
%20 = icmp eq i32 %10, 0
br i1 %20, label %39, label %24
21: ; preds = %24
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, %32
br i1 %23, label %39, label %24
24: ; preds = %17, %21
%25 = phi i32 [ %22, %21 ], [ %10, %17 ]
%26 = phi i32 [ %32, %21 ], [ 0, %17 ]
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %12, i64 %27
%29 = sub nsw i32 %25, %26
%30 = call i32 (i32, ...) @read(i32 %0, i8* %28, i32 %29)
%31 = icmp slt i32 %30, 0
%32 = add nsw i32 %30, %26
br i1 %31, label %33, label %21
33: ; preds = %24
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 112, i8* %36)
%38 = call i32 @free(i8* %12)
br label %39
39: ; preds = %21, %17, %33, %14, %6
%40 = phi i8* [ null, %6 ], [ null, %14 ], [ null, %33 ], [ %12, %17 ], [ %12, %21 ]
ret i8* %40
}