#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*/ FILE;

/* Variables and functions */
scalar_t__ chmod(unsigned char const *, int);
scalar_t__ chown(unsigned char const *, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned char const *,
                           char *);
char *os_ErrorMsg();
int file_perms_set(FILE *flog, const unsigned char *path, int group, int mode,
                   int old_group, int old_mode) {
  if (group > 0) {
    if (chown(path, -1, group) < 0) {
      fprintf(flog, "chown: %s: %s\n", path, os_ErrorMsg());
    }
  } else if (old_group > 0) {
    chown(path, -1, old_group);
  }
  if (mode > 0) {
    if (chmod(path, mode) < 0) {
      fprintf(flog, "chmod: %s: %s\n", path, os_ErrorMsg());
    }
  } else if (old_mode > 0) {
    chmod(path, old_mode);
  }
  return 0;
}


x86 assembly (clang, -O0):
<file_perms_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11b6 <file_perms_set+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0xffffffff,%esi
call   1040 <chown@plt>
cmp    $0x0,%rax
jge    11b1 <file_perms_set+0x71>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1060 <os_ErrorMsg@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe54(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11d6 <file_perms_set+0x96>
cmpl   $0x0,-0x1c(%rbp)
jle    11d1 <file_perms_set+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0xffffffff,%esi
call   1040 <chown@plt>
jmp    11d6 <file_perms_set+0x96>
cmpl   $0x0,-0x18(%rbp)
jle    1229 <file_perms_set+0xe9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <chmod@plt>
cmp    $0x0,%rax
jge    1224 <file_perms_set+0xe4>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1060 <os_ErrorMsg@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf0(%rip),%rsi
call   1030 <fprintf@plt>
jmp    1244 <file_perms_set+0x104>
cmpl   $0x0,-0x20(%rbp)
jle    123f <file_perms_set+0xff>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1050 <chmod@plt>
jmp    1244 <file_perms_set+0x104>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_perms_set>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r9d,%r15d
mov    %ecx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
test   %edx,%edx
jle    1186 <file_perms_set+0x46>
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1040 <chown@plt>
test   %rax,%rax
jns    119b <file_perms_set+0x5b>
xor    %eax,%eax
call   1060 <os_ErrorMsg@plt>
lea    0xe8a(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
jmp    119b <file_perms_set+0x5b>
test   %r8d,%r8d
jle    119b <file_perms_set+0x5b>
mov    %rbx,%rdi
mov    $0xffffffff,%esi
mov    %r8d,%edx
call   1040 <chown@plt>
test   %ebp,%ebp
jle    11cc <file_perms_set+0x8c>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <chmod@plt>
test   %rax,%rax
jns    11dc <file_perms_set+0x9c>
xor    %eax,%eax
call   1060 <os_ErrorMsg@plt>
lea    0xe53(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
jmp    11dc <file_perms_set+0x9c>
test   %r15d,%r15d
jle    11dc <file_perms_set+0x9c>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1050 <chmod@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"chown: %s: %s\0A\00"
@.str.1 = [15 x i8] c"chmod: %s: %s\0A\00"
define i32 @file_perms_set(i32* %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%13 = load i32, i32* %9
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %6
%16 = load i8*, i8** %8
%17 = load i32, i32* %9
%18 = call i64 @chown(i8* %16, i32 -1, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i32*, i32** %7
%22 = load i8*, i8** %8
%23 = call i8* (...) @os_ErrorMsg()
%24 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %23)
br label %25
25: ; preds = %20, %15
br label %34
26: ; preds = %6
%27 = load i32, i32* %11
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i8*, i8** %8
%31 = load i32, i32* %11
%32 = call i64 @chown(i8* %30, i32 -1, i32 %31)
br label %33
33: ; preds = %29, %26
br label %34
34: ; preds = %33, %25
%35 = load i32, i32* %10
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %48
37: ; preds = %34
%38 = load i8*, i8** %8
%39 = load i32, i32* %10
%40 = call i64 @chmod(i8* %38, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i32*, i32** %7
%44 = load i8*, i8** %8
%45 = call i8* (...) @os_ErrorMsg()
%46 = call i32 @fprintf(i32* %43, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %44, i8* %45)
br label %47
47: ; preds = %42, %37
br label %56
48: ; preds = %34
%49 = load i32, i32* %12
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i8*, i8** %8
%53 = load i32, i32* %12
%54 = call i64 @chmod(i8* %52, i32 %53)
br label %55
55: ; preds = %51, %48
br label %56
56: ; preds = %55, %47
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"chown: %s: %s\0A\00"
@.str.1 = [15 x i8] c"chmod: %s: %s\0A\00"
define i32 @file_perms_set(i32* nocapture %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %14
8: ; preds = %6
%9 = tail call i64 @chown(i8* %1, i32 -1, i32 %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = tail call i8* (...) @os_ErrorMsg()
%13 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %12)
br label %18
14: ; preds = %6
%15 = icmp sgt i32 %4, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i64 @chown(i8* %1, i32 -1, i32 %4)
br label %18
18: ; preds = %14, %16, %8, %11
%19 = icmp sgt i32 %3, 0
br i1 %19, label %20, label %26
20: ; preds = %18
%21 = tail call i64 @chmod(i8* %1, i32 %3)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = tail call i8* (...) @os_ErrorMsg()
%25 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %24)
br label %30
26: ; preds = %18
%27 = icmp sgt i32 %5, 0
br i1 %27, label %28, label %30
28: ; preds = %26
%29 = tail call i64 @chmod(i8* %1, i32 %5)
br label %30
30: ; preds = %26, %28, %20, %23
ret i32 0
}