#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 utimbuf {
  int /*<<< orphan*/ modtime;
  int /*<<< orphan*/ actime;
};
struct stat {
  int st_mode;
  int /*<<< orphan*/ st_gid;
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_mtime;
  int /*<<< orphan*/ st_atime;
};

/* Variables and functions */
scalar_t__ EPERM;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int S_ISGID;
int S_ISUID;
int S_ISVTX;
scalar_t__ chmod(char const *, int);
scalar_t__ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ utime(char const *, struct utimbuf *);
int setfile(const char *path, struct stat *fs) {
  ;
  ;
  struct utimbuf ut;
  int rval;
  rval = 0;
  fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
  ut.actime = fs->st_atime;
  ut.modtime = fs->st_mtime;
  if (utime(path, &ut)) {
    do {
    } while (0);
    rval = 1;
  }
  if (chown(path, fs->st_uid, fs->st_gid)) {
    if (errno != EPERM) {
      do {
      } while (0);
      rval = 1;
    }
    fs->st_mode &= ~(S_ISVTX | S_ISUID | S_ISGID);
  }
  if (chmod(path, fs->st_mode)) {
    do {
    } while (0);
    rval = 1;
  }
  return rval;
}


x86 assembly (clang, -O0):
<setfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e61(%rip),%rax
or     (%rax),%ecx
mov    0x2e80(%rip),%rax
or     (%rax),%ecx
mov    0x2e67(%rip),%rax
or     (%rax),%ecx
mov    0x2e7e(%rip),%rax
or     (%rax),%ecx
mov    -0x10(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <utime@plt>
cmp    $0x0,%rax
je     11b8 <setfile+0x88>
jmp    11ac <setfile+0x7c>
jmp    11b1 <setfile+0x81>
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edx
call   1040 <chown@plt>
cmp    $0x0,%rax
je     122a <setfile+0xfa>
mov    0x2dd0(%rip),%rax
mov    (%rax),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
je     1204 <setfile+0xd4>
jmp    11f8 <setfile+0xc8>
jmp    11fd <setfile+0xcd>
movl   $0x1,-0x1c(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%ecx
mov    0x2db4(%rip),%rax
or     (%rax),%ecx
mov    0x2d9b(%rip),%rax
or     (%rax),%ecx
xor    $0xffffffff,%ecx
mov    -0x10(%rbp),%rax
and    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1050 <chmod@plt>
cmp    $0x0,%rax
je     1254 <setfile+0x124>
jmp    1248 <setfile+0x118>
jmp    124d <setfile+0x11d>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e85(%rip),%r12
mov    0x2e6e(%rip),%r15
mov    (%r15),%eax
or     (%r12),%eax
mov    0x2e88(%rip),%rcx
or     (%rcx),%eax
mov    0x2e6f(%rip),%rcx
or     (%rcx),%eax
mov    0x2e86(%rip),%rcx
or     (%rcx),%eax
mov    %rsi,%rbx
mov    %rdi,%r14
and    %eax,(%rsi)
mov    0x10(%rsi),%eax
mov    %eax,0xc(%rsp)
mov    0xc(%rsi),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
call   1030 <utime@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %r14,%rdi
call   1040 <chown@plt>
test   %rax,%rax
je     11dc <setfile+0xac>
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
mov    $0x1,%eax
cmovne %eax,%ebp
mov    0x2e0d(%rip),%rax
mov    (%r12),%esi
or     (%rax),%esi
or     (%r15),%esi
not    %esi
and    (%rbx),%esi
mov    %esi,(%rbx)
jmp    11de <setfile+0xae>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1050 <chmod@plt>
test   %rax,%rax
mov    $0x1,%eax
cmove  %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32, i32, i32 }
%struct.utimbuf = type { i32, i32 }
@S_ISUID = global i32 0
@S_ISGID = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@S_ISVTX = global i32 0
define i32 @setfile(i8* %0, %struct.stat* %1) {
%3 = alloca i8*
%4 = alloca %struct.stat*
%5 = alloca %struct.utimbuf
%6 = alloca i32
store i8* %0, i8** %3
store %struct.stat* %1, %struct.stat** %4
store i32 0, i32* %6
%7 = load i32, i32* @S_ISUID
%8 = load i32, i32* @S_ISGID
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRWXG
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRWXO
%15 = or i32 %13, %14
%16 = load %struct.stat*, %struct.stat** %4
%17 = getelementptr inbounds %struct.stat, %struct.stat* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = and i32 %18, %15
store i32 %19, i32* %17
%20 = load %struct.stat*, %struct.stat** %4
%21 = getelementptr inbounds %struct.stat, %struct.stat* %20, i32 0, i32 4
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %5, i32 0, i32 1
store i32 %22, i32* %23
%24 = load %struct.stat*, %struct.stat** %4
%25 = getelementptr inbounds %struct.stat, %struct.stat* %24, i32 0, i32 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %5, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i8*, i8** %3
%29 = call i64 @utime(i8* %28, %struct.utimbuf* %5)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %2
br label %32
32: ; preds = %31
br label %33
33: ; preds = %32
store i32 1, i32* %6
br label %34
34: ; preds = %33, %2
%35 = load i8*, i8** %3
%36 = load %struct.stat*, %struct.stat** %4
%37 = getelementptr inbounds %struct.stat, %struct.stat* %36, i32 0, i32 2
%38 = load i32, i32* %37
%39 = load %struct.stat*, %struct.stat** %4
%40 = getelementptr inbounds %struct.stat, %struct.stat* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = call i64 @chown(i8* %35, i32 %38, i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %62
44: ; preds = %34
%45 = load i64, i64* @errno
%46 = load i64, i64* @EPERM
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49
store i32 1, i32* %6
br label %51
51: ; preds = %50, %44
%52 = load i32, i32* @S_ISVTX
%53 = load i32, i32* @S_ISUID
%54 = or i32 %52, %53
%55 = load i32, i32* @S_ISGID
%56 = or i32 %54, %55
%57 = xor i32 %56, -1
%58 = load %struct.stat*, %struct.stat** %4
%59 = getelementptr inbounds %struct.stat, %struct.stat* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = and i32 %60, %57
store i32 %61, i32* %59
br label %62
62: ; preds = %51, %34
%63 = load i8*, i8** %3
%64 = load %struct.stat*, %struct.stat** %4
%65 = getelementptr inbounds %struct.stat, %struct.stat* %64, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i64 @chmod(i8* %63, i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %62
br label %70
70: ; preds = %69
br label %71
71: ; preds = %70
store i32 1, i32* %6
br label %72
72: ; preds = %71, %62
%73 = load i32, i32* %6
ret i32 %73
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32, i32, i32 }
%struct.utimbuf = type { i32, i32 }
@S_ISUID = global i32 0
@S_ISGID = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@S_ISVTX = global i32 0
define i32 @setfile(i8* nocapture %0, %struct.stat* nocapture %1) {
%3 = alloca %struct.utimbuf
%4 = bitcast %struct.utimbuf* %3 to i8*
%5 = load i32, i32* @S_ISUID
%6 = load i32, i32* @S_ISGID
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRWXU
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXG
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRWXO
%13 = or i32 %11, %12
%14 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, %13
store i32 %16, i32* %14
%17 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 4
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 3
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = call i64 @utime(i8* %0, %struct.utimbuf* %3)
%24 = icmp ne i64 %23, 0
%25 = zext i1 %24 to i32
%26 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 2
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%29 = load i32, i32* %28
%30 = tail call i64 @chown(i8* %0, i32 %27, i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %2
%33 = load i32, i32* %14
br label %47
34: ; preds = %2
%35 = load i64, i64* @errno
%36 = load i64, i64* @EPERM
%37 = icmp eq i64 %35, %36
%38 = select i1 %37, i32 %25, i32 1
%39 = load i32, i32* @S_ISVTX
%40 = load i32, i32* @S_ISUID
%41 = or i32 %40, %39
%42 = load i32, i32* @S_ISGID
%43 = or i32 %41, %42
%44 = xor i32 %43, -1
%45 = load i32, i32* %14
%46 = and i32 %45, %44
store i32 %46, i32* %14
br label %47
47: ; preds = %32, %34
%48 = phi i32 [ %46, %34 ], [ %33, %32 ]
%49 = phi i32 [ %38, %34 ], [ %25, %32 ]
%50 = tail call i64 @chmod(i8* %0, i32 %48)
%51 = icmp eq i64 %50, 0
%52 = select i1 %51, i32 %49, i32 1
ret i32 %52
}