#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 scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EPERM;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int WEXITSTATUS(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getuid();
int open(char *, int, int);
scalar_t__ setuid(int);
int /*<<< orphan*/ stderr;
scalar_t__ sys_waitpid(scalar_t__, int *, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int);
main() {
  int fd;
  char buf[1024];
  pid_t pid;
  if (getuid() != 0) {
    fprintf(stderr, "ERROR: This test must be run as root - assuming ftruncate "
                    "doesn't need root.\n");
    exit(1);
  }
  fd = open("conftest.truncroot", O_RDWR | O_CREAT | O_TRUNC, 0666);
  if (!fd)
    exit(1);
  if (write(fd, buf, 1024) != 1024)
    exit(1);
  if ((pid = fork()) < 0)
    exit(1);
  if (pid) {
    int status = 1;
    if (sys_waitpid(pid, &status, 0) != pid) {
      unlink("conftest.truncroot");
      exit(1);
    }
    unlink("conftest.truncroot");
    exit(WEXITSTATUS(status));
  }
  if (setuid(500) < 0)
    exit(1);
  if (ftruncate(fd, 0) < 0) {
    if (errno == EPERM || errno == EACCES)
      exit(0);
  }
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
je     11f2 <main+0x42>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
mov    0x2de5(%rip),%rax
or     (%rax),%esi
lea    0xe3a(%rip),%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1235 <main+0x85>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <write@plt>
cmp    $0x400,%eax
je     125e <main+0xae>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
jge    1280 <main+0xd0>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x418(%rbp)
je     12ee <main+0x13e>
movl   $0x1,-0x41c(%rbp)
mov    -0x418(%rbp),%rdi
lea    -0x41c(%rbp),%rsi
xor    %edx,%edx
call   10a0 <sys_waitpid@plt>
cmp    -0x418(%rbp),%rax
je     12d0 <main+0x120>
lea    0xd8d(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd77(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x41c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%edi
call   10b0 <exit@plt>
mov    $0x1f4,%edi
call   10c0 <setuid@plt>
cmp    $0x0,%rax
jge    130c <main+0x15c>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
jge    1360 <main+0x1b0>
mov    0x2c91(%rip),%rax
mov    (%rax),%rax
mov    0x2c77(%rip),%rcx
cmp    (%rcx),%rax
je     1354 <main+0x1a4>
mov    0x2c77(%rip),%rax
mov    (%rax),%rax
mov    0x2c85(%rip),%rcx
cmp    (%rcx),%rax
jne    135b <main+0x1ab>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1360 <main+0x1b0>
mov    $0x1,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x418,%rsp
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
jne    126c <main+0xbc>
mov    0x2e07(%rip),%rax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
lea    0xe62(%rip),%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
test   %eax,%eax
je     1281 <main+0xd1>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1040 <write@plt>
cmp    $0x400,%eax
jne    1281 <main+0xd1>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1281 <main+0xd1>
jne    128b <main+0xdb>
mov    $0x1f4,%edi
call   10c0 <setuid@plt>
test   %rax,%rax
js     1281 <main+0xd1>
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
jns    1281 <main+0xd1>
mov    0x2d6e(%rip),%rax
mov    (%rax),%rax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
je     1265 <main+0xb5>
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%rax
jne    1281 <main+0xd1>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xd84(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rax,%r14
movl   $0x1,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   10a0 <sys_waitpid@plt>
mov    %rax,%rbx
lea    0xd9f(%rip),%rdi
call   1030 <unlink@plt>
cmp    %r14,%rbx
jne    1281 <main+0xd1>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [78 x i8] c"ERROR: This test must be run as root - assuming ftruncate doesn't need root.\0A\00"
@.str.1 = [19 x i8] c"conftest.truncroot\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@EACCES = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
%6 = call i64 (...) @getuid()
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 438)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %12
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %12
%24 = load i32, i32* %2
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%26 = call i32 @write(i32 %24, i8* %25, i32 1024)
%27 = icmp ne i32 %26, 1024
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = call i64 (...) @fork()
store i64 %31, i64* %4
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %30
%36 = load i64, i64* %4
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %51
38: ; preds = %35
store i32 1, i32* %5
%39 = load i64, i64* %4
%40 = call i64 @sys_waitpid(i64 %39, i32* %5, i32 0)
%41 = load i64, i64* %4
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %38
%47 = call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32, i32* %5
%49 = call i32 @WEXITSTATUS(i32 %48)
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %35
%52 = call i64 @setuid(i32 500)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %51
%57 = load i32, i32* %2
%58 = call i64 @ftruncate(i32 %57, i32 0)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %71
60: ; preds = %56
%61 = load i64, i64* @errno
%62 = load i64, i64* @EPERM
%63 = icmp eq i64 %61, %62
br i1 %63, label %68, label %64
64: ; preds = %60
%65 = load i64, i64* @errno
%66 = load i64, i64* @EACCES
%67 = icmp eq i64 %65, %66
br i1 %67, label %68, label %70
68: ; preds = %64, %60
%69 = call i32 @exit(i32 0)
unreachable
70: ; preds = %64
br label %71
71: ; preds = %70, %56
%72 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [78 x i8] c"ERROR: This test must be run as root - assuming ftruncate doesn't need root.\0A\00"
@.str.1 = [19 x i8] c"conftest.truncroot\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@EACCES = global i64 0
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = tail call i64 (...) @getuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 438)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = call i32 @write(i32 %16, i8* %3, i32 1024)
%22 = icmp eq i32 %21, 1024
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %20
%26 = tail call i64 (...) @fork()
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %25
%31 = icmp eq i64 %26, 0
br i1 %31, label %43, label %32
32: ; preds = %30
%33 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%34 = call i64 @sys_waitpid(i64 %26, i32* %2, i32 0)
%35 = icmp eq i64 %34, %26
%36 = call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br i1 %35, label %39, label %37
37: ; preds = %32
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32
%40 = load i32, i32* %2
%41 = call i32 @WEXITSTATUS(i32 %40)
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %30
%44 = tail call i64 @setuid(i32 500)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %43
%49 = tail call i64 @ftruncate(i32 %16, i32 0)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %60
51: ; preds = %48
%52 = load i64, i64* @errno
%53 = load i64, i64* @EPERM
%54 = icmp eq i64 %52, %53
%55 = load i64, i64* @EACCES
%56 = icmp eq i64 %52, %55
%57 = select i1 %54, i1 true, i1 %56
br i1 %57, label %58, label %60
58: ; preds = %51
%59 = tail call i32 @exit(i32 0)
unreachable
60: ; preds = %51, %48
%61 = tail call i32 @exit(i32 1)
unreachable
}