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

/* Variables and functions */
int /*<<< orphan*/ close(int const);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int send(int const, ...);
int /*<<< orphan*/ stderr;
char *strerror(int);
int strlen(char const *);
int vmci_reply(const int client_socket, const char *reply) {
  int ret;
  int saved_errno;
  uint32_t b;
  if (reply == NULL) {
    reply = "OK";
  }
  b = 0xbadbeef;
  ret = send(client_socket, &b, sizeof(b), 0);
  if (ret != sizeof(b)) {
    saved_errno = errno;
    reply = "Failed to send magic";
    fprintf(stderr, "%s: ret %d (%s) expected size %d\n", reply, ret,
            strerror(errno), sizeof(b));
    goto failed;
  }
  b = strlen(reply) + 1;
  ret = send(client_socket, &b, sizeof(b), 0);
  if (ret != sizeof(b)) {
    saved_errno = errno;
    reply = "Failed to send len";
    fprintf(stderr, "%s: ret %d (%s) expected size %d\n", reply, ret,
            strerror(errno), sizeof(b));
    goto failed;
  }
  ret = send(client_socket, reply, b, 0);
  if (b != ret) {
    saved_errno = errno;
    fprintf(stderr, "Failed to send content: ret %d (%s) expected size %d\n",
            ret, strerror(errno), b);
    goto failed;
  }
  close(client_socket);
  return (0);
failed:
  close(client_socket);
  errno = saved_errno;
  return (-1);
}


