#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 ftruncate(int, int);
int /*<<< orphan*/ mfd_fail_open(int, int, int);
int /*<<< orphan*/ printf(char *);
void mfd_fail_shrink(int fd) {
  int r;
  r = ftruncate(fd, 8192 / 2);
  if (r >= 0) {
    printf("ftruncate(SHRINK) didn't fail as expected\n");
    abort();
  }
  mfd_fail_open(fd, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
}


x86 assembly (clang, -O0):
<mfd_fail_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)
jl     1178 <mfd_fail_shrink+0x38>
lea    0xe94(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    0x2e6d(%rip),%rax
or     (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
call   1060 <mfd_fail_open@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfd_fail_shrink>:
push   %rbx
mov    %edi,%ebx
mov    $0x1000,%esi
call   1050 <ftruncate@plt>
test   %eax,%eax
jns    1186 <mfd_fail_shrink+0x46>
mov    0x2e80(%rip),%rax
mov    0x2e99(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e86(%rip),%rax
or     (%rax),%esi
mov    0x2e4d(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %ebx,%edi
pop    %rbx
jmp    1060 <mfd_fail_open@plt>
lea    0xe73(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@.str = [43 x i8] c"ftruncate(SHRINK) didn't fail as expected\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_fail_shrink(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @ftruncate(i32 %4, i32 4096)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @abort()
unreachable
11: ; preds = %1
%12 = load i32, i32* %2
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = call i32 @mfd_fail_open(i32 %12, i32 %17, i32 %20)
ret void
}

LLVM IR (-O2):
@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
@str = [42 x i8] c"ftruncate(SHRINK) didn't fail as expected\00"
define void @mfd_fail_shrink(i32 %0) {
%2 = tail call i32 @ftruncate(i32 %0, i32 4096)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (...) @abort()
unreachable
7: ; preds = %1
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %14, %13
%16 = tail call i32 @mfd_fail_open(i32 %0, i32 %12, i32 %15)
ret void
}