#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 ssize_t;
typedef scalar_t__ 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, scalar_t__);
scalar_t__ lseek(int const, scalar_t__, int /*<<< orphan*/);
int pread(int const, char **, size_t, scalar_t__);
void trim_file(const int fd, const char *file) {
  char *bp, buf[512];
  off_t offset, read_where, read_size, total_size, trunc_size;
  ssize_t amnt_read, i;
  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;
    }
    amnt_read = pread(fd, &buf, (size_t)read_size, read_where);
    if (amnt_read == 0)
      errx(EX_IOERR, "unexpected EOF on pread of '%s'", file);
    else if (amnt_read == -1)
      err(EX_IOERR, "pread failed on '%s'", file);
    else if (amnt_read < -1)
      errx(EX_IOERR, "unexpected return from pread of '%s': %ld", file,
           amnt_read);
    else {
      bp = buf + amnt_read;
      for (i = 0; i < amnt_read; i++) {
        switch (*--bp) {
        case '\n':
        case '\r':
          break;
        default:
          trunc_size -= i;
          i = amnt_read;
          offset = 0;
        }
      }
    }
    if (offset > 0) {
      trunc_size -= amnt_read;
      offset -= amnt_read;
    }
  }
  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    %eax,%eax
mov    %eax,%esi
call   1050 <lseek@plt>
mov    %rax,-0x240(%rbp)
cmp    $0x0,%rax
jge    11a1 <trim_file+0x51>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe64(%rip),%rsi
call   1060 <err@plt>
cmpq   $0x0,-0x240(%rbp)
jne    11b4 <trim_file+0x64>
jmp    1426 <trim_file+0x2d6>
mov    -0x240(%rbp),%rax
mov    %rax,-0x248(%rbp)
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jle    13dc <trim_file+0x28c>
mov    -0x228(%rbp),%rax
sub    $0x200,%rax
mov    %rax,-0x230(%rbp)
movq   $0x200,-0x238(%rbp)
cmpq   $0x0,-0x230(%rbp)
jge    1223 <trim_file+0xd3>
mov    -0x230(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x238(%rbp)
movq   $0x0,-0x230(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x238(%rbp),%rdx
mov    -0x230(%rbp),%rcx
call   1070 <pread@plt>
mov    %eax,-0x24c(%rbp)
cmpl   $0x0,-0x24c(%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    1399 <trim_file+0x249>
cmpl   $0xffffffff,-0x24c(%rbp)
jne    129e <trim_file+0x14e>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xda0(%rip),%rsi
call   1060 <err@plt>
jmp    1394 <trim_file+0x244>
cmpl   $0xffffffff,-0x24c(%rbp)
jge    12d1 <trim_file+0x181>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x24c(%rbp),%ecx
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1030 <errx@plt>
jmp    138f <trim_file+0x23f>
lea    -0x220(%rbp),%rax
movslq -0x24c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x250(%rbp)
mov    -0x250(%rbp),%eax
cmp    -0x24c(%rbp),%eax
jge    138a <trim_file+0x23a>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movsbl -0x1(%rax),%eax
mov    %eax,-0x254(%rbp)
sub    $0xa,%eax
je     133d <trim_file+0x1ed>
jmp    1329 <trim_file+0x1d9>
mov    -0x254(%rbp),%eax
sub    $0xd,%eax
jne    1342 <trim_file+0x1f2>
jmp    133d <trim_file+0x1ed>
jmp    1371 <trim_file+0x221>
movslq -0x250(%rbp),%rcx
mov    -0x248(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x248(%rbp)
mov    -0x24c(%rbp),%eax
mov    %eax,-0x250(%rbp)
movq   $0x0,-0x228(%rbp)
jmp    1376 <trim_file+0x226>
mov    -0x250(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x250(%rbp)
jmp    12f0 <trim_file+0x1a0>
jmp    138f <trim_file+0x23f>
jmp    1394 <trim_file+0x244>
jmp    1399 <trim_file+0x249>
cmpq   $0x0,-0x228(%rbp)
jle    13d7 <trim_file+0x287>
movslq -0x24c(%rbp),%rcx
mov    -0x248(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x248(%rbp)
movslq -0x24c(%rbp),%rcx
mov    -0x228(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x228(%rbp)
jmp    11c9 <trim_file+0x79>
mov    -0x248(%rbp),%rax
cmp    -0x240(%rbp),%rax
jge    1426 <trim_file+0x2d6>
mov    -0x4(%rbp),%edi
mov    -0x248(%rbp),%rsi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    1421 <trim_file+0x2d1>
mov    0x2bd1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xc57(%rip),%rsi
call   1060 <err@plt>
jmp    1426 <trim_file+0x2d6>
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     12c7 <trim_file+0x177>
je     12b5 <trim_file+0x165>
lea    0x10(%rsp),%r12
mov    %rax,%rbx
mov    %rax,0x8(%rsp)
mov    %rax,%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rcx
sub    $0x200,%rcx
mov    $0x0,%eax
cmovb  %rax,%rcx
mov    $0x200,%edx
cmovb  %r14,%rdx
mov    %ebp,%edi
mov    %r12,%rsi
call   1070 <pread@plt>
movslq %eax,%r13
cmp    $0xffffffff,%eax
je     1200 <trim_file+0xb0>
test   %eax,%eax
jne    1220 <trim_file+0xd0>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <errx@plt>
jmp    1290 <trim_file+0x140>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    %r15,%rdx
call   1060 <err@plt>
jmp    1290 <trim_file+0x140>
nopw   0x0(%rax,%rax,1)
cmp    $0xfffffffe,%eax
jg     1243 <trim_file+0xf3>
mov    0x2db4(%rip),%rcx
mov    (%rcx),%edi
lea    0xe14(%rip),%rsi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <errx@plt>
jmp    1290 <trim_file+0x140>
mov    %eax,%ecx
lea    0xf(%rsp),%rdx
add    %rdx,%rcx
xor    %edx,%edx
jmp    126b <trim_file+0x11b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%edx
add    $0xffffffffffffffff,%rcx
cmp    %eax,%edx
jge    1285 <trim_file+0x135>
movsbl (%rcx),%esi
cmp    $0xa,%esi
je     1260 <trim_file+0x110>
cmp    $0xd,%esi
je     1260 <trim_file+0x110>
movslq %edx,%rdx
sub    %rdx,%rbx
xor    %r14d,%r14d
mov    %eax,%edx
jmp    1260 <trim_file+0x110>
test   %r14,%r14
jle    129f <trim_file+0x14f>
nopw   0x0(%rax,%rax,1)
sub    %r13,%rbx
sub    %r13,%r14
test   %r14,%r14
jg     11a0 <trim_file+0x50>
cmp    0x8(%rsp),%rbx
jge    12b5 <trim_file+0x165>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12f0 <trim_file+0x1a0>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd32(%rip),%rsi
mov    0x2d0b(%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    0xd7c(%rip),%rsi
jmp    12ce <trim_file+0x17e>

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 i32
%13 = alloca i32
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, i64 0, i32 %15)
store i64 %16, i64* %10
%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* %10
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
br label %121
26: ; preds = %22
%27 = load i64, i64* %10
store i64 %27, i64* %11
store i64 %27, i64* %7
br label %28
28: ; preds = %106, %26
%29 = load i64, i64* %7
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %107
31: ; preds = %28
%32 = load i64, i64* %7
%33 = sub nsw i64 %32, 512
store i64 %33, i64* %8
store i64 512, i64* %9
%34 = load i64, i64* %8
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i64, i64* %8
%38 = add nsw i64 512, %37
store i64 %38, i64* %9
store i64 0, i64* %8
br label %39
39: ; preds = %36, %31
%40 = load i32, i32* %3
%41 = bitcast [512 x i8]* %6 to i8**
%42 = load i64, i64* %9
%43 = load i64, i64* %8
%44 = call i32 @pread(i32 %40, i8** %41, i64 %42, i64 %43)
store i32 %44, i32* %12
%45 = load i32, i32* %12
%46 = icmp eq i32 %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 %94
51: ; preds = %39
%52 = load i32, i32* %12
%53 = icmp eq i32 %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 %93
58: ; preds = %51
%59 = load i32, i32* %12
%60 = icmp slt i32 %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 i32, i32* %12
%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, i32 %64)
br label %92
66: ; preds = %58
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%68 = load i32, i32* %12
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
store i8* %70, i8** %5
store i32 0, i32* %13
br label %71
71: ; preds = %88, %66
%72 = load i32, i32* %13
%73 = load i32, i32* %12
%74 = icmp slt i32 %72, %73
br i1 %74, label %75, label %91
75: ; preds = %71
%76 = load i8*, i8** %5
%77 = getelementptr inbounds i8, i8* %76, i32 -1
store i8* %77, i8** %5
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
switch i32 %79, label %81 [
i32 10, label %80
i32 13, label %80
]
80: ; preds = %75, %75
br label %87
81: ; preds = %75
%82 = load i32, i32* %13
%83 = sext i32 %82 to i64
%84 = load i64, i64* %11
%85 = sub nsw i64 %84, %83
store i64 %85, i64* %11
%86 = load i32, i32* %12
store i32 %86, i32* %13
store i64 0, i64* %7
br label %87
87: ; preds = %81, %80
br label %88
88: ; preds = %87
%89 = load i32, i32* %13
%90 = add nsw i32 %89, 1
store i32 %90, i32* %13
br label %71
91: ; preds = %71
br label %92
92: ; preds = %91, %61
br label %93
93: ; preds = %92, %54
br label %94
94: ; preds = %93, %47
%95 = load i64, i64* %7
%96 = icmp sgt i64 %95, 0
br i1 %96, label %97, label %106
97: ; preds = %94
%98 = load i32, i32* %12
%99 = sext i32 %98 to i64
%100 = load i64, i64* %11
%101 = sub nsw i64 %100, %99
store i64 %101, i64* %11
%102 = load i32, i32* %12
%103 = sext i32 %102 to i64
%104 = load i64, i64* %7
%105 = sub nsw i64 %104, %103
store i64 %105, i64* %7
br label %106
106: ; preds = %97, %94
br label %28
107: ; preds = %28
%108 = load i64, i64* %11
%109 = load i64, i64* %10
%110 = icmp slt i64 %108, %109
br i1 %110, label %111, label %121
111: ; preds = %107
%112 = load i32, i32* %3
%113 = load i64, i64* %11
%114 = call i32 @ftruncate(i32 %112, i64 %113)
%115 = icmp eq i32 %114, -1
br i1 %115, label %116, label %120
116: ; preds = %111
%117 = load i32, i32* @EX_IOERR
%118 = load i8*, i8** %4
%119 = call i32 @err(i32 %117, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %118)
br label %120
120: ; preds = %116, %111
br label %121
121: ; preds = %25, %120, %107
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, i64 0, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %69, label %8
8: ; preds = %2
%9 = icmp eq i64 %6, 0
br i1 %9, label %73, label %10
10: ; preds = %8
%11 = bitcast [512 x i8]* %3 to i8**
br label %16
12: ; preds = %58, %54
%13 = phi i64 [ %56, %54 ], [ %61, %58 ]
%14 = phi i64 [ %57, %54 ], [ %62, %58 ]
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %63
16: ; preds = %10, %12
%17 = phi i64 [ %6, %10 ], [ %13, %12 ]
%18 = phi i64 [ %6, %10 ], [ %14, %12 ]
%19 = icmp ult i64 %18, 512
%20 = tail call i64 @llvm.usub.sat.i64(i64 %18, i64 512)
%21 = select i1 %19, i64 %18, i64 512
%22 = call i32 @pread(i32 %0, i8** %11, i64 %21, i64 %20)
switch i32 %22, label %29 [
i32 0, label %23
i32 -1, label %26
]
23: ; preds = %16
%24 = load i32, i32* @EX_IOERR
%25 = tail call i32 (i32, i8*, i8*, ...) @errx(i32 %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %54
26: ; preds = %16
%27 = load i32, i32* @EX_IOERR
%28 = tail call i32 @err(i32 %27, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %54
29: ; preds = %16
%30 = icmp slt i32 %22, -1
br i1 %30, label %31, label %34
31: ; preds = %29
%32 = load i32, i32* @EX_IOERR
%33 = tail call i32 (i32, i8*, i8*, ...) @errx(i32 %32, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %1, i32 %22)
br label %54
34: ; preds = %29
%35 = zext i32 %22 to i64
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %35
br label %37
37: ; preds = %34, %48
%38 = phi i32 [ %52, %48 ], [ 0, %34 ]
%39 = phi i8* [ %42, %48 ], [ %36, %34 ]
%40 = phi i64 [ %50, %48 ], [ %17, %34 ]
%41 = phi i64 [ %49, %48 ], [ %18, %34 ]
%42 = getelementptr inbounds i8, i8* %39, i64 -1
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
switch i32 %44, label %45 [
i32 10, label %48
i32 13, label %48
]
45: ; preds = %37
%46 = sext i32 %38 to i64
%47 = sub nsw i64 %40, %46
br label %48
48: ; preds = %45, %37, %37
%49 = phi i64 [ 0, %45 ], [ %41, %37 ], [ %41, %37 ]
%50 = phi i64 [ %47, %45 ], [ %40, %37 ], [ %40, %37 ]
%51 = phi i32 [ %22, %45 ], [ %38, %37 ], [ %38, %37 ]
%52 = add nsw i32 %51, 1
%53 = icmp slt i32 %52, %22
br i1 %53, label %37, label %58
54: ; preds = %23, %26, %31
%55 = sext i32 %22 to i64
%56 = sub nsw i64 %17, %55
%57 = sub nsw i64 %18, %55
br label %12
58: ; preds = %48
%59 = icmp sgt i64 %49, 0
%60 = sext i32 %22 to i64
%61 = sub nsw i64 %50, %60
%62 = sub nsw i64 %49, %60
br i1 %59, label %12, label %63
63: ; preds = %12, %58
%64 = phi i64 [ %13, %12 ], [ %50, %58 ]
%65 = icmp slt i64 %64, %6
br i1 %65, label %66, label %73
66: ; preds = %63
%67 = tail call i32 @ftruncate(i32 %0, i64 %64)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %73
69: ; preds = %66, %2
%70 = 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), %66 ]
%71 = load i32, i32* @EX_IOERR
%72 = tail call i32 @err(i32 %71, i8* %70, i8* %1)
br label %73
73: ; preds = %69, %63, %66, %8
ret void
}