#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 O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
int ftruncate(int, int);
int mfd_assert_open(int, int, int);
int /*<<< orphan*/ mfd_assert_size(int, int);
int /*<<< orphan*/ printf(char *);
void mfd_assert_shrink(int fd) {
  int r, fd2;
  r = ftruncate(fd, 8192 / 2);
  if (r < 0) {
    printf("ftruncate(SHRINK) failed: %m\n");
    abort();
  }
  mfd_assert_size(fd, 8192 / 2);
  fd2 = mfd_assert_open(fd, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  close(fd2);
  mfd_assert_size(fd, 0);
}


x86 assembly (clang, -O0):
<mfd_assert_shrink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x1000,%esi
call   1050 <ftruncate@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1198 <mfd_assert_shrink+0x38>
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%edi
mov    $0x1000,%esi
call   1080 <mfd_assert_size@plt>
mov    -0x4(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    0x2dfd(%rip),%rax
or     (%rax),%edx
call   1070 <mfd_assert_open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1080 <mfd_assert_size@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfd_assert_shrink>:
push   %rbx
mov    %edi,%ebx
mov    $0x1000,%esi
call   1050 <ftruncate@plt>
test   %eax,%eax
js     11c2 <mfd_assert_shrink+0x62>
mov    %ebx,%edi
mov    $0x1000,%esi
call   1080 <mfd_assert_size@plt>
mov    0x2e54(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5a(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %ebx,%edi
call   1070 <mfd_assert_open@plt>
mov    %eax,%edi
call   1060 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
pop    %rbx
jmp    1080 <mfd_assert_size@plt>
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"ftruncate(SHRINK) failed: %m\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define void @mfd_assert_shrink(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @ftruncate(i32 %5, i32 4096)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%11 = call i32 (...) @abort()
unreachable
12: ; preds = %1
%13 = load i32, i32* %2
%14 = call i32 @mfd_assert_size(i32 %13, i32 4096)
%15 = load i32, i32* %2
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = call i32 @mfd_assert_open(i32 %15, i32 %20, i32 %23)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %2
%28 = call i32 @mfd_assert_size(i32 %27, i32 0)
ret void
}

LLVM IR (-O2):
@.str = [30 x i8] c"ftruncate(SHRINK) failed: %m\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define void @mfd_assert_shrink(i32 %0) {
%2 = tail call i32 @ftruncate(i32 %0, i32 4096)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (...) @abort()
unreachable
7: ; preds = %1
%8 = tail call i32 @mfd_assert_size(i32 %0, i32 4096)
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = tail call i32 @mfd_assert_open(i32 %0, i32 %13, i32 %16)
%18 = tail call i32 @close(i32 %17)
%19 = tail call i32 @mfd_assert_size(i32 %0, i32 0)
ret void
}