#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 uid_t;
struct stat {
  int /*<<< orphan*/ st_mode;
};
struct passwd {
  int pw_uid;
};
struct group {
  int gr_gid;
};
typedef int gid_t;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int chown(char const *, int, int);
int enum_opt_dir(int /*<<< orphan*/, char *, int, int /*<<< orphan*/, int, int);
struct group *getgrnam(char const *);
struct passwd *getpwnam(char const *);
int /*<<< orphan*/ stat(char const *, struct stat *);
int optChown(const char *src, const char *groupname, const char *username,
             const int isEnum) {
  int ret = 0;
  struct group *group;
  struct passwd *pwd;
  gid_t gid;
  uid_t uid;
  if ((group = getgrnam(groupname)) == NULL) {
    gid = -1;
  } else {
    gid = group->gr_gid;
  }
  if ((pwd = getpwnam(username)) == NULL) {
    uid = -1;
  } else {
    uid = pwd->pw_uid;
  }
  struct stat info;
  stat(src, &info);
  ret = chown(src, uid, gid);
  ;
  if (0 == ret) {
    if (S_ISDIR(info.st_mode)) {
      if (isEnum) {
        ret = enum_opt_dir(0, (char *)src, 4, 0, uid, gid);
      }
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<optChown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <getgrnam@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a1 <optChown+0x41>
movl   $0xffffffff,-0x34(%rbp)
jmp    11aa <optChown+0x4a>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11cd <optChown+0x6d>
movl   $0xffffffff,-0x38(%rbp)
jmp    11d6 <optChown+0x76>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1070 <chown@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%eax
jne    1243 <optChown+0xe3>
mov    -0x40(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     123e <optChown+0xde>
cmpl   $0x0,-0x1c(%rbp)
je     1239 <optChown+0xd9>
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%r8d
mov    -0x34(%rbp),%r9d
xor    %ecx,%ecx
mov    $0x4,%edx
mov    %ecx,%edi
call   1080 <enum_opt_dir@plt>
mov    %eax,-0x20(%rbp)
jmp    123e <optChown+0xde>
jmp    1243 <optChown+0xe3>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optChown>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r15d
mov    %rdx,%rbp
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1060 <getgrnam@plt>
mov    $0xffffffff,%r14d
mov    $0xffffffff,%r12d
test   %rax,%rax
je     1191 <optChown+0x31>
mov    (%rax),%r12d
mov    %rbp,%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     11a1 <optChown+0x41>
mov    (%rax),%r14d
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r12d,%edx
call   1070 <chown@plt>
test   %eax,%eax
je     11cd <optChown+0x6d>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %r15d,%r15d
je     11c0 <optChown+0x60>
test   %rcx,%rcx
je     11c0 <optChown+0x60>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    %r14d,%r8d
mov    %r12d,%r9d
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <enum_opt_dir@plt>

LLVM IR (-O0):
%struct.group = type { i32 }
%struct.passwd = type { i32 }
%struct.stat = type { i32 }
define i32 @optChown(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.group*
%11 = alloca %struct.passwd*
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
%15 = load i8*, i8** %6
%16 = call %struct.group* @getgrnam(i8* %15)
store %struct.group* %16, %struct.group** %10
%17 = icmp eq %struct.group* %16, null
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %12
br label %23
19: ; preds = %4
%20 = load %struct.group*, %struct.group** %10
%21 = getelementptr inbounds %struct.group, %struct.group* %20, i32 0, i32 0
%22 = load i32, i32* %21
store i32 %22, i32* %12
br label %23
23: ; preds = %19, %18
%24 = load i8*, i8** %7
%25 = call %struct.passwd* @getpwnam(i8* %24)
store %struct.passwd* %25, %struct.passwd** %11
%26 = icmp eq %struct.passwd* %25, null
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 -1, i32* %13
br label %32
28: ; preds = %23
%29 = load %struct.passwd*, %struct.passwd** %11
%30 = getelementptr inbounds %struct.passwd, %struct.passwd* %29, i32 0, i32 0
%31 = load i32, i32* %30
store i32 %31, i32* %13
br label %32
32: ; preds = %28, %27
%33 = load i8*, i8** %5
%34 = call i32 @stat(i8* %33, %struct.stat* %14)
%35 = load i8*, i8** %5
%36 = load i32, i32* %13
%37 = load i32, i32* %12
%38 = call i32 @chown(i8* %35, i32 %36, i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp eq i32 0, %39
br i1 %40, label %41, label %56
41: ; preds = %32
%42 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i64 @S_ISDIR(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %55
46: ; preds = %41
%47 = load i32, i32* %8
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = load i8*, i8** %5
%51 = load i32, i32* %13
%52 = load i32, i32* %12
%53 = call i32 @enum_opt_dir(i32 0, i8* %50, i32 4, i32 0, i32 %51, i32 %52)
store i32 %53, i32* %9
br label %54
54: ; preds = %49, %46
br label %55
55: ; preds = %54, %41
br label %56
56: ; preds = %55, %32
%57 = load i32, i32* %9
ret i32 %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.group = type { i32 }
%struct.passwd = type { i32 }
define i32 @optChown(i8* %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = alloca %struct.stat
%6 = tail call %struct.group* @getgrnam(i8* %1)
%7 = icmp eq %struct.group* %6, null
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = getelementptr inbounds %struct.group, %struct.group* %6, i64 0, i32 0
%10 = load i32, i32* %9
br label %11
11: ; preds = %4, %8
%12 = phi i32 [ %10, %8 ], [ -1, %4 ]
%13 = tail call %struct.passwd* @getpwnam(i8* %2)
%14 = icmp eq %struct.passwd* %13, null
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 0
%17 = load i32, i32* %16
br label %18
18: ; preds = %11, %15
%19 = phi i32 [ %17, %15 ], [ -1, %11 ]
%20 = bitcast %struct.stat* %5 to i8*
%21 = call i32 @stat(i8* %0, %struct.stat* %5)
%22 = tail call i32 @chown(i8* %0, i32 %19, i32 %12)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %18
%25 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%26 = load i32, i32* %25
%27 = tail call i64 @S_ISDIR(i32 %26)
%28 = icmp ne i64 %27, 0
%29 = icmp ne i32 %3, 0
%30 = and i1 %29, %28
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = tail call i32 @enum_opt_dir(i32 0, i8* %0, i32 4, i32 0, i32 %19, i32 %12)
br label %33
33: ; preds = %24, %31, %18
%34 = phi i32 [ %32, %31 ], [ 0, %24 ], [ %22, %18 ]
ret i32 %34
}