#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*/ F_SETFL;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ __NR_ftruncate;
int /*<<< orphan*/ close(int);
int dup(int);
scalar_t__ errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int syscall(int /*<<< orphan*/, int, int);
int write(int, int *, int);
int ftruncate(int fd, off_t length) {
  struct stat f_info;
  errno = 0;
  if (fstat(fd, &f_info) != 0) {
    fprintf(stderr, "Failed to stat file specified by fd (%d): %s\n", fd,
            strerror(errno));
    return -1;
  }
  int cur_max = f_info.st_size;
  int write_size = cur_max - length;
  int new_fd = dup(fd);
  fcntl(new_fd, F_SETFL, O_WRONLY);
  errno = 0;
  if (new_fd < 0) {
    fprintf(stderr,
            "Failed to open file specified by fd (%d) for writing: %s\n", fd,
            strerror(errno));
    return -1;
  }
  if (length == cur_max || length > cur_max) {
    return syscall(__NR_ftruncate, fd, length);
  } else {
    int to_write[length];
    int store_pos;
    memset(to_write, 0, length);
    store_pos = lseek(new_fd, 0, SEEK_CUR);
    if (store_pos < 0) {
      fprintf(stderr, "Failed to seek in file specified by fd (%d): %s", fd,
              strerror(errno));
      return -1;
    }
    lseek(new_fd, length, SEEK_SET);
    errno = 0;
    int written = write(new_fd, to_write, write_size);
    if (written < write_size) {
      fprintf(stderr, "Warning wrote %d bytes, expected to write %d!\n",
              written, write_size);
    } else if (written < 0) {
      fprintf(stderr, "Failed to write 0 after truncated data: %s",
              strerror(errno));
      return -1;
    }
    errno = 0;
    if (lseek(new_fd, store_pos, SEEK_SET) < 0) {
      fprintf(stderr, "Failed to seek back to original offset: %s",
              strerror(errno));
    }
    close(new_fd);
    return syscall(__NR_ftruncate, fd, length);
  }
}


