#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 /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ EX_IOERR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *, ...);
int ftruncate(int const, long);
long lseek(int const, int /*<<< orphan*/, int /*<<< orphan*/);
long pread(int const, char **, size_t, long);
void trim_file(const int fd, const char *file) {
  char *bp, buf[512];
  long byte_count, i, offset, read_size, read_where, total_size, trunc_size;
  if ((total_size = lseek(fd, (off_t)0, SEEK_END)) < 0)
    err(EX_IOERR, "could not seek '%s'", file);
  if (total_size == 0)
    return;
  offset = trunc_size = total_size;
  while (offset > 0) {
    read_where = offset - 512;
    read_size = 512;
    if (read_where < 0) {
      read_size = 512 + read_where;
      read_where = 0;
    }
    byte_count = pread(fd, &buf, (size_t)read_size, read_where);
    if (byte_count == 0)
      errx(EX_IOERR, "unexpected EOF on pread of '%s'", file);
    else if (byte_count == -1)
      err(EX_IOERR, "pread failed on '%s'", file);
    else if (byte_count < -1)
      errx(EX_IOERR, "unexpected return from pread of '%s': %ld", file,
           byte_count);
    else {
      bp = buf + byte_count;
      for (i = 0; i < byte_count; i++) {
        switch (*--bp) {
        case '\n':
        case '\r':
          break;
        default:
          trunc_size -= i;
          i = byte_count;
          offset = 0;
        }
      }
    }
    if (offset > 0) {
      trunc_size -= byte_count;
      offset -= byte_count;
    }
  }
  if (trunc_size < total_size)
    if (ftruncate(fd, trunc_size) == -1)
      err(EX_IOERR, "could not truncate '%s'", file);
}


