#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 scalar_t__ loff_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
int ftruncate(int, scalar_t__);
int /*<<< orphan*/ printf(char *, ...);
int sys_memfd_create(char const *, unsigned int);
int mfd_assert_new(const char *name, loff_t sz, unsigned int flags) {
  int r, fd;
  fd = sys_memfd_create(name, flags);
  if (fd < 0) {
    printf("memfd_create(\"%s\", %u) failed: %m\n", name, flags);
    abort();
  }
  r = ftruncate(fd, sz);
  if (r < 0) {
    printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
    abort();
  }
  return fd;
}


x86 assembly (clang, -O0):
<mfd_assert_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <sys_memfd_create@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1188 <mfd_assert_new+0x48>
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <ftruncate@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ba <mfd_assert_new+0x7a>
mov    -0x10(%rbp),%rsi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfd_assert_new>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %edx,%esi
call   1060 <sys_memfd_create@plt>
test   %eax,%eax
js     1178 <mfd_assert_new+0x38>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <ftruncate@plt>
test   %eax,%eax
js     1193 <mfd_assert_new+0x53>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe81(%rip),%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xe89(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"memfd_create(\22%s\22, %u) failed: %m\0A\00"
@.str.1 = [28 x i8] c"ftruncate(%llu) failed: %m\0A\00"
define i32 @mfd_assert_new(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* %6
%11 = call i32 @sys_memfd_create(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = load i32, i32* %6
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16)
%18 = call i32 (...) @abort()
unreachable
19: ; preds = %3
%20 = load i32, i32* %8
%21 = load i64, i64* %5
%22 = call i32 @ftruncate(i32 %20, i64 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i64, i64* %5
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %26)
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %19
%30 = load i32, i32* %8
ret i32 %30
}

LLVM IR (-O2):
@.str = [35 x i8] c"memfd_create(\22%s\22, %u) failed: %m\0A\00"
@.str.1 = [28 x i8] c"ftruncate(%llu) failed: %m\0A\00"
define i32 @mfd_assert_new(i8* %0, i64 %1, i32 %2) {
%4 = tail call i32 @sys_memfd_create(i8* %0, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %2)
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %3
%10 = tail call i32 @ftruncate(i32 %4, i64 %1)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %1)
%14 = tail call i32 (...) @abort()
unreachable
15: ; preds = %9
ret i32 %4
}