#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*/ memfd;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ PR_SET_DUMPABLE;
int /*<<< orphan*/ PR_SET_PDEATHSIG;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int prctl(int /*<<< orphan*/, int /*<<< orphan*/);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int __child(int sk, int memfd) {
  int ret;
  char buf;
  ret = prctl(PR_SET_PDEATHSIG, SIGKILL);
  if (ret) {
    fprintf(stderr, "%s: Child could not set DEATHSIG\n", strerror(errno));
    return -1;
  }
  ret = send(sk, &memfd, sizeof(memfd), 0);
  if (ret != sizeof(memfd)) {
    fprintf(stderr, "%s: Child failed to send fd number\n", strerror(errno));
    return -1;
  }
  while ((ret = recv(sk, &buf, sizeof(buf), 0)) > 0) {
    if (buf == 'P') {
      ret = prctl(PR_SET_DUMPABLE, 0);
      if (ret < 0) {
        fprintf(stderr, "%s: Child failed to disable ptrace\n",
                strerror(errno));
        return -1;
      }
    } else {
      fprintf(stderr, "Child received unknown command %c\n", buf);
      return -1;
    }
    ret = send(sk, &buf, sizeof(buf), 0);
    if (ret != 1) {
      fprintf(stderr, "%s: Child failed to ack\n", strerror(errno));
      return -1;
    }
  }
  if (ret < 0) {
    fprintf(stderr, "%s: Child failed to read from socket\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<__child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1060 <prctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11bc <__child+0x6c>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137c <__child+0x22c>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    $0x4,%rax
je     121c <__child+0xcc>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137c <__child+0x22c>
jmp    1221 <__child+0xd1>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    1331 <__child+0x1e1>
movsbl -0x11(%rbp),%eax
cmp    $0x50,%eax
jne    12a9 <__child+0x159>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <prctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12a4 <__child+0x154>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137c <__child+0x22c>
jmp    12d0 <__child+0x180>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
movsbl -0x11(%rbp),%edx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137c <__child+0x22c>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
je     132c <__child+0x1dc>
mov    0x2cdf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137c <__child+0x22c>
jmp    1221 <__child+0xd1>
cmpl   $0x0,-0x10(%rbp)
jge    1375 <__child+0x225>
mov    0x2c96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xd44(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137c <__child+0x22c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__child>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %esi,0xc(%rsp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1060 <prctl@plt>
test   %eax,%eax
je     119d <__child+0x4d>
mov    0x2e57(%rip),%rax
mov    (%rax),%ebx
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe68(%rip),%rsi
jmp    125f <__child+0x10f>
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
cmp    $0x4,%eax
jne    1241 <__child+0xf1>
lea    0xb(%rsp),%r14
mov    0x2e01(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    127a <__child+0x12a>
movsbl 0xb(%rsp),%edx
cmp    $0x50,%edx
jne    12a2 <__child+0x152>
mov    (%r15),%edi
xor    %esi,%esi
call   1060 <prctl@plt>
test   %eax,%eax
js     12bb <__child+0x16b>
mov    $0x1,%edx
mov    %ebx,%edi
mov    %r14,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
cmp    $0x1,%eax
je     11d0 <__child+0x80>
mov    0x2db0(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe4e(%rip),%rsi
jmp    125f <__child+0x10f>
mov    0x2d90(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdc3(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    1270 <__child+0x120>
mov    0x2d4f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe06(%rip),%rsi
jmp    125f <__child+0x10f>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    126b <__child+0x11b>
mov    0x2d16(%rip),%rax
mov    (%rax),%ebx
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xd6d(%rip),%rsi
jmp    125f <__child+0x10f>

LLVM IR (-O0):
@PR_SET_PDEATHSIG = global i32 0
@SIGKILL = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"%s: Child could not set DEATHSIG\0A\00"
@errno = global i32 0
@.str.1 = [36 x i8] c"%s: Child failed to send fd number\0A\00"
@PR_SET_DUMPABLE = global i32 0
@.str.2 = [36 x i8] c"%s: Child failed to disable ptrace\0A\00"
@.str.3 = [35 x i8] c"Child received unknown command %c\0A\00"
@.str.4 = [25 x i8] c"%s: Child failed to ack\0A\00"
@.str.5 = [38 x i8] c"%s: Child failed to read from socket\0A\00"
define i32 @__child(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* @PR_SET_PDEATHSIG
%9 = load i32, i32* @SIGKILL
%10 = call i32 @prctl(i32 %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %74
18: ; preds = %2
%19 = load i32, i32* %4
%20 = call i32 (i32, ...) @send(i32 %19, i32* %5, i64 4, i32 0)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = icmp ne i64 %22, 4
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 -1, i32* %3
br label %74
29: ; preds = %18
br label %30
30: ; preds = %64, %29
%31 = load i32, i32* %4
%32 = call i32 @recv(i32 %31, i8* %7, i32 1, i32 0)
store i32 %32, i32* %6
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %65
34: ; preds = %30
%35 = load i8, i8* %7
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 80
br i1 %37, label %38, label %49
38: ; preds = %34
%39 = load i32, i32* @PR_SET_DUMPABLE
%40 = call i32 @prctl(i32 %39, i32 0)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %46)
store i32 -1, i32* %3
br label %74
48: ; preds = %38
br label %54
49: ; preds = %34
%50 = load i32, i32* @stderr
%51 = load i8, i8* %7
%52 = sext i8 %51 to i32
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %52)
store i32 -1, i32* %3
br label %74
54: ; preds = %48
%55 = load i32, i32* %4
%56 = call i32 (i32, ...) @send(i32 %55, i8* %7, i64 1, i32 0)
store i32 %56, i32* %6
%57 = load i32, i32* %6
%58 = icmp ne i32 %57, 1
br i1 %58, label %59, label %64
59: ; preds = %54
%60 = load i32, i32* @stderr
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %62)
store i32 -1, i32* %3
br label %74
64: ; preds = %54
br label %30
65: ; preds = %30
%66 = load i32, i32* %6
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %65
%69 = load i32, i32* @stderr
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %71)
store i32 -1, i32* %3
br label %74
73: ; preds = %65
store i32 0, i32* %3
br label %74
74: ; preds = %73, %68, %59, %49, %43, %24, %13
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@PR_SET_PDEATHSIG = global i32 0
@SIGKILL = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"%s: Child could not set DEATHSIG\0A\00"
@errno = global i32 0
@.str.1 = [36 x i8] c"%s: Child failed to send fd number\0A\00"
@PR_SET_DUMPABLE = global i32 0
@.str.2 = [36 x i8] c"%s: Child failed to disable ptrace\0A\00"
@.str.3 = [35 x i8] c"Child received unknown command %c\0A\00"
@.str.4 = [25 x i8] c"%s: Child failed to ack\0A\00"
@.str.5 = [38 x i8] c"%s: Child failed to read from socket\0A\00"
define i32 @__child(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8
store i32 %1, i32* %3
%5 = load i32, i32* @PR_SET_PDEATHSIG
%6 = load i32, i32* @SIGKILL
%7 = tail call i32 @prctl(i32 %5, i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %56
14: ; preds = %2
%15 = call i32 (i32, ...) @send(i32 %0, i32* %3, i64 4, i32 0)
%16 = icmp eq i32 %15, 4
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %56
22: ; preds = %14, %41
%23 = call i32 @recv(i32 %0, i8* %4, i32 1, i32 0)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %49
25: ; preds = %22
%26 = load i8, i8* %4
%27 = icmp eq i8 %26, 80
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load i32, i32* @PR_SET_DUMPABLE
%30 = call i32 @prctl(i32 %29, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %56
37: ; preds = %25
%38 = sext i8 %26 to i32
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %56
41: ; preds = %28
%42 = call i32 (i32, ...) @send(i32 %0, i8* %4, i64 1, i32 0)
%43 = icmp eq i32 %42, 1
br i1 %43, label %22, label %44
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %47)
br label %56
49: ; preds = %22
%50 = icmp slt i32 %23, 0
br i1 %50, label %51, label %56
51: ; preds = %49
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %49, %51, %44, %37, %32, %17, %9
%57 = phi i32 [ -1, %9 ], [ -1, %17 ], [ -1, %32 ], [ -1, %44 ], [ -1, %37 ], [ -1, %51 ], [ 0, %49 ]
ret i32 %57
}