#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*/ SEEK_END;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(size_t);
size_t read(int, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
size_t receivebytes(int fd, char **msg) {
  char buff[10000];
  size_t len = 0;
  len = read(fd, buff, 10000);
  if (len < 1)
    return 0;
  if (len > 10000)
    lseek(fd, 0, SEEK_END);
  (*msg) = malloc((len + 1) * sizeof(char));
  strncpy((*msg), buff, len);
  (*msg)[len] = '\0';
  return len;
}


x86 assembly (clang, -O0):
<receivebytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2740,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x2738(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x2730(%rbp),%rsi
mov    $0x2710,%edx
call   1050 <read@plt>
mov    %rax,-0x2738(%rbp)
cmpq   $0x1,-0x2738(%rbp)
01
jae    1193 <receivebytes+0x53>
movq   $0x0,-0x8(%rbp)
jmp    120c <receivebytes+0xcc>
cmpq   $0x2710,-0x2738(%rbp)
jbe    11b7 <receivebytes+0x77>
mov    -0xc(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x2738(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    -0x2730(%rbp),%rsi
mov    -0x2738(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x2738(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2738(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2740,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receivebytes>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2710,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x2710,%edx
call   1050 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a4 <receivebytes+0x64>
cmp    $0x2711,%rbx
jb     1180 <receivebytes+0x40>
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rbx,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,(%r14)
mov    %rsp,%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
mov    (%r14),%rax
movb   $0x0,(%rax,%rbx,1)
mov    %rbx,%rax
add    $0x2710,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
define i64 @receivebytes(i32 %0, i8** %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10000 x i8]
%7 = alloca i64
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %7
%8 = load i32, i32* %4
%9 = getelementptr inbounds [10000 x i8], [10000 x i8]* %6, i64 0, i64 0
%10 = call i64 @read(i32 %8, i8* %9, i32 10000)
store i64 %10, i64* %7
%11 = load i64, i64* %7
%12 = icmp ult i64 %11, 1
br i1 %12, label %13, label %14
13: ; preds = %2
store i64 0, i64* %3
br label %37
14: ; preds = %2
%15 = load i64, i64* %7
%16 = icmp ugt i64 %15, 10000
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* %4
%19 = load i32, i32* @SEEK_END
%20 = call i32 @lseek(i32 %18, i32 0, i32 %19)
br label %21
21: ; preds = %17, %14
%22 = load i64, i64* %7
%23 = add i64 %22, 1
%24 = mul i64 %23, 1
%25 = call i8* @malloc(i64 %24)
%26 = load i8**, i8*** %5
store i8* %25, i8** %26
%27 = load i8**, i8*** %5
%28 = load i8*, i8** %27
%29 = getelementptr inbounds [10000 x i8], [10000 x i8]* %6, i64 0, i64 0
%30 = load i64, i64* %7
%31 = call i32 @strncpy(i8* %28, i8* %29, i64 %30)
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = load i64, i64* %7
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
%36 = load i64, i64* %7
store i64 %36, i64* %3
br label %37
37: ; preds = %21, %13
%38 = load i64, i64* %3
ret i64 %38
}

LLVM IR (-O2):
@SEEK_END = global i32 0
define i64 @receivebytes(i32 %0, i8** nocapture %1) {
%3 = alloca [10000 x i8]
%4 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 0
%5 = call i64 @read(i32 %0, i8* %4, i32 10000)
%6 = icmp eq i64 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = icmp ugt i64 %5, 10000
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @lseek(i32 %0, i32 0, i32 %10)
br label %12
12: ; preds = %9, %7
%13 = add i64 %5, 1
%14 = tail call i8* @malloc(i64 %13)
store i8* %14, i8** %1
%15 = call i32 @strncpy(i8* %14, i8* %4, i64 %5)
%16 = load i8*, i8** %1
%17 = getelementptr inbounds i8, i8* %16, i64 %5
store i8 0, i8* %17
br label %18
18: ; preds = %2, %12
ret i64 %5
}