#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 */

/* Variables and functions */
int __child(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int sys_memfd_create(char *, int /*<<< orphan*/);
int child(int sk) {
  int memfd, ret;
  memfd = sys_memfd_create("test", 0);
  if (memfd < 0) {
    fprintf(stderr, "%s: Child could not create memfd\n", strerror(errno));
    ret = -1;
  } else {
    ret = __child(sk, memfd);
    close(memfd);
  }
  close(sk);
  return ret;
}


x86 assembly (clang, -O0):
<child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe9e(%rip),%rdi
xor    %esi,%esi
call   1050 <sys_memfd_create@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ae <child+0x5e>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
lea    0xe68(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    11c4 <child+0x74>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1060 <__child@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
lea    0xea2(%rip),%rdi
xor    %esi,%esi
call   1050 <sys_memfd_create@plt>
test   %eax,%eax
js     1180 <child+0x30>
mov    %eax,%ebx
mov    %r14d,%edi
mov    %eax,%esi
call   1060 <__child@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
jmp    11ad <child+0x5d>
mov    0x2e59(%rip),%rax
mov    (%rax),%ebx
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe67(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %r14d,%edi
call   1030 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"test\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"%s: Child could not create memfd\0A\00"
@errno = global i32 0
define i32 @child(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @sys_memfd_create(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %11)
store i32 -1, i32* %4
br label %19
13: ; preds = %1
%14 = load i32, i32* %2
%15 = load i32, i32* %3
%16 = call i32 @__child(i32 %14, i32 %15)
store i32 %16, i32* %4
%17 = load i32, i32* %3
%18 = call i32 @close(i32 %17)
br label %19
19: ; preds = %13, %8
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %4
ret i32 %22
}

LLVM IR (-O2):
@.str = [5 x i8] c"test\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"%s: Child could not create memfd\0A\00"
@errno = global i32 0
define i32 @child(i32 %0) {
%2 = tail call i32 @sys_memfd_create(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %12
9: ; preds = %1
%10 = tail call i32 @__child(i32 %0, i32 %2)
%11 = tail call i32 @close(i32 %2)
br label %12
12: ; preds = %9, %4
%13 = phi i32 [ -1, %4 ], [ %10, %9 ]
%14 = tail call i32 @close(i32 %0)
ret i32 %13
}