#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 *);
scalar_t__ 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();
  if (ftruncate(fd, (off_t)0) < 0) {
    prterr("trunc_hack: ftruncate(0)");
    exit(1);
  }
  if (ftruncate(fd, (off_t)100000) < 0) {
    prterr("trunc_hack: ftruncate(100000)");
    exit(1);
  }
  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);
  }
  if (ftruncate(fd, 0) < 0) {
    prterr("trunc_hack: ftruncate(0) (2nd call)");
    exit(1);
  }
}


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>
cmp    $0x0,%rax
jge    119c <check_trunc_hack+0x3c>
lea    0xe73(%rip),%rdi
call   1030 <prterr@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x186a0,%esi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
jge    11c9 <check_trunc_hack+0x69>
lea    0xe5f(%rip),%rdi
call   1030 <prterr@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11f2 <check_trunc_hack+0x92>
lea    0xe51(%rip),%rdi
call   1030 <prterr@plt>
movl   $0xffffffff,-0x8(%rbp)
cmpl   $0x186a0,-0x8(%rbp)
je     1215 <check_trunc_hack+0xb5>
lea    0xe43(%rip),%rdi
call   1040 <prt@plt>
mov    $0x82,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
jge    123f <check_trunc_hack+0xdf>
lea    0xe3c(%rip),%rdi
call   1030 <prterr@plt>
mov    $0x1,%edi
call   1070 <exit@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>
test   %rax,%rax
js     11bc <check_trunc_hack+0x5c>
mov    %ebx,%edi
mov    $0x186a0,%esi
call   1050 <ftruncate@plt>
test   %rax,%rax
js     11c5 <check_trunc_hack+0x65>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1080 <fstat@plt>
test   %rax,%rax
jne    11ce <check_trunc_hack+0x6e>
cmpl   $0x186a0,0x8(%rsp)
jne    11e2 <check_trunc_hack+0x82>
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
test   %rax,%rax
js     11f8 <check_trunc_hack+0x98>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3d(%rip),%rdi
jmp    11ff <check_trunc_hack+0x9f>
lea    0xe4d(%rip),%rdi
jmp    11ff <check_trunc_hack+0x9f>
lea    0xe62(%rip),%rdi
call   1030 <prterr@plt>
movl   $0xffffffff,0x8(%rsp)
ff
lea    0xe60(%rip),%rdi
call   1040 <prt@plt>
mov    $0x82,%edi
call   1070 <exit@plt>
lea    0xe6d(%rip),%rdi
call   1030 <prterr@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [25 x i8] c"trunc_hack: ftruncate(0)\00"
@.str.1 = [30 x i8] c"trunc_hack: ftruncate(100000)\00"
@.str.2 = [18 x i8] c"trunc_hack: fstat\00"
@.str.3 = [35 x i8] c"no extend on truncate! not posix!\0A\00"
@.str.4 = [36 x i8] c"trunc_hack: ftruncate(0) (2nd call)\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 i64 @ftruncate(i32 %4, i32 0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @prterr(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* %2
%12 = call i64 @ftruncate(i32 %11, i32 100000)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @prterr(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = load i32, i32* %2
%19 = call i64 @fstat(i32 %18, %struct.stat* %1)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @prterr(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%23 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
store i32 -1, i32* %23
br label %24
24: ; preds = %21, %17
%25 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%26 = load i32, i32* %25
%27 = icmp ne i32 %26, 100000
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @prt(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @exit(i32 130)
unreachable
31: ; preds = %24
%32 = load i32, i32* %2
%33 = call i64 @ftruncate(i32 %32, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @prterr(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [25 x i8] c"trunc_hack: ftruncate(0)\00"
@.str.1 = [30 x i8] c"trunc_hack: ftruncate(100000)\00"
@.str.2 = [18 x i8] c"trunc_hack: fstat\00"
@.str.3 = [35 x i8] c"no extend on truncate! not posix!\0A\00"
@.str.4 = [36 x i8] c"trunc_hack: ftruncate(0) (2nd call)\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 i64 @ftruncate(i32 %3, i32 0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @prterr(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i64 @ftruncate(i32 %3, i32 100000)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @prterr(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9
%16 = call i64 @fstat(i32 %3, %struct.stat* %1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = tail call i32 @prterr(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%20 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
store i32 -1, i32* %20
br label %25
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 100000
br i1 %24, label %28, label %25
25: ; preds = %18, %21
%26 = tail call i32 @prt(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @exit(i32 130)
unreachable
28: ; preds = %21
%29 = tail call i64 @ftruncate(i32 %3, i32 0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = tail call i32 @prterr(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %28
ret void
}