int get_two_vars(int sock, char *out, int len) {
  char buf1[512], buf2[512];
  unsigned int size1, size2;
  int size;
  if (recv(sock, buf1, sizeof(buf1), 0) < 0) {
    return -1;
  }
  if (recv(sock, buf2, sizeof(buf2), 0) < 0) {
    return -1;
  }
  memcpy(&size1, buf1, sizeof(int));
  memcpy(&size2, buf2, sizeof(int));
  size = size1 + size2;
  if (size > len) {
    return -1;
  }
  memcpy(out, buf1, size1);
  memcpy(out + size1, buf2, size2);
  return size;
}


x86 assembly (clang, -O0):
<get_two_vars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1030 <recv@plt>
cmp    $0x0,%eax
jge    1162 <get_two_vars+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <get_two_vars+0xf6>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1030 <recv@plt>
cmp    $0x0,%eax
jge    118f <get_two_vars+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <get_two_vars+0xf6>
mov    -0x220(%rbp),%eax
mov    %eax,-0x424(%rbp)
mov    -0x420(%rbp),%eax
mov    %eax,-0x428(%rbp)
mov    -0x424(%rbp),%eax
add    -0x428(%rbp),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11d4 <get_two_vars+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <get_two_vars+0xf6>
mov    -0x10(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    -0x424(%rbp),%eax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x424(%rbp),%eax
add    %rax,%rdi
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%eax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x42c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_two_vars>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x200(%rsp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1030 <recv@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11aa <get_two_vars+0x8a>
mov    %rsp,%rsi
mov    $0x200,%edx
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %eax,%eax
call   1030 <recv@plt>
test   %eax,%eax
js     11aa <get_two_vars+0x8a>
mov    0x200(%rsp),%ebx
mov    (%rsp),%r12d
lea    (%r12,%rbx,1),%r13d
cmp    %r14d,%r13d
jg     11aa <get_two_vars+0x8a>
lea    0x200(%rsp),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
add    %rbx,%r15
mov    %rsp,%rsi
mov    %r15,%rdi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %r13d,%ebp
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_two_vars(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca [512 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = load i32, i32* %5
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%15 = call i32 (i32, i8*, i64, i32, ...) bitcast (i32 (...)* @recv to i32 (i32, i8*, i64, i32, ...)*)(i32 %13, i8* %14, i64 512, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %49
18: ; preds = %3
%19 = load i32, i32* %5
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%21 = call i32 (i32, i8*, i64, i32, ...) bitcast (i32 (...)* @recv to i32 (i32, i8*, i64, i32, ...)*)(i32 %19, i8* %20, i64 512, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -1, i32* %4
br label %49
24: ; preds = %18
%25 = bitcast i32* %10 to i8*
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%27 = bitcast i32* %11 to i8*
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%29 = load i32, i32* %10
%30 = load i32, i32* %11
%31 = add i32 %29, %30
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = load i32, i32* %7
%34 = icmp sgt i32 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %24
store i32 -1, i32* %4
br label %49
36: ; preds = %24
%37 = load i8*, i8** %6
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%39 = load i32, i32* %10
%40 = zext i32 %39 to i64
%41 = load i8*, i8** %6
%42 = load i32, i32* %10
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* %11
%47 = zext i32 %46 to i64
%48 = load i32, i32* %12
store i32 %48, i32* %4
br label %49
49: ; preds = %36, %35, %23, %17
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
define i32 @get_two_vars(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = call i32 (i32, i8*, i64, i32, ...) bitcast (i32 (...)* @recv to i32 (i32, i8*, i64, i32, ...)*)(i32 %0, i8* %6, i64 512, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %3
%11 = call i32 (i32, i8*, i64, i32, ...) bitcast (i32 (...)* @recv to i32 (i32, i8*, i64, i32, ...)*)(i32 %0, i8* %7, i64 512, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %10
%14 = bitcast [512 x i8]* %4 to i32*
%15 = load i32, i32* %14
%16 = bitcast [512 x i8]* %5 to i32*
%17 = load i32, i32* %16
%18 = add i32 %17, %15
%19 = icmp sgt i32 %18, %2
br i1 %19, label %24, label %20
20: ; preds = %13
%21 = zext i32 %15 to i64
%22 = getelementptr inbounds i8, i8* %1, i64 %21
%23 = zext i32 %17 to i64
br label %24
24: ; preds = %13, %10, %3, %20
%25 = phi i32 [ %18, %20 ], [ -1, %3 ], [ -1, %10 ], [ -1, %13 ]
ret i32 %25
}