#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 {
  long long st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ftruncate(int, long long);
scalar_t__ lock_whole_file(int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
long long strtoll(char const *, char **, int /*<<< orphan*/);
int /*<<< orphan*/ usage();
int main(int argc, const char *argv[]) {
  char *end;
  struct stat st;
  int fd;
  if (argc != 3) {
    usage();
    return 2;
  }
  long long pos = strtoll(argv[2], &end, 0);
  if (!end || *end || !*argv[2]) {
    usage();
    return 2;
  }
  fd = open(argv[1], O_WRONLY);
  if (fd < 0) {
    fprintf(stderr, "cannot open file %s: %m\n", argv[1]);
    return 1;
  }
  if (fstat(fd, &st) < 0) {
    fprintf(stderr, "cannot stat file %s: %m\n", argv[1]);
    return 1;
  }
  if (!S_ISREG(st.st_mode)) {
    fprintf(stderr, "%s is not a regular file\n", argv[1]);
    return 1;
  }
  if (pos >= st.st_size) {
    fprintf(stderr, "truncate position %lld after end of file\n", pos);
    return 2;
  }
  if (pos < -st.st_size) {
    fprintf(stderr, "truncate position %lld before start of file\n", pos);
    return 2;
  }
  if (pos < 0) {
    pos += st.st_size;
  }
  if (lock_whole_file(fd, F_WRLCK) <= 0) {
    return 1;
  }
  if (ftruncate(fd, pos) < 0) {
    fprintf(stderr, "cannot truncate file %s at position %lld: %m\n", argv[1],
            pos);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c3 <main+0x33>
mov    $0x0,%al
call   1080 <usage@plt>
movl   $0x2,-0x4(%rbp)
jmp    13d9 <main+0x249>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoll@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1206 <main+0x76>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1206 <main+0x76>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
cmpb   $0x0,(%rax)
jne    1219 <main+0x89>
mov    $0x0,%al
call   1080 <usage@plt>
movl   $0x2,-0x4(%rbp)
jmp    13d9 <main+0x249>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1267 <main+0xd7>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x249>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jge    12a8 <main+0x118>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x249>
mov    -0x20(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    12e4 <main+0x154>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd61(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x249>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jl     1319 <main+0x189>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xd46(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13d9 <main+0x249>
mov    -0x38(%rbp),%rax
xor    %ecx,%ecx
sub    -0x28(%rbp),%rcx
cmp    %rcx,%rax
jge    1353 <main+0x1c3>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
lea    0xd36(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13d9 <main+0x249>
cmpq   $0x0,-0x38(%rbp)
jge    136a <main+0x1da>
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2c64(%rip),%rax
mov    (%rax),%esi
call   10a0 <lock_whole_file@plt>
cmp    $0x0,%rax
jg     1391 <main+0x201>
movl   $0x1,-0x4(%rbp)
jmp    13d9 <main+0x249>
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1030 <ftruncate@plt>
cmp    $0x0,%rax
jge    13ca <main+0x23a>
mov    0x2c32(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x38(%rbp),%rcx
lea    0xce0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
cmp    $0x3,%edi
jne    11c6 <main+0x36>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <strtoll@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rax
test   %rax,%rax
je     11c6 <main+0x36>
cmpb   $0x0,(%rax)
je     11df <main+0x4f>
xor    %eax,%eax
call   1080 <usage@plt>
mov    $0x2,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rbx),%rax
cmpb   $0x0,(%rax)
je     11c6 <main+0x36>
mov    0x8(%rbx),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1249 <main+0xb9>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
js     125f <main+0xcf>
mov    0x18(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     1275 <main+0xe5>
mov    0x10(%rsp),%r12
cmp    %r12,%r14
jge    129a <main+0x10a>
mov    %r12,%rax
neg    %rax
cmp    %rax,%r14
jge    12b9 <main+0x129>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
jmp    12aa <main+0x11a>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xda3(%rip),%rsi
jmp    1289 <main+0xf9>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xda6(%rip),%rsi
jmp    1289 <main+0xf9>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xda9(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    11d2 <main+0x42>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11cd <main+0x3d>
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10a0 <lock_whole_file@plt>
test   %rax,%rax
jle    1316 <main+0x186>
mov    %r14,%r15
sar    $0x3f,%r15
and    %r12,%r15
add    %r14,%r15
mov    %ebp,%edi
mov    %r15,%rsi
call   1030 <ftruncate@plt>
test   %rax,%rax
jns    1308 <main+0x178>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xda5(%rip),%rsi
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    11d2 <main+0x42>
mov    $0x1,%eax
jmp    11d2 <main+0x42>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"cannot open file %s: %m\0A\00"
@.str.1 = [25 x i8] c"cannot stat file %s: %m\0A\00"
@.str.2 = [26 x i8] c"%s is not a regular file\0A\00"
@.str.3 = [42 x i8] c"truncate position %lld after end of file\0A\00"
@.str.4 = [45 x i8] c"truncate position %lld before start of file\0A\00"
@F_WRLCK = global i32 0
@.str.5 = [46 x i8] c"cannot truncate file %s at position %lld: %m\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (...) @usage()
store i32 2, i32* %3
br label %117
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = call i64 @strtoll(i8* %17, i8** %6, i32 0)
store i64 %18, i64* %9
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %32
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %21
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %26, %21, %14
%33 = call i32 (...) @usage()
store i32 2, i32* %3
br label %117
34: ; preds = %26
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = load i32, i32* @O_WRONLY
%39 = call i32 @open(i8* %37, i32 %38)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %34
%43 = load i32, i32* @stderr
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %46)
store i32 1, i32* %3
br label %117
48: ; preds = %34
%49 = load i32, i32* %8
%50 = call i64 @fstat(i32 %49, %struct.stat* %7)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %56)
store i32 1, i32* %3
br label %117
58: ; preds = %48
%59 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%60 = load i32, i32* %59
%61 = call i32 @S_ISREG(i32 %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %69, label %63
63: ; preds = %58
%64 = load i32, i32* @stderr
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 1
%67 = load i8*, i8** %66
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %67)
store i32 1, i32* %3
br label %117
69: ; preds = %58
%70 = load i64, i64* %9
%71 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%72 = load i64, i64* %71
%73 = icmp sge i64 %70, %72
br i1 %73, label %74, label %78
74: ; preds = %69
%75 = load i32, i32* @stderr
%76 = load i64, i64* %9
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 %76)
store i32 2, i32* %3
br label %117
78: ; preds = %69
%79 = load i64, i64* %9
%80 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%81 = load i64, i64* %80
%82 = sub nsw i64 0, %81
%83 = icmp slt i64 %79, %82
br i1 %83, label %84, label %88
84: ; preds = %78
%85 = load i32, i32* @stderr
%86 = load i64, i64* %9
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %85, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i64 %86)
store i32 2, i32* %3
br label %117
88: ; preds = %78
%89 = load i64, i64* %9
%90 = icmp slt i64 %89, 0
br i1 %90, label %91, label %96
91: ; preds = %88
%92 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%93 = load i64, i64* %92
%94 = load i64, i64* %9
%95 = add nsw i64 %94, %93
store i64 %95, i64* %9
br label %96
96: ; preds = %91, %88
%97 = load i32, i32* %8
%98 = load i32, i32* @F_WRLCK
%99 = call i64 @lock_whole_file(i32 %97, i32 %98)
%100 = icmp sle i64 %99, 0
br i1 %100, label %101, label %102
101: ; preds = %96
store i32 1, i32* %3
br label %117
102: ; preds = %96
%103 = load i32, i32* %8
%104 = load i64, i64* %9
%105 = call i64 @ftruncate(i32 %103, i64 %104)
%106 = icmp slt i64 %105, 0
br i1 %106, label %107, label %114
107: ; preds = %102
%108 = load i32, i32* @stderr
%109 = load i8**, i8*** %5
%110 = getelementptr inbounds i8*, i8** %109, i64 1
%111 = load i8*, i8** %110
%112 = load i64, i64* %9
%113 = call i32 (i32, i8*, ...) @fprintf(i32 %108, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i8* %111, i64 %112)
br label %114
114: ; preds = %107, %102
%115 = load i32, i32* %8
%116 = call i32 @close(i32 %115)
store i32 0, i32* %3
br label %117
117: ; preds = %114, %101, %84, %74, %63, %52, %42, %32, %12
%118 = load i32, i32* %3
ret i32 %118
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"cannot open file %s: %m\0A\00"
@.str.1 = [25 x i8] c"cannot stat file %s: %m\0A\00"
@.str.2 = [26 x i8] c"%s is not a regular file\0A\00"
@.str.3 = [42 x i8] c"truncate position %lld after end of file\0A\00"
@.str.4 = [45 x i8] c"truncate position %lld before start of file\0A\00"
@F_WRLCK = global i32 0
@.str.5 = [46 x i8] c"cannot truncate file %s at position %lld: %m\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = bitcast i8** %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 (...) @usage()
br label %80
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = call i64 @strtoll(i8* %12, i8** %3, i32 0)
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
br i1 %15, label %23, label %16
16: ; preds = %10
%17 = load i8, i8* %14
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i8*, i8** %11
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19, %16, %10
%24 = tail call i32 (...) @usage()
br label %80
25: ; preds = %19
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_WRONLY
%29 = tail call i32 @open(i8* %27, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %26
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %80
35: ; preds = %25
%36 = call i64 @fstat(i32 %29, %struct.stat* %4)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %26
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %80
42: ; preds = %35
%43 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%44 = load i32, i32* %43
%45 = tail call i32 @S_ISREG(i32 %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %26
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %80
51: ; preds = %42
%52 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%53 = load i64, i64* %52
%54 = icmp slt i64 %13, %53
br i1 %54, label %58, label %55
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = tail call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 %13)
br label %80
58: ; preds = %51
%59 = sub nsw i64 0, %53
%60 = icmp slt i64 %13, %59
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i32, i32* @stderr
%63 = tail call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i64 %13)
br label %80
64: ; preds = %58
%65 = icmp slt i64 %13, 0
%66 = select i1 %65, i64 %53, i64 0
%67 = add nsw i64 %66, %13
%68 = load i32, i32* @F_WRLCK
%69 = tail call i64 @lock_whole_file(i32 %29, i32 %68)
%70 = icmp slt i64 %69, 1
br i1 %70, label %80, label %71
71: ; preds = %64
%72 = tail call i64 @ftruncate(i32 %29, i64 %67)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %71
%75 = load i32, i32* @stderr
%76 = load i8*, i8** %26
%77 = tail call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i8* %76, i64 %67)
br label %78
78: ; preds = %74, %71
%79 = tail call i32 @close(i32 %29)
br label %80
80: ; preds = %23, %31, %38, %47, %55, %61, %78, %64, %8
%81 = phi i32 [ 2, %8 ], [ 2, %23 ], [ 1, %31 ], [ 1, %38 ], [ 2, %55 ], [ 2, %61 ], [ 0, %78 ], [ 1, %47 ], [ 1, %64 ]
ret i32 %81
}