#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 /*<<< orphan*/ size;
typedef int /*<<< orphan*/ op;

/* Variables and functions */
int MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, unsigned int);
int /*<<< orphan*/ memdlopen(unsigned int, void *);
void *mmap(int /*<<< orphan*/ *, unsigned int, int, int, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ readall(int, void *, unsigned int);
void receive_and_exec_code(int s) {
  unsigned int size;
  void (*f)(void);
  int prot;
  char op;
  void *p;
  readall(s, &op, sizeof(op));
  readall(s, &size, sizeof(size));
  if (size > 65536)
    errx(1, "size too large: %d", size);
  prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  p = mmap(NULL, size, prot, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  if (p == MAP_FAILED)
    err(1, "mmap");
  readall(s, p, size);
  switch (op) {
  case '0':
    f = p;
    f();
    break;
  case '1':
    memdlopen(size, p);
    break;
  default:
    break;
  }
  _exit(0);
}


x86 assembly (clang, -O0):
<receive_and_exec_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1080 <readall@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1080 <readall@plt>
cmpl   $0x10000,-0x8(%rbp)
jbe    11ae <receive_and_exec_code+0x4e>
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
lea    0xe57(%rip),%rsi
call   1050 <errx@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    0x2e0a(%rip),%rcx
or     (%rcx),%eax
mov    0x2e11(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    0x2e07(%rip),%rax
mov    (%rax),%ecx
mov    0x2dde(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%rax
jne    121f <receive_and_exec_code+0xbf>
mov    $0x1,%edi
lea    0xdf9(%rip),%rsi
call   1060 <err@plt>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1080 <readall@plt>
movsbl -0x15(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x30,%eax
je     1254 <receive_and_exec_code+0xf4>
jmp    1243 <receive_and_exec_code+0xe3>
mov    -0x24(%rbp),%eax
sub    $0x31,%eax
je     1264 <receive_and_exec_code+0x104>
jmp    1275 <receive_and_exec_code+0x115>
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
call   *-0x10(%rbp)
jmp    127a <receive_and_exec_code+0x11a>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1070 <memdlopen@plt>
jmp    127a <receive_and_exec_code+0x11a>
jmp    127a <receive_and_exec_code+0x11a>
xor    %edi,%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<receive_and_exec_code>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1080 <readall@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1080 <readall@plt>
mov    0x4(%rsp),%eax
cmp    $0x10001,%eax
jb     11a7 <receive_and_exec_code+0x47>
lea    0xe69(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1050 <errx@plt>
mov    0x4(%rsp),%eax
mov    0x2e4a(%rip),%rcx
mov    0x2e13(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e18(%rip),%rcx
or     (%rcx),%edx
mov    0x2e17(%rip),%rsi
mov    0x2df0(%rip),%rcx
mov    (%rcx),%ecx
or     (%rsi),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2dc8(%rip),%rax
cmp    (%rax),%rbx
jne    1206 <receive_and_exec_code+0xa6>
lea    0xe17(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0x4(%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <readall@plt>
movsbl 0x3(%rsp),%eax
cmp    $0x31,%eax
je     122c <receive_and_exec_code+0xcc>
cmp    $0x30,%eax
jne    1238 <receive_and_exec_code+0xd8>
call   *%rbx
xor    %edi,%edi
call   1030 <_exit@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
call   1070 <memdlopen@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"size too large: %d\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define void @receive_and_exec_code(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca void ()*
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8*
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = call i32 @readall(i32 %8, i8* %6, i32 1)
%10 = load i32, i32* %2
%11 = bitcast i32* %3 to i8*
%12 = call i32 @readall(i32 %10, i8* %11, i32 4)
%13 = load i32, i32* %3
%14 = icmp ugt i32 %13, 65536
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i32, i32* %3
%17 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %15, %1
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @PROT_EXEC
%23 = or i32 %21, %22
store i32 %23, i32* %5
%24 = load i32, i32* %3
%25 = load i32, i32* %5
%26 = load i32, i32* @MAP_ANONYMOUS
%27 = load i32, i32* @MAP_PRIVATE
%28 = or i32 %26, %27
%29 = call i8* @mmap(i32* null, i32 %24, i32 %25, i32 %28, i32 -1, i32 0)
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %33, label %35
33: ; preds = %18
%34 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %18
%36 = load i32, i32* %2
%37 = load i8*, i8** %7
%38 = load i32, i32* %3
%39 = call i32 @readall(i32 %36, i8* %37, i32 %38)
%40 = load i8, i8* %6
%41 = sext i8 %40 to i32
switch i32 %41, label %50 [
i32 48, label %42
i32 49, label %46
]
42: ; preds = %35
%43 = load i8*, i8** %7
%44 = bitcast i8* %43 to void ()*
store void ()* %44, void ()** %4
%45 = load void ()*, void ()** %4
br label %51
46: ; preds = %35
%47 = load i32, i32* %3
%48 = load i8*, i8** %7
%49 = call i32 @memdlopen(i32 %47, i8* %48)
br label %51
50: ; preds = %35
br label %51
51: ; preds = %50, %46, %42
%52 = call i32 @_exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [19 x i8] c"size too large: %d\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define void @receive_and_exec_code(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = bitcast i32* %2 to i8*
%5 = call i32 @readall(i32 %0, i8* %3, i32 1)
%6 = call i32 @readall(i32 %0, i8* %4, i32 4)
%7 = load i32, i32* %2
%8 = icmp ugt i32 %7, 65536
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
%11 = load i32, i32* %2
br label %12
12: ; preds = %9, %1
%13 = phi i32 [ %11, %9 ], [ %7, %1 ]
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @PROT_EXEC
%18 = or i32 %16, %17
%19 = load i32, i32* @MAP_ANONYMOUS
%20 = load i32, i32* @MAP_PRIVATE
%21 = or i32 %20, %19
%22 = call i8* @mmap(i32* null, i32 %13, i32 %18, i32 %21, i32 -1, i32 0)
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %27
25: ; preds = %12
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %12
%28 = load i32, i32* %2
%29 = call i32 @readall(i32 %0, i8* %22, i32 %28)
%30 = load i8, i8* %3
%31 = sext i8 %30 to i32
switch i32 %31, label %37 [
i32 48, label %32
i32 49, label %34
]
32: ; preds = %27
%33 = bitcast i8* %22 to void ()*
br label %37
34: ; preds = %27
%35 = load i32, i32* %2
%36 = call i32 @memdlopen(i32 %35, i8* %22)
br label %37
37: ; preds = %27, %34, %32
%38 = call i32 @_exit(i32 0)
unreachable
}