#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;
  scalar_t__ st_nlink;
};

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int close(int);
int errno;
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
int truncate(char *, int);
int unlink(char *);
int test_truncate(void) {
  int fd, rc;
  struct stat buf;
  char *tmpname = "test-truncate.dat";
  fd = open(tmpname, O_CREAT | O_RDWR, S_IRWXU);
  if (fd < 0) {
    printf("Fail to create file %s, errno = %d at line %d!\n", tmpname, errno,
           371);
    return (1);
  }
  rc = close(fd);
  if (rc < 0) {
    printf("Fail to close file %s, errno = %d at line %d!\n", tmpname, errno,
           376);
    return (1);
  }
  rc = truncate(tmpname, 1234567);
  if (rc < 0) {
    printf("Fail to lengthen file %s, errno = %d at line %d!\n", tmpname, errno,
           381);
    return (1);
  }
  rc = truncate(tmpname, 567);
  if (rc < 0) {
    printf("Fail to shorten file %s, errno = %d at line %d!\n", tmpname, errno,
           389);
    return (1);
  }
  rc = stat(tmpname, &buf);
  if (rc < 0) {
    printf("Fail to stat file %s, errno = %d at line %d!\n", tmpname, errno,
           394);
    return (1);
  }
  if (buf.st_size != 567) {
    printf("Unexpected size %d, errno = %d at line %d!\n", (int)buf.st_nlink,
           errno, 398);
    return (1);
  }
  rc = unlink(tmpname);
  if (rc < 0) {
    printf("Fail to unlink %s, errno = %d at line %d!\n", tmpname, errno, 403);
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<test_truncate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d0 <test_truncate+0x70>
mov    -0x28(%rbp),%rsi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
lea    0xe5a(%rip),%rdi
mov    $0x173,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <test_truncate+0x209>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1211 <test_truncate+0xb1>
mov    -0x28(%rbp),%rsi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
lea    0xe49(%rip),%rdi
mov    $0x178,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <test_truncate+0x209>
mov    -0x28(%rbp),%rdi
mov    $0x12d687,%esi
call   1070 <truncate@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1258 <test_truncate+0xf8>
mov    -0x28(%rbp),%rsi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
lea    0xe31(%rip),%rdi
mov    $0x17d,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <test_truncate+0x209>
mov    -0x28(%rbp),%rdi
mov    $0x237,%esi
call   1070 <truncate@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    129f <test_truncate+0x13f>
mov    -0x28(%rbp),%rsi
mov    0x2d4a(%rip),%rax
mov    (%rax),%edx
lea    0xe1c(%rip),%rdi
mov    $0x185,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <test_truncate+0x209>
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12e5 <test_truncate+0x185>
mov    -0x28(%rbp),%rsi
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
lea    0xe07(%rip),%rdi
mov    $0x18a,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <test_truncate+0x209>
cmpl   $0x237,-0x20(%rbp)
je     1320 <test_truncate+0x1c0>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    0x2cc9(%rip),%rax
mov    (%rax),%edx
lea    0xdfa(%rip),%rdi
mov    $0x18e,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <test_truncate+0x209>
mov    -0x28(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1362 <test_truncate+0x202>
mov    -0x28(%rbp),%rsi
mov    0x2c87(%rip),%rax
mov    (%rax),%edx
lea    0xde4(%rip),%rdi
mov    $0x193,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <test_truncate+0x209>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_truncate>:
sub    $0x18,%rsp
mov    0x2e8d(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
lea    0xe7a(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1234 <test_truncate+0xd4>
mov    %eax,%edi
call   1050 <close@plt>
test   %eax,%eax
js     1252 <test_truncate+0xf2>
lea    0xe57(%rip),%rdi
mov    $0x12d687,%esi
call   1070 <truncate@plt>
test   %eax,%eax
js     1270 <test_truncate+0x110>
lea    0xe3e(%rip),%rdi
mov    $0x237,%esi
call   1070 <truncate@plt>
test   %eax,%eax
js     128e <test_truncate+0x12e>
lea    0xe25(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %eax,%eax
js     12ac <test_truncate+0x14c>
cmpl   $0x237,0x8(%rsp)
jne    12d9 <test_truncate+0x179>
lea    0xdfe(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    12d4 <test_truncate+0x174>
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
lea    0xf0b(%rip),%rdi
lea    0xdd6(%rip),%rsi
mov    $0x193,%ecx
jmp    12c8 <test_truncate+0x168>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
lea    0xdce(%rip),%rdi
lea    0xdb5(%rip),%rsi
mov    $0x173,%ecx
jmp    12c8 <test_truncate+0x168>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
lea    0xde0(%rip),%rdi
lea    0xd97(%rip),%rsi
mov    $0x178,%ecx
jmp    12c8 <test_truncate+0x168>
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
lea    0xdf1(%rip),%rdi
lea    0xd79(%rip),%rsi
mov    $0x17d,%ecx
jmp    12c8 <test_truncate+0x168>
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
lea    0xe05(%rip),%rdi
lea    0xd5b(%rip),%rsi
mov    $0x185,%ecx
jmp    12c8 <test_truncate+0x168>
mov    0x2d15(%rip),%rax
mov    (%rax),%edx
lea    0xe18(%rip),%rdi
lea    0xd3d(%rip),%rsi
mov    $0x18a,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
ret
mov    0x10(%rsp),%esi
mov    0x2ce4(%rip),%rax
mov    (%rax),%edx
lea    0xe15(%rip),%rdi
mov    $0x18e,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12cf <test_truncate+0x16f>

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@.str = [18 x i8] c"test-truncate.dat\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [48 x i8] c"Fail to create file %s, errno = %d at line %d!\0A\00"
@errno = global i32 0
@.str.2 = [47 x i8] c"Fail to close file %s, errno = %d at line %d!\0A\00"
@.str.3 = [50 x i8] c"Fail to lengthen file %s, errno = %d at line %d!\0A\00"
@.str.4 = [49 x i8] c"Fail to shorten file %s, errno = %d at line %d!\0A\00"
@.str.5 = [46 x i8] c"Fail to stat file %s, errno = %d at line %d!\0A\00"
@.str.6 = [44 x i8] c"Unexpected size %d, errno = %d at line %d!\0A\00"
@.str.7 = [43 x i8] c"Fail to unlink %s, errno = %d at line %d!\0A\00"
define i32 @test_truncate() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = call i32 @open(i8* %6, i32 %9, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = load i8*, i8** %5
%16 = load i32, i32* @errno
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %16, i32 371)
store i32 1, i32* %1
br label %74
18: ; preds = %0
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = load i32, i32* @errno
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %24, i32 %25, i32 376)
store i32 1, i32* %1
br label %74
27: ; preds = %18
%28 = load i8*, i8** %5
%29 = call i32 @truncate(i8* %28, i32 1234567)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i8*, i8** %5
%34 = load i32, i32* @errno
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i8* %33, i32 %34, i32 381)
store i32 1, i32* %1
br label %74
36: ; preds = %27
%37 = load i8*, i8** %5
%38 = call i32 @truncate(i8* %37, i32 567)
store i32 %38, i32* %3
%39 = load i32, i32* %3
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i8*, i8** %5
%43 = load i32, i32* @errno
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %42, i32 %43, i32 389)
store i32 1, i32* %1
br label %74
45: ; preds = %36
%46 = load i8*, i8** %5
%47 = call i32 @stat(i8* %46, %struct.stat* %4)
store i32 %47, i32* %3
%48 = load i32, i32* %3
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = load i8*, i8** %5
%52 = load i32, i32* @errno
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i8* %51, i32 %52, i32 394)
store i32 1, i32* %1
br label %74
54: ; preds = %45
%55 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%56 = load i32, i32* %55
%57 = icmp ne i32 %56, 567
br i1 %57, label %58, label %64
58: ; preds = %54
%59 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%60 = load i64, i64* %59
%61 = trunc i64 %60 to i32
%62 = load i32, i32* @errno
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i32 %61, i32 %62, i32 398)
store i32 1, i32* %1
br label %74
64: ; preds = %54
%65 = load i8*, i8** %5
%66 = call i32 @unlink(i8* %65)
store i32 %66, i32* %3
%67 = load i32, i32* %3
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %64
%70 = load i8*, i8** %5
%71 = load i32, i32* @errno
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* %70, i32 %71, i32 403)
store i32 1, i32* %1
br label %74
73: ; preds = %64
store i32 0, i32* %1
br label %74
74: ; preds = %73, %69, %58, %50, %41, %32, %23, %14
%75 = load i32, i32* %1
ret i32 %75
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@.str = [18 x i8] c"test-truncate.dat\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [48 x i8] c"Fail to create file %s, errno = %d at line %d!\0A\00"
@errno = global i32 0
@.str.2 = [47 x i8] c"Fail to close file %s, errno = %d at line %d!\0A\00"
@.str.3 = [50 x i8] c"Fail to lengthen file %s, errno = %d at line %d!\0A\00"
@.str.4 = [49 x i8] c"Fail to shorten file %s, errno = %d at line %d!\0A\00"
@.str.5 = [46 x i8] c"Fail to stat file %s, errno = %d at line %d!\0A\00"
@.str.6 = [44 x i8] c"Unexpected size %d, errno = %d at line %d!\0A\00"
@.str.7 = [43 x i8] c"Fail to unlink %s, errno = %d at line %d!\0A\00"
define i32 @test_truncate() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRWXU
%7 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %10, i32 371)
br label %52
12: ; preds = %0
%13 = tail call i32 @close(i32 %7)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %16, i32 376)
br label %52
18: ; preds = %12
%19 = tail call i32 @truncate(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 1234567)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %22, i32 381)
br label %52
24: ; preds = %18
%25 = tail call i32 @truncate(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 567)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %28, i32 389)
br label %52
30: ; preds = %24
%31 = call i32 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* @errno
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %34, i32 394)
br label %52
36: ; preds = %30
%37 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%38 = load i32, i32* %37
%39 = icmp eq i32 %38, 567
br i1 %39, label %46, label %40
40: ; preds = %36
%41 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%42 = load i64, i64* %41
%43 = trunc i64 %42 to i32
%44 = load i32, i32* @errno
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i32 %43, i32 %44, i32 398)
br label %52
46: ; preds = %36
%47 = tail call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* @errno
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %50, i32 403)
br label %52
52: ; preds = %46, %49, %40, %33, %27, %21, %15, %9
%53 = phi i32 [ 1, %9 ], [ 1, %15 ], [ 1, %21 ], [ 1, %27 ], [ 1, %33 ], [ 1, %40 ], [ 1, %49 ], [ 0, %46 ]
ret i32 %53
}