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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ memcmp(char *, char *, int);
int read_all(int, char *, int);
int /*<<< orphan*/ stderr;
int receive_and_validate_session_key(char *expected_key, int sock) {
  char received_key[(32) + 1];
  received_key[(32)] = '\0';
  ssize_t got = read_all(sock, received_key, (32));
  if (got < 0) {
    return -1;
  }
  if (got < (32)) {
    fprintf(stderr, "Did not receive full-length key.\n");
    return -1;
  }
  if (memcmp(expected_key, received_key, (32)) != 0) {
    fprintf(stderr, "Received incorrect secret key.\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<receive_and_validate_session_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movb   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1050 <read_all@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    116d <receive_and_validate_session_key+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <receive_and_validate_session_key+0xac>
cmpl   $0x20,-0x44(%rbp)
jge    1198 <receive_and_validate_session_key+0x68>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <receive_and_validate_session_key+0xac>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
je     11d5 <receive_and_validate_session_key+0xa5>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <receive_and_validate_session_key+0xac>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_and_validate_session_key>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%eax
mov    %rdi,%r14
movb   $0x0,0x20(%rsp)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x20,%edx
call   1050 <read_all@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1195 <receive_and_validate_session_key+0x65>
cmp    $0x20,%eax
jae    1167 <receive_and_validate_session_key+0x37>
lea    0xe9b(%rip),%rsi
jmp    1183 <receive_and_validate_session_key+0x53>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x20,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
je     1193 <receive_and_validate_session_key+0x63>
lea    0xe9f(%rip),%rsi
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1040 <fprintf@plt>
jmp    1195 <receive_and_validate_session_key+0x65>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"Did not receive full-length key.\0A\00"
@.str.1 = [32 x i8] c"Received incorrect secret key.\0A\00"
define i32 @receive_and_validate_session_key(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [33 x i8]
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 32
store i8 0, i8* %8
%9 = load i32, i32* %5
%10 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%11 = call i32 @read_all(i32 %9, i8* %10, i32 32)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %30
15: ; preds = %2
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 32
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %30
21: ; preds = %15
%22 = load i8*, i8** %4
%23 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%24 = call i64 @memcmp(i8* %22, i8* %23, i32 32)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %30
29: ; preds = %21
store i32 0, i32* %3
br label %30
30: ; preds = %29, %26, %18, %14
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"Did not receive full-length key.\0A\00"
@.str.1 = [32 x i8] c"Received incorrect secret key.\0A\00"
define i32 @receive_and_validate_session_key(i8* %0, i32 %1) {
%3 = alloca [33 x i8]
%4 = getelementptr inbounds [33 x i8], [33 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [33 x i8], [33 x i8]* %3, i64 0, i64 32
store i8 0, i8* %5
%6 = call i32 @read_all(i32 %1, i8* %4, i32 32)
%7 = icmp slt i32 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %2
%9 = icmp ult i32 %6, 32
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = call i64 @memcmp(i8* %0, i8* %4, i32 32)
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10, %8
%14 = phi i8* [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), %8 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %10 ]
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* %14)
br label %17
17: ; preds = %13, %10, %2
%18 = phi i32 [ -1, %2 ], [ 0, %10 ], [ -1, %13 ]
ret i32 %18
}