#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*/ uid_t;
typedef int /*<<< orphan*/ mode_t;
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ assert(char const *);
scalar_t__ chmod(char const *, int /*<<< orphan*/);
scalar_t__ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
  assert(path);
  if (mode != ((mode_t)-1))
    if (chmod(path, mode) < 0)
      return -errno;
  if (uid != ((uid_t)-1) || gid != ((gid_t)-1))
    if (chown(path, uid, gid) < 0)
      return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<chmod_and_chown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <assert@plt>
cmpl   $0xffffffff,-0x14(%rbp)
je     1186 <chmod_and_chown+0x56>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <chmod@plt>
cmp    $0x0,%rax
jge    1181 <chmod_and_chown+0x51>
xor    %eax,%eax
mov    0x2e69(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <chmod_and_chown+0xa2>
jmp    1186 <chmod_and_chown+0x56>
cmpl   $0xffffffff,-0x18(%rbp)
jne    119a <chmod_and_chown+0x6a>
cmpl   $0xffffffff,-0x1c(%rbp)
je     11cb <chmod_and_chown+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1040 <chown@plt>
cmp    $0x0,%rax
jge    11c6 <chmod_and_chown+0x96>
xor    %eax,%eax
mov    0x2e24(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <chmod_and_chown+0xa2>
jmp    11cb <chmod_and_chown+0x9b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chmod_and_chown>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%rbp
call   1030 <assert@plt>
cmp    $0xffffffff,%ebx
je     115b <chmod_and_chown+0x2b>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1050 <chmod@plt>
test   %rax,%rax
js     117b <chmod_and_chown+0x4b>
mov    %r14d,%eax
and    %r15d,%eax
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     1186 <chmod_and_chown+0x56>
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
call   1040 <chown@plt>
test   %rax,%rax
jns    1186 <chmod_and_chown+0x56>
mov    0x2e5e(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @chmod_and_chown(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%10 = load i8*, i8** %6
%11 = call i32 @assert(i8* %10)
%12 = load i32, i32* %7
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %23
14: ; preds = %4
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i64 @chmod(i8* %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
store i32 %21, i32* %5
br label %40
22: ; preds = %14
br label %23
23: ; preds = %22, %4
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, -1
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* %9
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %39
29: ; preds = %26, %23
%30 = load i8*, i8** %6
%31 = load i32, i32* %8
%32 = load i32, i32* %9
%33 = call i64 @chown(i8* %30, i32 %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %5
br label %40
38: ; preds = %29
br label %39
39: ; preds = %38, %26
store i32 0, i32* %5
br label %40
40: ; preds = %39, %35, %19
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @chmod_and_chown(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @assert(i8* %0)
%6 = icmp eq i32 %1, -1
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = tail call i64 @chmod(i8* %0, i32 %1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %22
13: ; preds = %7, %4
%14 = and i32 %3, %2
%15 = icmp eq i32 %14, -1
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = tail call i64 @chown(i8* %0, i32 %2, i32 %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
br label %22
22: ; preds = %13, %16, %19, %10
%23 = phi i32 [ %12, %10 ], [ %21, %19 ], [ 0, %16 ], [ 0, %13 ]
ret i32 %23
}