x86 assembly (clang, -O0):
<vmci_reply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <vmci_reply+0x25>
lea    0xe8f(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0xbadbeef,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
je     1207 <vmci_reply+0xb7>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe4e(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%ecx
mov    %rax,%r8
lea    0xe23(%rip),%rsi
mov    $0x4,%r9d
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1323 <vmci_reply+0x1d3>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
je     12a1 <vmci_reply+0x151>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xdeb(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    %rax,%r8
lea    0xd89(%rip),%rsi
mov    $0x4,%r9d
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1323 <vmci_reply+0x1d3>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     130f <vmci_reply+0x1bf>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%edx
mov    %rax,%rcx
mov    -0x1c(%rbp),%r8d
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1323 <vmci_reply+0x1d3>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    133e <vmci_reply+0x1ee>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%ecx
mov    0x2ca3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmci_reply>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
test   %rsi,%rsi
lea    0xe9c(%rip),%rbp
cmovne %rsi,%rbp
movl   $0xbadbeef,0x4(%rsp)
0b
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
cmp    $0x4,%eax
jne    11de <vmci_reply+0x8e>
mov    %rbp,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
mov    %r14d,%edi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
cmp    $0x4,%eax
jne    120a <vmci_reply+0xba>
mov    0x4(%rsp),%edx
xor    %ebx,%ebx
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
cmp    %eax,0x4(%rsp)
jne    126c <vmci_reply+0x11c>
mov    %r14d,%edi
call   1050 <close@plt>
jmp    125f <vmci_reply+0x10f>
mov    %eax,%ebx
mov    0x2df1(%rip),%rax
mov    (%rax),%ebp
mov    0x2df0(%rip),%rax
mov    (%rax),%r15d
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xe17(%rip),%rsi
lea    0xdfb(%rip),%rdx
jmp    1234 <vmci_reply+0xe4>
mov    %eax,%ebx
mov    0x2dc5(%rip),%rax
mov    (%rax),%ebp
mov    0x2dc4(%rip),%rax
mov    (%rax),%r15d
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xdeb(%rip),%rsi
lea    0xe06(%rip),%rdx
mov    $0x4,%r9d
mov    %r15d,%edi
mov    %ebx,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    0x2d80(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r15d
mov    0x2d62(%rip),%rax
mov    (%rax),%ebp
mov    0x2d61(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1070 <strerror@plt>
mov    0x4(%rsp),%r8d
lea    0xdb9(%rip),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1249 <vmci_reply+0xf9>

LLVM IR (-O0):
@.str = [3 x i8] c"OK\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Failed to send magic\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"%s: ret %d (%s) expected size %d\0A\00"
@.str.3 = [19 x i8] c"Failed to send len\00"
@.str.4 = [54 x i8] c"Failed to send content: ret %d (%s) expected size %d\0A\00"
define i32 @vmci_reply(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %5
br label %12
12: ; preds = %11, %2
store i32 195935983, i32* %8
%13 = load i32, i32* %4
%14 = call i32 (i32, ...) @send(i32 %13, i32* %8, i64 4, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = icmp ne i64 %16, 4
br i1 %17, label %18, label %26
18: ; preds = %12
%19 = load i32, i32* @errno
store i32 %19, i32* %7
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8** %5
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %21, i32 %22, i8* %24, i64 4)
br label %62
26: ; preds = %12
%27 = load i8*, i8** %5
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
%30 = load i32, i32* %4
%31 = call i32 (i32, ...) @send(i32 %30, i32* %8, i64 4, i32 0)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = icmp ne i64 %33, 4
br i1 %34, label %35, label %43
35: ; preds = %26
%36 = load i32, i32* @errno
store i32 %36, i32* %7
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8** %5
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %5
%39 = load i32, i32* %6
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %38, i32 %39, i8* %41, i64 4)
br label %62
43: ; preds = %26
%44 = load i32, i32* %4
%45 = load i8*, i8** %5
%46 = load i32, i32* %8
%47 = call i32 (i32, ...) @send(i32 %44, i8* %45, i32 %46, i32 0)
store i32 %47, i32* %6
%48 = load i32, i32* %8
%49 = load i32, i32* %6
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %59
51: ; preds = %43
%52 = load i32, i32* @errno
store i32 %52, i32* %7
%53 = load i32, i32* @stderr
%54 = load i32, i32* %6
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = load i32, i32* %8
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i32 %54, i8* %56, i32 %57)
br label %62
59: ; preds = %43
%60 = load i32, i32* %4
%61 = call i32 @close(i32 %60)
store i32 0, i32* %3
br label %66
62: ; preds = %51, %35, %18
%63 = load i32, i32* %4
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %7
store i32 %65, i32* @errno
store i32 -1, i32* %3
br label %66
66: ; preds = %62, %59
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [3 x i8] c"OK\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Failed to send magic\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"%s: ret %d (%s) expected size %d\0A\00"
@.str.3 = [19 x i8] c"Failed to send len\00"
@.str.4 = [54 x i8] c"Failed to send content: ret %d (%s) expected size %d\0A\00"
define i32 @vmci_reply(i32 %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i8* %1, null
%6 = select i1 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1
store i32 195935983, i32* %3
%7 = call i32 (i32, ...) @send(i32 %0, i32* %3, i64 4, i32 0)
%8 = icmp eq i32 %7, 4
br i1 %8, label %14, label %9
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = load i32, i32* @stderr
%12 = call i8* @strerror(i32 %10)
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %7, i8* %12, i64 4)
br label %37
14: ; preds = %2
%15 = call i32 @strlen(i8* %6)
%16 = add nsw i32 %15, 1
store i32 %16, i32* %3
%17 = call i32 (i32, ...) @send(i32 %0, i32* %3, i64 4, i32 0)
%18 = icmp eq i32 %17, 4
br i1 %18, label %24, label %19
19: ; preds = %14
%20 = load i32, i32* @errno
%21 = load i32, i32* @stderr
%22 = call i8* @strerror(i32 %20)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %17, i8* %22, i64 4)
br label %37
24: ; preds = %14
%25 = load i32, i32* %3
%26 = call i32 (i32, ...) @send(i32 %0, i8* %6, i32 %25, i32 0)
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, %26
br i1 %28, label %35, label %29
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = load i32, i32* @stderr
%32 = call i8* @strerror(i32 %30)
%33 = load i32, i32* %3
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i32 %26, i8* %32, i32 %33)
br label %37
35: ; preds = %24
%36 = call i32 @close(i32 %0)
br label %40
37: ; preds = %29, %19, %9
%38 = phi i32 [ %10, %9 ], [ %20, %19 ], [ %30, %29 ]
%39 = call i32 @close(i32 %0)
store i32 %38, i32* @errno
br label %40
40: ; preds = %37, %35
%41 = phi i32 [ -1, %37 ], [ 0, %35 ]
ret i32 %41
}