#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*/ exit(int);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int receive_msg(int fd, char *buf) {
  int n, total = 0;
  do {
    n = recv(fd, &buf[total], 32768 - total - 1, 0);
    total += n;
    if (total >= 32768 - 1) {
      printf("Buffer overflow.\n");
      exit(1);
    }
  } while (n > 0);
  return total;
}


x86 assembly (clang, -O0):
<receive_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x8000,%edx
sub    -0x18(%rbp),%edx
sub    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x7fff,-0x18(%rbp)
jl     1195 <receive_msg+0x65>
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    119a <receive_msg+0x6a>
cmpl   $0x0,-0x14(%rbp)
jg     1146 <receive_msg+0x16>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_msg>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rsi
mov    $0x7fff,%edx
sub    %ebx,%edx
mov    %ebp,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
add    %eax,%ebx
cmp    $0x7fff,%ebx
jge    116c <receive_msg+0x3c>
test   %eax,%eax
jg     1140 <receive_msg+0x10>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe8d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"Buffer overflow.\0A\00"
define i32 @receive_msg(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %6
br label %7
7: ; preds = %26, %2
%8 = load i32, i32* %3
%9 = load i8*, i8** %4
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i32, i32* %6
%14 = sub nsw i32 32768, %13
%15 = sub nsw i32 %14, 1
%16 = call i32 @recv(i32 %8, i8* %12, i32 %15, i32 0)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = add nsw i32 %18, %17
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp sge i32 %20, 32767
br i1 %21, label %22, label %25
22: ; preds = %7
%23 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %7
br label %26
26: ; preds = %25
%27 = load i32, i32* %5
%28 = icmp sgt i32 %27, 0
br i1 %28, label %7, label %29
29: ; preds = %26
%30 = load i32, i32* %6
ret i32 %30
}

LLVM IR (-O2):
@str = [17 x i8] c"Buffer overflow.\00"
define i32 @receive_msg(i32 %0, i8* %1) {
br label %3
3: ; preds = %14, %2
%4 = phi i32 [ 0, %2 ], [ %9, %14 ]
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = sub i32 32767, %4
%8 = tail call i32 @recv(i32 %0, i8* %6, i32 %7, i32 0)
%9 = add nsw i32 %8, %4
%10 = icmp sgt i32 %9, 32766
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %3
%15 = icmp sgt i32 %8, 0
br i1 %15, label %3, label %16
16: ; preds = %14
ret i32 %9
}