x86 assembly (clang, -O0):
<ftruncate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e13(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
je     1216 <ftruncate+0x76>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%edx
mov    %rax,%rcx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14b0 <ftruncate+0x310>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    0x2d77(%rip),%rax
movq   $0x0,(%rax)
cmpl   $0x0,-0x1c(%rbp)
jge    12a6 <ftruncate+0x106>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%edx
mov    %rax,%rcx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14b0 <ftruncate+0x310>
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     12be <ftruncate+0x11e>
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    12da <ftruncate+0x13a>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   10a0 <syscall@plt>
mov    %eax,-0x4(%rbp)
jmp    14b0 <ftruncate+0x310>
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    0x2cd3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1376 <ftruncate+0x1d6>
mov    0x2ca6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2c84(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    -0x5c(%rbp),%edx
mov    %rax,%rcx
lea    0xd0c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    14a9 <ftruncate+0x309>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2c3d(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x58(%rbp),%rsi
mov    0x2c33(%rip),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    13d8 <ftruncate+0x238>
mov    0x2c1b(%rip),%rax
mov    (%rax),%edi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xccc(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    1429 <ftruncate+0x289>
cmpl   $0x0,-0x3c(%rbp)
jge    1424 <ftruncate+0x284>
mov    0x2bef(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2bd3(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xcbd(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    14a9 <ftruncate+0x309>
jmp    1429 <ftruncate+0x289>
mov    0x2b98(%rip),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    0x2b7c(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
cmp    $0x0,%eax
jge    1483 <ftruncate+0x2e3>
mov    0x2b7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2b61(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xc76(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    0x2b26(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   10a0 <syscall@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftruncate>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %edi,%r12d
mov    0x2e0b(%rip),%r14
movq   $0x0,(%r14)
lea    -0x40(%rbp),%rsi
call   10c0 <fstat@plt>
test   %rax,%rax
je     120d <ftruncate+0x6d>
mov    0x2dff(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%rdi
call   10b0 <strerror@plt>
lea    0xe16(%rip),%rsi
mov    %ebx,%edi
mov    %r12d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x40(%rbp),%r13d
mov    %r12d,%edi
call   1050 <dup@plt>
mov    %eax,%r15d
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
call   1030 <fcntl@plt>
movq   $0x0,(%r14)
test   %r15d,%r15d
js     12f1 <ftruncate+0x151>
cmp    %ebx,%r13d
jle    130d <ftruncate+0x16d>
mov    %r13d,-0x2c(%rbp)
mov    %rsp,-0x38(%rbp)
mov    %ebx,%eax
mov    %rsp,%r13
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <memset@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
test   %eax,%eax
js     132e <ftruncate+0x18e>
mov    %eax,-0x44(%rbp)
sub    %ebx,-0x2c(%rbp)
mov    0x2d1e(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %ebx,-0x48(%rbp)
mov    %ebx,%esi
call   1060 <lseek@plt>
movq   $0x0,(%r14)
mov    %r15d,%edi
mov    %r13,%rsi
mov    -0x2c(%rbp),%r13d
mov    %r13d,%edx
call   1040 <write@plt>
cmp    %r13d,%eax
jge    135a <ftruncate+0x1ba>
mov    0x2cfe(%rip),%rcx
mov    (%rcx),%edi
lea    0xdb5(%rip),%rsi
mov    %eax,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    135e <ftruncate+0x1be>
mov    0x2ce0(%rip),%rax
mov    (%rax),%ebx
xor    %edi,%edi
call   10b0 <strerror@plt>
lea    0xd26(%rip),%rsi
jmp    11ea <ftruncate+0x4a>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
mov    %r12d,%esi
mov    %ebx,%edx
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <syscall@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%rdi
call   10b0 <strerror@plt>
lea    0xd22(%rip),%rsi
mov    %ebx,%edi
mov    %r12d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    13e5 <ftruncate+0x245>
test   %eax,%eax
js     13c1 <ftruncate+0x221>
movq   $0x0,(%r14)
mov    0x2c54(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    -0x44(%rbp),%esi
call   1060 <lseek@plt>
test   %eax,%eax
jns    13a3 <ftruncate+0x203>
mov    0x2c54(%rip),%rax
mov    (%rax),%r13d
mov    (%r14),%rdi
call   10b0 <strerror@plt>
lea    0xd5c(%rip),%rsi
mov    %r13d,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %r15d,%edi
call   1080 <close@plt>
mov    0x2c06(%rip),%rax
mov    (%rax),%edi
mov    %r12d,%esi
mov    -0x48(%rbp),%edx
call   10a0 <syscall@plt>
jmp    13ea <ftruncate+0x24a>
mov    0x2c10(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%rdi
call   10b0 <strerror@plt>
lea    0xcee(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xffffffff,%eax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
jmp    11fe <ftruncate+0x5e>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@stderr = global i32 0
@.str = [46 x i8] c"Failed to stat file specified by fd (%d): %s\0A\00"
@F_SETFL = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [58 x i8] c"Failed to open file specified by fd (%d) for writing: %s\0A\00"
@__NR_ftruncate = global i32 0
@SEEK_CUR = global i32 0
@.str.2 = [48 x i8] c"Failed to seek in file specified by fd (%d): %s\00"
@SEEK_SET = global i32 0
@.str.3 = [47 x i8] c"Warning wrote %d bytes, expected to write %d!\0A\00"
@.str.4 = [43 x i8] c"Failed to write 0 after truncated data: %s\00"
@.str.5 = [43 x i8] c"Failed to seek back to original offset: %s\00"
define i32 @ftruncate(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64 0, i64* @errno
%15 = load i32, i32* %4
%16 = call i64 @fstat(i32 %15, %struct.stat* %6)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i32, i32* %4
%21 = load i64, i64* @errno
%22 = call i8* @strerror(i64 %21)
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %20, i8* %22)
store i32 -1, i32* %3
br label %120
24: ; preds = %2
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = load i32, i32* %5
%29 = sub nsw i32 %27, %28
store i32 %29, i32* %8
%30 = load i32, i32* %4
%31 = call i32 @dup(i32 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* @O_WRONLY
%35 = call i32 @fcntl(i32 %32, i32 %33, i32 %34)
store i64 0, i64* @errno
%36 = load i32, i32* %9
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %24
%39 = load i32, i32* @stderr
%40 = load i32, i32* %4
%41 = load i64, i64* @errno
%42 = call i8* @strerror(i64 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i32 %40, i8* %42)
store i32 -1, i32* %3
br label %120
44: ; preds = %24
%45 = load i32, i32* %5
%46 = load i32, i32* %7
%47 = icmp eq i32 %45, %46
br i1 %47, label %52, label %48
48: ; preds = %44
%49 = load i32, i32* %5
%50 = load i32, i32* %7
%51 = icmp sgt i32 %49, %50
br i1 %51, label %52, label %57
52: ; preds = %48, %44
%53 = load i32, i32* @__NR_ftruncate
%54 = load i32, i32* %4
%55 = load i32, i32* %5
%56 = call i32 @syscall(i32 %53, i32 %54, i32 %55)
store i32 %56, i32* %3
br label %120
57: ; preds = %48
%58 = load i32, i32* %5
%59 = zext i32 %58 to i64
%60 = call i8* @llvm.stacksave()
store i8* %60, i8** %10
%61 = alloca i32, i64 %59
store i64 %59, i64* %11
%62 = load i32, i32* %5
%63 = call i32 @memset(i32* %61, i32 0, i32 %62)
%64 = load i32, i32* %9
%65 = load i32, i32* @SEEK_CUR
%66 = call i32 @lseek(i32 %64, i32 0, i32 %65)
store i32 %66, i32* %12
%67 = load i32, i32* %12
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %57
%70 = load i32, i32* @stderr
%71 = load i32, i32* %4
%72 = load i64, i64* @errno
%73 = call i8* @strerror(i64 %72)
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %71, i8* %73)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %118
75: ; preds = %57
%76 = load i32, i32* %9
%77 = load i32, i32* %5
%78 = load i32, i32* @SEEK_SET
%79 = call i32 @lseek(i32 %76, i32 %77, i32 %78)
store i64 0, i64* @errno
%80 = load i32, i32* %9
%81 = load i32, i32* %8
%82 = call i32 @write(i32 %80, i32* %61, i32 %81)
store i32 %82, i32* %14
%83 = load i32, i32* %14
%84 = load i32, i32* %8
%85 = icmp slt i32 %83, %84
br i1 %85, label %86, label %91
86: ; preds = %75
%87 = load i32, i32* @stderr
%88 = load i32, i32* %14
%89 = load i32, i32* %8
%90 = call i32 (i32, i8*, ...) @fprintf(i32 %87, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %88, i32 %89)
br label %100
91: ; preds = %75
%92 = load i32, i32* %14
%93 = icmp slt i32 %92, 0
br i1 %93, label %94, label %99
94: ; preds = %91
%95 = load i32, i32* @stderr
%96 = load i64, i64* @errno
%97 = call i8* @strerror(i64 %96)
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %97)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %118
99: ; preds = %91
br label %100
100: ; preds = %99, %86
store i64 0, i64* @errno
%101 = load i32, i32* %9
%102 = load i32, i32* %12
%103 = load i32, i32* @SEEK_SET
%104 = call i32 @lseek(i32 %101, i32 %102, i32 %103)
%105 = icmp slt i32 %104, 0
br i1 %105, label %106, label %111
106: ; preds = %100
%107 = load i32, i32* @stderr
%108 = load i64, i64* @errno
%109 = call i8* @strerror(i64 %108)
%110 = call i32 (i32, i8*, ...) @fprintf(i32 %107, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %109)
br label %111
111: ; preds = %106, %100
%112 = load i32, i32* %9
%113 = call i32 @close(i32 %112)
%114 = load i32, i32* @__NR_ftruncate
%115 = load i32, i32* %4
%116 = load i32, i32* %5
%117 = call i32 @syscall(i32 %114, i32 %115, i32 %116)
store i32 %117, i32* %3
store i32 1, i32* %13
br label %118
118: ; preds = %111, %94, %69
%119 = load i8*, i8** %10
br label %120
120: ; preds = %118, %52, %38, %18
%121 = load i32, i32* %3
ret i32 %121
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@stderr = global i32 0
@.str = [46 x i8] c"Failed to stat file specified by fd (%d): %s\0A\00"
@F_SETFL = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [58 x i8] c"Failed to open file specified by fd (%d) for writing: %s\0A\00"
@__NR_ftruncate = global i32 0
@SEEK_CUR = global i32 0
@.str.2 = [48 x i8] c"Failed to seek in file specified by fd (%d): %s\00"
@SEEK_SET = global i32 0
@.str.3 = [47 x i8] c"Warning wrote %d bytes, expected to write %d!\0A\00"
@.str.4 = [43 x i8] c"Failed to write 0 after truncated data: %s\00"
@.str.5 = [43 x i8] c"Failed to seek back to original offset: %s\00"
define i32 @ftruncate(i32 %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
store i64 0, i64* @errno
%5 = call i64 @fstat(i32 %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i64, i64* @errno
%10 = tail call i8* @strerror(i64 %9)
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %10)
br label %73
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = sub nsw i32 %14, %1
%16 = tail call i32 @dup(i32 %0)
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* @O_WRONLY
%19 = tail call i32 @fcntl(i32 %16, i32 %17, i32 %18)
store i64 0, i64* @errno
%20 = icmp slt i32 %16, 0
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = load i32, i32* @stderr
%23 = tail call i8* @strerror(i64 0)
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %23)
br label %73
25: ; preds = %12
%26 = icmp sgt i32 %14, %1
br i1 %26, label %30, label %27
27: ; preds = %25
%28 = load i32, i32* @__NR_ftruncate
%29 = tail call i32 @syscall(i32 %28, i32 %0, i32 %1)
br label %73
30: ; preds = %25
%31 = zext i32 %1 to i64
%32 = tail call i8* @llvm.stacksave()
%33 = alloca i32, i64 %31
%34 = call i32 @memset(i32* %33, i32 0, i32 %1)
%35 = load i32, i32* @SEEK_CUR
%36 = call i32 @lseek(i32 %16, i32 0, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = load i64, i64* @errno
%41 = call i8* @strerror(i64 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %0, i8* %41)
br label %71
43: ; preds = %30
%44 = load i32, i32* @SEEK_SET
%45 = call i32 @lseek(i32 %16, i32 %1, i32 %44)
store i64 0, i64* @errno
%46 = call i32 @write(i32 %16, i32* %33, i32 %15)
%47 = icmp slt i32 %46, %15
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %46, i32 %15)
br label %58
51: ; preds = %43
%52 = icmp slt i32 %46, 0
br i1 %52, label %53, label %58
53: ; preds = %51
%54 = load i32, i32* @stderr
%55 = load i64, i64* @errno
%56 = call i8* @strerror(i64 %55)
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %56)
br label %71
58: ; preds = %51, %48
store i64 0, i64* @errno
%59 = load i32, i32* @SEEK_SET
%60 = call i32 @lseek(i32 %16, i32 %36, i32 %59)
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %58
%63 = load i32, i32* @stderr
%64 = load i64, i64* @errno
%65 = call i8* @strerror(i64 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %62, %58
%68 = call i32 @close(i32 %16)
%69 = load i32, i32* @__NR_ftruncate
%70 = call i32 @syscall(i32 %69, i32 %0, i32 %1)
br label %71
71: ; preds = %53, %67, %38
%72 = phi i32 [ -1, %38 ], [ %70, %67 ], [ -1, %53 ]
br label %73
73: ; preds = %21, %27, %71, %7
%74 = phi i32 [ -1, %7 ], [ -1, %21 ], [ %29, %27 ], [ %72, %71 ]
ret i32 %74
}