x86 assembly (clang, -O0):
<trim_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,-0x250(%rbp)
cmp    $0x0,%rax
jge    119f <trim_file+0x4f>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe66(%rip),%rsi
call   1060 <err@plt>
cmpq   $0x0,-0x250(%rbp)
jne    11b2 <trim_file+0x62>
jmp    142e <trim_file+0x2de>
mov    -0x250(%rbp),%rax
mov    %rax,-0x258(%rbp)
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
jle    13e4 <trim_file+0x294>
mov    -0x238(%rbp),%rax
sub    $0x200,%rax
mov    %rax,-0x248(%rbp)
movq   $0x200,-0x240(%rbp)
cmpq   $0x0,-0x248(%rbp)
jge    1221 <trim_file+0xd1>
mov    -0x248(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x240(%rbp)
movq   $0x0,-0x248(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x240(%rbp),%rdx
mov    -0x248(%rbp),%rcx
call   1070 <pread@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    1273 <trim_file+0x123>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
jmp    13a1 <trim_file+0x251>
cmpq   $0xffffffffffffffff,-0x228(%rbp)
ff
jne    129f <trim_file+0x14f>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd9f(%rip),%rsi
call   1060 <err@plt>
jmp    139c <trim_file+0x24c>
cmpq   $0xffffffffffffffff,-0x228(%rbp)
ff
jge    12d4 <trim_file+0x184>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x228(%rbp),%rcx
lea    0xd81(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
jmp    1397 <trim_file+0x247>
lea    -0x220(%rbp),%rax
add    -0x228(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x230(%rbp)
mov    -0x230(%rbp),%rax
cmp    -0x228(%rbp),%rax
jge    1392 <trim_file+0x242>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movsbl -0x1(%rax),%eax
mov    %eax,-0x25c(%rbp)
sub    $0xa,%eax
je     1340 <trim_file+0x1f0>
jmp    132c <trim_file+0x1dc>
mov    -0x25c(%rbp),%eax
sub    $0xd,%eax
jne    1345 <trim_file+0x1f5>
jmp    1340 <trim_file+0x1f0>
jmp    1376 <trim_file+0x226>
mov    -0x230(%rbp),%rcx
mov    -0x258(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x258(%rbp)
mov    -0x228(%rbp),%rax
mov    %rax,-0x230(%rbp)
movq   $0x0,-0x238(%rbp)
jmp    137b <trim_file+0x22b>
mov    -0x230(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x230(%rbp)
jmp    12f1 <trim_file+0x1a1>
jmp    1397 <trim_file+0x247>
jmp    139c <trim_file+0x24c>
jmp    13a1 <trim_file+0x251>
cmpq   $0x0,-0x238(%rbp)
jle    13df <trim_file+0x28f>
mov    -0x228(%rbp),%rcx
mov    -0x258(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x258(%rbp)
mov    -0x228(%rbp),%rcx
mov    -0x238(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x238(%rbp)
jmp    11c7 <trim_file+0x77>
mov    -0x258(%rbp),%rax
cmp    -0x250(%rbp),%rax
jge    142e <trim_file+0x2de>
mov    -0x4(%rbp),%edi
mov    -0x258(%rbp),%rsi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    1429 <trim_file+0x2d9>
mov    0x2bc9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xc4f(%rip),%rsi
call   1060 <err@plt>
jmp    142e <trim_file+0x2de>
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
test   %rax,%rax
js     12b6 <trim_file+0x166>
je     12a4 <trim_file+0x154>
lea    0x10(%rsp),%r12
mov    %rax,%rbx
mov    %rax,0x8(%rsp)
mov    %rax,%r14
jmp    11c4 <trim_file+0x74>
nopw   0x0(%rax,%rax,1)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
mov    %r15,%rdx
call   1060 <err@plt>
sub    %r13,%rbx
sub    %r13,%r14
jle    128e <trim_file+0x13e>
mov    %r14,%rcx
sub    $0x200,%rcx
mov    $0x200,%edx
cmovb  %r14,%rdx
mov    $0x0,%eax
cmovb  %rax,%rcx
mov    %ebp,%edi
mov    %r12,%rsi
call   1070 <pread@plt>
mov    %rax,%r13
cmp    $0xffffffffffffffff,%rax
je     11a0 <trim_file+0x50>
test   %r13,%r13
jne    1220 <trim_file+0xd0>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <errx@plt>
jmp    11b8 <trim_file+0x68>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0xfffffffffffffffe,%r13
jg     1248 <trim_file+0xf8>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
mov    %r15,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1030 <errx@plt>
jmp    11b8 <trim_file+0x68>
lea    0xf(%rsp),%rax
add    %r13,%rax
xor    %ecx,%ecx
jmp    126d <trim_file+0x11d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rcx
add    $0xffffffffffffffff,%rax
cmp    %r13,%rcx
jge    1285 <trim_file+0x135>
movsbl (%rax),%edx
cmp    $0xa,%edx
je     1260 <trim_file+0x110>
cmp    $0xd,%edx
je     1260 <trim_file+0x110>
sub    %rcx,%rbx
mov    %r13,%rcx
xor    %r14d,%r14d
jmp    1260 <trim_file+0x110>
test   %r14,%r14
jg     11b8 <trim_file+0x68>
cmp    0x8(%rsp),%rbx
jge    12a4 <trim_file+0x154>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12df <trim_file+0x18f>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd43(%rip),%rsi
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rdx
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <err@plt>
lea    0xd8d(%rip),%rsi
jmp    12bd <trim_file+0x16d>

LLVM IR (-O0):
@SEEK_END = global i32 0
@EX_IOERR = global i32 0
@.str = [20 x i8] c"could not seek '%s'\00"
@.str.1 = [32 x i8] c"unexpected EOF on pread of '%s'\00"
@.str.2 = [21 x i8] c"pread failed on '%s'\00"
@.str.3 = [42 x i8] c"unexpected return from pread of '%s': %ld\00"
@.str.4 = [24 x i8] c"could not truncate '%s'\00"
define void @trim_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [512 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i32 %0, i32* %3
store i8* %1, i8** %4
%14 = load i32, i32* %3
%15 = load i32, i32* @SEEK_END
%16 = call i64 @lseek(i32 %14, i32 0, i32 %15)
store i64 %16, i64* %12
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* @EX_IOERR
%20 = load i8*, i8** %4
%21 = call i32 @err(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %18, %2
%23 = load i64, i64* %12
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
br label %117
26: ; preds = %22
%27 = load i64, i64* %12
store i64 %27, i64* %13
store i64 %27, i64* %9
br label %28
28: ; preds = %102, %26
%29 = load i64, i64* %9
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %103
31: ; preds = %28
%32 = load i64, i64* %9
%33 = sub nsw i64 %32, 512
store i64 %33, i64* %11
store i64 512, i64* %10
%34 = load i64, i64* %11
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i64, i64* %11
%38 = add nsw i64 512, %37
store i64 %38, i64* %10
store i64 0, i64* %11
br label %39
39: ; preds = %36, %31
%40 = load i32, i32* %3
%41 = bitcast [512 x i8]* %6 to i8**
%42 = load i64, i64* %10
%43 = load i64, i64* %11
%44 = call i64 @pread(i32 %40, i8** %41, i64 %42, i64 %43)
store i64 %44, i64* %7
%45 = load i64, i64* %7
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %39
%48 = load i32, i32* @EX_IOERR
%49 = load i8*, i8** %4
%50 = call i32 (i32, i8*, i8*, ...) @errx(i32 %48, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %49)
br label %92
51: ; preds = %39
%52 = load i64, i64* %7
%53 = icmp eq i64 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i32, i32* @EX_IOERR
%56 = load i8*, i8** %4
%57 = call i32 @err(i32 %55, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %56)
br label %91
58: ; preds = %51
%59 = load i64, i64* %7
%60 = icmp slt i64 %59, -1
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load i32, i32* @EX_IOERR
%63 = load i8*, i8** %4
%64 = load i64, i64* %7
%65 = call i32 (i32, i8*, i8*, ...) @errx(i32 %62, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %63, i64 %64)
br label %90
66: ; preds = %58
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%68 = load i64, i64* %7
%69 = getelementptr inbounds i8, i8* %67, i64 %68
store i8* %69, i8** %5
store i64 0, i64* %8
br label %70
70: ; preds = %86, %66
%71 = load i64, i64* %8
%72 = load i64, i64* %7
%73 = icmp slt i64 %71, %72
br i1 %73, label %74, label %89
74: ; preds = %70
%75 = load i8*, i8** %5
%76 = getelementptr inbounds i8, i8* %75, i32 -1
store i8* %76, i8** %5
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
switch i32 %78, label %80 [
i32 10, label %79
i32 13, label %79
]
79: ; preds = %74, %74
br label %85
80: ; preds = %74
%81 = load i64, i64* %8
%82 = load i64, i64* %13
%83 = sub nsw i64 %82, %81
store i64 %83, i64* %13
%84 = load i64, i64* %7
store i64 %84, i64* %8
store i64 0, i64* %9
br label %85
85: ; preds = %80, %79
br label %86
86: ; preds = %85
%87 = load i64, i64* %8
%88 = add nsw i64 %87, 1
store i64 %88, i64* %8
br label %70
89: ; preds = %70
br label %90
90: ; preds = %89, %61
br label %91
91: ; preds = %90, %54
br label %92
92: ; preds = %91, %47
%93 = load i64, i64* %9
%94 = icmp sgt i64 %93, 0
br i1 %94, label %95, label %102
95: ; preds = %92
%96 = load i64, i64* %7
%97 = load i64, i64* %13
%98 = sub nsw i64 %97, %96
store i64 %98, i64* %13
%99 = load i64, i64* %7
%100 = load i64, i64* %9
%101 = sub nsw i64 %100, %99
store i64 %101, i64* %9
br label %102
102: ; preds = %95, %92
br label %28
103: ; preds = %28
%104 = load i64, i64* %13
%105 = load i64, i64* %12
%106 = icmp slt i64 %104, %105
br i1 %106, label %107, label %117
107: ; preds = %103
%108 = load i32, i32* %3
%109 = load i64, i64* %13
%110 = call i32 @ftruncate(i32 %108, i64 %109)
%111 = icmp eq i32 %110, -1
br i1 %111, label %112, label %116
112: ; preds = %107
%113 = load i32, i32* @EX_IOERR
%114 = load i8*, i8** %4
%115 = call i32 @err(i32 %113, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %114)
br label %116
116: ; preds = %112, %107
br label %117
117: ; preds = %25, %116, %103
ret void
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@EX_IOERR = global i32 0
@.str = [20 x i8] c"could not seek '%s'\00"
@.str.1 = [32 x i8] c"unexpected EOF on pread of '%s'\00"
@.str.2 = [21 x i8] c"pread failed on '%s'\00"
@.str.3 = [42 x i8] c"unexpected return from pread of '%s': %ld\00"
@.str.4 = [24 x i8] c"could not truncate '%s'\00"
define void @trim_file(i32 %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @lseek(i32 %0, i32 0, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %62, label %8
8: ; preds = %2
%9 = icmp eq i64 %6, 0
br i1 %9, label %66, label %10
10: ; preds = %8
%11 = bitcast [512 x i8]* %3 to i8**
br label %18
12: ; preds = %54, %25, %28, %33
%13 = phi i64 [ %51, %54 ], [ %19, %25 ], [ %19, %28 ], [ %19, %33 ]
%14 = phi i64 [ %50, %54 ], [ %20, %25 ], [ %20, %28 ], [ %20, %33 ]
%15 = sub nsw i64 %14, %24
%16 = sub nsw i64 %13, %24
%17 = icmp sgt i64 %15, 0
br i1 %17, label %18, label %56
18: ; preds = %10, %12
%19 = phi i64 [ %6, %10 ], [ %16, %12 ]
%20 = phi i64 [ %6, %10 ], [ %15, %12 ]
%21 = icmp ult i64 %20, 512
%22 = select i1 %21, i64 %20, i64 512
%23 = tail call i64 @llvm.usub.sat.i64(i64 %20, i64 512)
%24 = call i64 @pread(i32 %0, i8** %11, i64 %22, i64 %23)
switch i64 %24, label %31 [
i64 0, label %25
i64 -1, label %28
]
25: ; preds = %18
%26 = load i32, i32* @EX_IOERR
%27 = tail call i32 (i32, i8*, i8*, ...) @errx(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %12
28: ; preds = %18
%29 = load i32, i32* @EX_IOERR
%30 = tail call i32 @err(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %12
31: ; preds = %18
%32 = icmp slt i64 %24, -1
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = load i32, i32* @EX_IOERR
%35 = tail call i32 (i32, i8*, i8*, ...) @errx(i32 %34, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %1, i64 %24)
br label %12
36: ; preds = %31
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %24
br label %38
38: ; preds = %36, %48
%39 = phi i64 [ %51, %48 ], [ %19, %36 ]
%40 = phi i8* [ %43, %48 ], [ %37, %36 ]
%41 = phi i64 [ %50, %48 ], [ %20, %36 ]
%42 = phi i64 [ %52, %48 ], [ 0, %36 ]
%43 = getelementptr inbounds i8, i8* %40, i64 -1
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
switch i32 %45, label %46 [
i32 10, label %48
i32 13, label %48
]
46: ; preds = %38
%47 = sub nsw i64 %39, %42
br label %48
48: ; preds = %46, %38, %38
%49 = phi i64 [ %24, %46 ], [ %42, %38 ], [ %42, %38 ]
%50 = phi i64 [ 0, %46 ], [ %41, %38 ], [ %41, %38 ]
%51 = phi i64 [ %47, %46 ], [ %39, %38 ], [ %39, %38 ]
%52 = add nsw i64 %49, 1
%53 = icmp slt i64 %52, %24
br i1 %53, label %38, label %54
54: ; preds = %48
%55 = icmp sgt i64 %50, 0
br i1 %55, label %12, label %56
56: ; preds = %12, %54
%57 = phi i64 [ %16, %12 ], [ %51, %54 ]
%58 = icmp slt i64 %57, %6
br i1 %58, label %59, label %66
59: ; preds = %56
%60 = tail call i32 @ftruncate(i32 %0, i64 %57)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %66
62: ; preds = %59, %2
%63 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), %59 ]
%64 = load i32, i32* @EX_IOERR
%65 = tail call i32 @err(i32 %64, i8* %63, i8* %1)
br label %66
66: ; preds = %62, %56, %59, %8
ret void
}