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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *, ...);
int readU32LittleEndian(int /*<<< orphan*/ *);
int /*<<< orphan*/ receiveByte(int);
uint8_t *receivePacket(int fd) {
  uint8_t packet_header[5];
  uint8_t *packet_buffer;
  uint32_t length;
  uint32_t i;
  for (i = 0; i < 5; i++) {
    packet_header[i] = receiveByte(fd);
  }
  length = readU32LittleEndian(&(packet_header[1]));
  if (length > 1000000) {
    printf("Got absurdly large packet length of %d\n", length);
    printf("Exiting, since the packet is probably garbled\n");
    exit(1);
  }
  packet_buffer = malloc(length + 5);
  memcpy(packet_buffer, packet_header, 5);
  for (i = 0; i < length; i++) {
    packet_buffer[i + 5] = receiveByte(fd);
  }
  return packet_buffer;
}


x86 assembly (clang, -O0):
<receivePacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x5,-0x30(%rbp)
jge    119c <receivePacket+0x3c>
mov    -0x4(%rbp),%edi
call   1070 <receiveByte@plt>
mov    %eax,%ecx
movslq -0x30(%rbp),%rax
mov    %ecx,-0x20(%rbp,%rax,4)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1172 <receivePacket+0x12>
lea    -0x20(%rbp),%rdi
add    $0x4,%rdi
call   1050 <readU32LittleEndian@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xf4240,-0x2c(%rbp)
jle    11e2 <receivePacket+0x82>
mov    -0x2c(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x2c(%rbp),%edi
add    $0x5,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x5,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    123e <receivePacket+0xde>
mov    -0x4(%rbp),%edi
call   1070 <receiveByte@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%ecx
add    $0x5,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    120a <receivePacket+0xaa>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receivePacket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r15d
call   1080 <receiveByte@plt>
mov    %eax,(%rsp)
mov    %r15d,%edi
call   1080 <receiveByte@plt>
lea    0x4(%rsp),%rbx
mov    %eax,0x4(%rsp)
mov    %r15d,%edi
call   1080 <receiveByte@plt>
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1080 <receiveByte@plt>
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
call   1080 <receiveByte@plt>
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   1060 <readU32LittleEndian@plt>
mov    %eax,%r14d
cmp    $0xf4241,%eax
jge    1226 <receivePacket+0xb6>
lea    0x5(%r14),%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rsp,%rsi
mov    %rax,%rdi
mov    $0x5,%edx
call   1050 <memcpy@plt>
test   %r14d,%r14d
jle    1216 <receivePacket+0xa6>
mov    %r14d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
call   1080 <receiveByte@plt>
mov    %eax,0x14(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1200 <receivePacket+0x90>
mov    %r12,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd3(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdea(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"Got absurdly large packet length of %d\0A\00"
@.str.1 = [47 x i8] c"Exiting, since the packet is probably garbled\0A\00"
define i32* @receivePacket(i32 %0) {
%2 = alloca i32
%3 = alloca [5 x i32]
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %6
br label %7
7: ; preds = %16, %1
%8 = load i32, i32* %6
%9 = icmp slt i32 %8, 5
br i1 %9, label %10, label %19
10: ; preds = %7
%11 = load i32, i32* %2
%12 = call i32 @receiveByte(i32 %11)
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 %14
store i32 %12, i32* %15
br label %16
16: ; preds = %10
%17 = load i32, i32* %6
%18 = add nsw i32 %17, 1
store i32 %18, i32* %6
br label %7
19: ; preds = %7
%20 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 1
%21 = call i32 @readU32LittleEndian(i32* %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 1000000
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* %5
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 5
%32 = call i32* @malloc(i32 %31)
store i32* %32, i32** %4
%33 = load i32*, i32** %4
%34 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 0
%35 = call i32 @memcpy(i32* %33, i32* %34, i32 5)
store i32 0, i32* %6
br label %36
36: ; preds = %48, %29
%37 = load i32, i32* %6
%38 = load i32, i32* %5
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %51
40: ; preds = %36
%41 = load i32, i32* %2
%42 = call i32 @receiveByte(i32 %41)
%43 = load i32*, i32** %4
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %43, i64 %46
store i32 %42, i32* %47
br label %48
48: ; preds = %40
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %36
51: ; preds = %36
%52 = load i32*, i32** %4
ret i32* %52
}

LLVM IR (-O2):
@.str = [40 x i8] c"Got absurdly large packet length of %d\0A\00"
@str = [46 x i8] c"Exiting, since the packet is probably garbled\00"
define i32* @receivePacket(i32 %0) {
%2 = alloca [5 x i32]
%3 = bitcast [5 x i32]* %2 to i8*
%4 = tail call i32 @receiveByte(i32 %0)
%5 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 0
store i32 %4, i32* %5
%6 = tail call i32 @receiveByte(i32 %0)
%7 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 1
store i32 %6, i32* %7
%8 = tail call i32 @receiveByte(i32 %0)
%9 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 2
store i32 %8, i32* %9
%10 = tail call i32 @receiveByte(i32 %0)
%11 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 3
store i32 %10, i32* %11
%12 = tail call i32 @receiveByte(i32 %0)
%13 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 4
store i32 %12, i32* %13
%14 = call i32 @readU32LittleEndian(i32* %7)
%15 = icmp sgt i32 %14, 1000000
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %14)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = add nsw i32 %14, 5
%22 = call i32* @malloc(i32 %21)
%23 = call i32 @memcpy(i32* %22, i32* %5, i32 5)
%24 = icmp sgt i32 %14, 0
br i1 %24, label %25, label %34
25: ; preds = %20
%26 = zext i32 %14 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ 0, %25 ], [ %32, %27 ]
%29 = call i32 @receiveByte(i32 %0)
%30 = add nuw nsw i64 %28, 5
%31 = getelementptr inbounds i32, i32* %22, i64 %30
store i32 %29, i32* %31
%32 = add nuw nsw i64 %28, 1
%33 = icmp eq i64 %32, %26
br i1 %33, label %34, label %27
34: ; preds = %27, %20
ret i32* %22
}