#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 */
struct stat {
  int st_size;
};
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ ftruncate(int, int);
int get_fd();
int /*<<< orphan*/ prt(char *);
int /*<<< orphan*/ prterr(char *);
void check_trunc_hack(void) {
  struct stat statbuf;
  int fd = get_fd();
  ftruncate(fd, (off_t)0);
  ftruncate(fd, (off_t)100000);
  if (fstat(fd, &statbuf)) {
    prterr("trunc_hack: fstat");
    statbuf.st_size = -1;
  }
  if (statbuf.st_size != (off_t)100000) {
    prt("no extend on truncate! not posix!\n");
    exit(130);
  }
  ftruncate(fd, 0);
}


x86 assembly (clang, -O0):
<check_trunc_hack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <get_fd@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
mov    -0xc(%rbp),%edi
mov    $0x186a0,%esi
call   1050 <ftruncate@plt>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11b2 <check_trunc_hack+0x52>
lea    0xe5a(%rip),%rdi
call   1030 <prterr@plt>
movl   $0xffffffff,-0x8(%rbp)
cmpl   $0x186a0,-0x8(%rbp)
je     11d5 <check_trunc_hack+0x75>
lea    0xe4c(%rip),%rdi
call   1040 <prt@plt>
mov    $0x82,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_trunc_hack>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1060 <get_fd@plt>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
mov    %ebx,%edi
mov    $0x186a0,%esi
call   1050 <ftruncate@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1080 <fstat@plt>
test   %rax,%rax
jne    11ac <check_trunc_hack+0x4c>
cmpl   $0x186a0,0x8(%rsp)
jne    11c0 <check_trunc_hack+0x60>
mov    %ebx,%edi
xor    %esi,%esi
add    $0x10,%rsp
pop    %rbx
jmp    1050 <ftruncate@plt>
lea    0xe4d(%rip),%rdi
call   1030 <prterr@plt>
movl   $0xffffffff,0x8(%rsp)
ff
lea    0xe4b(%rip),%rdi
call   1040 <prt@plt>
mov    $0x82,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [18 x i8] c"trunc_hack: fstat\00"
@.str.1 = [35 x i8] c"no extend on truncate! not posix!\0A\00"
define void @check_trunc_hack() {
%1 = alloca %struct.stat
%2 = alloca i32
%3 = call i32 (...) @get_fd()
store i32 %3, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @ftruncate(i32 %4, i32 0)
%6 = load i32, i32* %2
%7 = call i32 @ftruncate(i32 %6, i32 100000)
%8 = load i32, i32* %2
%9 = call i64 @fstat(i32 %8, %struct.stat* %1)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @prterr(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
store i32 -1, i32* %13
br label %14
14: ; preds = %11, %0
%15 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%16 = load i32, i32* %15
%17 = icmp ne i32 %16, 100000
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @prt(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 130)
unreachable
21: ; preds = %14
%22 = load i32, i32* %2
%23 = call i32 @ftruncate(i32 %22, i32 0)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [18 x i8] c"trunc_hack: fstat\00"
@.str.1 = [35 x i8] c"no extend on truncate! not posix!\0A\00"
define void @check_trunc_hack() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = tail call i32 (...) @get_fd()
%4 = tail call i32 @ftruncate(i32 %3, i32 0)
%5 = tail call i32 @ftruncate(i32 %3, i32 100000)
%6 = call i64 @fstat(i32 %3, %struct.stat* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = tail call i32 @prterr(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
store i32 -1, i32* %10
br label %15
11: ; preds = %0
%12 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 100000
br i1 %14, label %18, label %15
15: ; preds = %8, %11
%16 = tail call i32 @prt(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 130)
unreachable
18: ; preds = %11
%19 = tail call i32 @ftruncate(i32 %3, i32 0)
ret void
}