#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 O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ __close(int);
int __ftruncate(int, int /*<<< orphan*/);
int __open(char const *, int);
int /*<<< orphan*/ __set_errno(int);
int errno;
int __truncate(const char *path, off_t length) {
  int fd, ret, save;
  fd = __open(path, O_WRONLY | (length == 0 ? O_TRUNC : 0));
  if (fd < 0)
    return -1;
  if (length == 0)
    ret = 0;
  else
    ret = __ftruncate(fd, length);
  save = errno;
  (void)__close(fd);
  if (ret < 0)
    __set_errno(save);
  return ret;
}


x86 assembly (clang, -O0):
<__truncate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    117e <__truncate+0x3e>
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    1188 <__truncate+0x48>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    1188 <__truncate+0x48>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x34(%rbp),%eax
or     %eax,%esi
call   1040 <__open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b2 <__truncate+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <__truncate+0xc2>
cmpl   $0x0,-0x14(%rbp)
jne    11c8 <__truncate+0x88>
movl   $0x0,-0x1c(%rbp)
jmp    11d6 <__truncate+0x96>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <__ftruncate@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <__close@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    11fc <__truncate+0xbc>
mov    -0x20(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__truncate>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    0x2e7b(%rip),%rax
xor    %esi,%esi
test   %ebx,%ebx
jne    115c <__truncate+0x1c>
mov    0x2e9e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1040 <__open@plt>
test   %eax,%eax
js     1197 <__truncate+0x57>
mov    %eax,%ebp
test   %ebx,%ebx
je     119e <__truncate+0x5e>
mov    %ebp,%edi
mov    %ebx,%esi
call   1050 <__ftruncate@plt>
mov    %eax,%ebx
mov    0x2e59(%rip),%rax
mov    (%rax),%r14d
mov    %ebp,%edi
call   1060 <__close@plt>
test   %ebx,%ebx
jns    11a7 <__truncate+0x67>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
jmp    11a7 <__truncate+0x67>
mov    $0xffffffff,%ebx
jmp    11a7 <__truncate+0x67>
mov    %ebp,%edi
call   1060 <__close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
define i32 @__truncate(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* @O_TRUNC
br label %16
15: ; preds = %2
br label %16
16: ; preds = %15, %13
%17 = phi i32 [ %14, %13 ], [ 0, %15 ]
%18 = or i32 %10, %17
%19 = call i32 @__open(i8* %9, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %3
br label %42
23: ; preds = %16
%24 = load i32, i32* %5
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 0, i32* %7
br label %31
27: ; preds = %23
%28 = load i32, i32* %6
%29 = load i32, i32* %5
%30 = call i32 @__ftruncate(i32 %28, i32 %29)
store i32 %30, i32* %7
br label %31
31: ; preds = %27, %26
%32 = load i32, i32* @errno
store i32 %32, i32* %8
%33 = load i32, i32* %6
%34 = call i32 @__close(i32 %33)
%35 = load i32, i32* %7
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i32, i32* %8
%39 = call i32 @__set_errno(i32 %38)
br label %40
40: ; preds = %37, %31
%41 = load i32, i32* %7
store i32 %41, i32* %3
br label %42
42: ; preds = %40, %22
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
define i32 @__truncate(i8* %0, i32 %1) {
%3 = load i32, i32* @O_WRONLY
%4 = icmp eq i32 %1, 0
%5 = load i32, i32* @O_TRUNC
%6 = select i1 %4, i32 %5, i32 0
%7 = or i32 %6, %3
%8 = tail call i32 @__open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %2
br i1 %4, label %11, label %13
11: ; preds = %10
%12 = tail call i32 @__close(i32 %8)
br label %20
13: ; preds = %10
%14 = tail call i32 @__ftruncate(i32 %8, i32 %1)
%15 = load i32, i32* @errno
%16 = tail call i32 @__close(i32 %8)
%17 = icmp slt i32 %14, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @__set_errno(i32 %15)
br label %20
20: ; preds = %11, %13, %18, %2
%21 = phi i32 [ -1, %2 ], [ %14, %18 ], [ %14, %13 ], [ 0, %11 ]
ret i32 %21
}