#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;

/* Variables and functions */
int _read_bytes(int, char *, int);
char *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int sscanf(char *, char *, int *);
char *_recv_msg(int fd) {
  char header[10];
  uint32_t size;
  char *buf;
  if (_read_bytes(fd, header, 9) != 9) {
    perror("reading message header");
    exit(1);
  }
  if (sscanf(header, "%ul", &size) != 1) {
    perror("parsing message header");
    exit(1);
  }
  buf = calloc(1, (size + 1));
  if (buf == NULL) {
    perror("malloc");
    exit(1);
  }
  if (_read_bytes(fd, buf, size) != size) {
    perror("reading message");
    exit(1);
  }
  return buf;
}


x86 assembly (clang, -O0):
<_recv_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x9,%edx
call   1030 <_read_bytes@plt>
cmp    $0x9,%eax
je     118b <_recv_msg+0x3b>
lea    0xe84(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0xe(%rbp),%rdi
lea    0xe81(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
je     11be <_recv_msg+0x6e>
lea    0xe6c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f3 <_recv_msg+0xa3>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <_read_bytes@plt>
cmp    -0x14(%rbp),%eax
je     1221 <_recv_msg+0xd1>
lea    0xe27(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_recv_msg>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
lea    0x6(%rsp),%rsi
mov    $0x9,%edx
call   1030 <_read_bytes@plt>
cmp    $0x9,%eax
jne    11be <_recv_msg+0x6e>
lea    0xea1(%rip),%rsi
lea    0x6(%rsp),%rdi
mov    %rsp,%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11c7 <_recv_msg+0x77>
mov    (%rsp),%ebx
lea    0x1(%rbx),%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
test   %rax,%rax
je     11d0 <_recv_msg+0x80>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1030 <_read_bytes@plt>
cmp    (%rsp),%eax
jne    11d9 <_recv_msg+0x89>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe3b(%rip),%rdi
jmp    11e0 <_recv_msg+0x90>
lea    0xe4d(%rip),%rdi
jmp    11e0 <_recv_msg+0x90>
lea    0xe5b(%rip),%rdi
jmp    11e0 <_recv_msg+0x90>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"reading message header\00"
@.str.1 = [4 x i8] c"%ul\00"
@.str.2 = [23 x i8] c"parsing message header\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [16 x i8] c"reading message\00"
define i8* @_recv_msg(i32 %0) {
%2 = alloca i32
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%8 = call i32 @_read_bytes(i32 %6, i8* %7, i32 9)
%9 = icmp ne i32 %8, 9
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
%23 = call i8* @calloc(i32 1, i32 %22)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = load i32, i32* %2
%31 = load i8*, i8** %5
%32 = load i32, i32* %4
%33 = call i32 @_read_bytes(i32 %30, i8* %31, i32 %32)
%34 = load i32, i32* %4
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %29
%40 = load i8*, i8** %5
ret i8* %40
}

LLVM IR (-O2):
@.str = [23 x i8] c"reading message header\00"
@.str.1 = [4 x i8] c"%ul\00"
@.str.2 = [23 x i8] c"parsing message header\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [16 x i8] c"reading message\00"
define i8* @_recv_msg(i32 %0) {
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = call i32 @_read_bytes(i32 %0, i8* %4, i32 9)
%7 = icmp eq i32 %6, 9
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%13 = icmp eq i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %11
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
%20 = call i8* @calloc(i32 1, i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = call i32 @_read_bytes(i32 %0, i8* %20, i32 %18)
%27 = load i32, i32* %3
%28 = icmp eq i32 %26, %27
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
ret i8* %20
}