#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 int off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int ftruncate(int, int);
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char const *, int);
int ftruncate_growable(int fd, off_t length) {
  int ret;
  off_t pos;
  static const char zero = 0;
  if ((ret = ftruncate(fd, length)) != -1) {
    return ret;
  }
  if ((pos = lseek(fd, 0, SEEK_CUR)) == -1) {
    perror("lseek");
    return -1;
  }
  if (lseek(fd, length, SEEK_SET) == -1) {
    perror("lseek");
    return -1;
  }
  if (write(fd, &zero, 1) == -1) {
    perror("write");
    return -1;
  }
  if (ftruncate(fd, length) == -1) {
    perror("ftruncate");
    return -1;
  }
  if (lseek(fd, pos, SEEK_SET) == -1) {
    perror("lseek");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ftruncate_growable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <ftruncate@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
je     1170 <ftruncate_growable+0x30>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1279 <ftruncate_growable+0x139>
mov    -0x8(%rbp),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11a7 <ftruncate_growable+0x67>
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <ftruncate_growable+0x139>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11dc <ftruncate_growable+0x9c>
lea    0xe36(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <ftruncate_growable+0x139>
mov    -0x8(%rbp),%edi
lea    0xe1a(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1211 <ftruncate_growable+0xd1>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <ftruncate_growable+0x139>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    123d <ftruncate_growable+0xfd>
lea    0xde1(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <ftruncate_growable+0x139>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1272 <ftruncate_growable+0x132>
lea    0xda0(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <ftruncate_growable+0x139>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftruncate_growable>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebx
mov    %edi,%ebp
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11f0 <ftruncate_growable+0xb0>
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0xe8e(%rip),%r14
cmp    $0xffffffff,%eax
je     11e3 <ftruncate_growable+0xa3>
mov    %eax,%r15d
mov    0x2e56(%rip),%r12
mov    (%r12),%edx
mov    %ebp,%edi
mov    %ebx,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     11e3 <ftruncate_growable+0xa3>
lea    0xe65(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11d3 <ftruncate_growable+0x93>
mov    %ebp,%edi
mov    %ebx,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11dc <ftruncate_growable+0x9c>
mov    (%r12),%edx
mov    %ebp,%edi
mov    %r15d,%esi
call   1050 <lseek@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11f0 <ftruncate_growable+0xb0>
jmp    11e3 <ftruncate_growable+0xa3>
lea    0xe2d(%rip),%r14
jmp    11e3 <ftruncate_growable+0xa3>
lea    0xe2a(%rip),%r14
mov    %r14,%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ftruncate_growable.zero = internal constant i8 0
@SEEK_CUR = global i32 0
@.str = [6 x i8] c"lseek\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [10 x i8] c"ftruncate\00"
define i32 @ftruncate_growable(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* %5
%10 = call i32 @ftruncate(i32 %8, i32 %9)
store i32 %10, i32* %6
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %6
store i32 %13, i32* %3
br label %51
14: ; preds = %2
%15 = load i32, i32* %4
%16 = load i32, i32* @SEEK_CUR
%17 = call i32 @lseek(i32 %15, i32 0, i32 %16)
store i32 %17, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
21: ; preds = %14
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %22, i32 %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
29: ; preds = %21
%30 = load i32, i32* %4
%31 = call i32 @write(i32 %30, i8* @ftruncate_growable.zero, i32 1)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
35: ; preds = %29
%36 = load i32, i32* %4
%37 = load i32, i32* %5
%38 = call i32 @ftruncate(i32 %36, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
42: ; preds = %35
%43 = load i32, i32* %4
%44 = load i32, i32* %7
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @lseek(i32 %43, i32 %44, i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
50: ; preds = %42
store i32 0, i32* %3
br label %51
51: ; preds = %50, %48, %40, %33, %27, %19, %12
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@ftruncate_growable.zero = internal constant i8 0
@SEEK_CUR = global i32 0
@.str = [6 x i8] c"lseek\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [10 x i8] c"ftruncate\00"
define i32 @ftruncate_growable(i32 %0, i32 %1) {
%3 = tail call i32 @ftruncate(i32 %0, i32 %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %26
5: ; preds = %2
%6 = load i32, i32* @SEEK_CUR
%7 = tail call i32 @lseek(i32 %0, i32 0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %23, label %9
9: ; preds = %5
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %0, i32 %1, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %23, label %13
13: ; preds = %9
%14 = tail call i32 @write(i32 %0, i8* @ftruncate_growable.zero, i32 1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %23, label %16
16: ; preds = %13
%17 = tail call i32 @ftruncate(i32 %0, i32 %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @lseek(i32 %0, i32 %7, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %19, %16, %13, %9, %5
%24 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %16 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %19 ]
%25 = tail call i32 @perror(i8* %24)
br label %26
26: ; preds = %23, %19, %2
%27 = phi i32 [ %3, %2 ], [ 0, %19 ], [ -1, %23 ]
ret i32 %27
}