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

/* Variables and functions */
int /*<<< orphan*/ chown(char *, int /*<<< orphan*/, int /*<<< orphan*/);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
void set_pid_ownership(char *user, char *group, char *pid_obj) {
  struct passwd *pwd;
  struct group *grp;
  pid_t uid = 0;
  gid_t gid = 0;
  if (group) {
    if ((grp = getgrnam(group)))
      gid = grp->gr_gid;
  }
  if (user) {
    if ((pwd = getpwnam(user)))
      uid = pwd->pw_uid;
  }
  if (pid_obj) {
    chown(pid_obj, uid, gid);
  }
}


x86 assembly (clang, -O0):
<set_pid_ownership>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1182 <set_pid_ownership+0x52>
mov    -0x10(%rbp),%rdi
call   1040 <getgrnam@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     117d <set_pid_ownership+0x4d>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
jmp    1182 <set_pid_ownership+0x52>
cmpq   $0x0,-0x8(%rbp)
je     11b2 <set_pid_ownership+0x82>
mov    -0x8(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ad <set_pid_ownership+0x7d>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11b2 <set_pid_ownership+0x82>
cmpq   $0x0,-0x18(%rbp)
je     11cc <set_pid_ownership+0x9c>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
call   1050 <chown@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_pid_ownership>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rdi,%rbx
xor    %r14d,%r14d
mov    $0x0,%ebp
test   %rsi,%rsi
je     115e <set_pid_ownership+0x2e>
mov    %rsi,%rdi
call   1040 <getgrnam@plt>
mov    $0x0,%ebp
test   %rax,%rax
je     115e <set_pid_ownership+0x2e>
mov    (%rax),%ebp
test   %rbx,%rbx
je     1173 <set_pid_ownership+0x43>
mov    %rbx,%rdi
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1173 <set_pid_ownership+0x43>
mov    (%rax),%r14d
test   %r15,%r15
je     118f <set_pid_ownership+0x5f>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <chown@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
define void @set_pid_ownership(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.passwd*
%8 = alloca %struct.group*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %9
store i32 0, i32* %10
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %22
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call %struct.group* @getgrnam(i8* %14)
store %struct.group* %15, %struct.group** %8
%16 = icmp ne %struct.group* %15, null
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load %struct.group*, %struct.group** %8
%19 = getelementptr inbounds %struct.group, %struct.group* %18, i32 0, i32 0
%20 = load i32, i32* %19
store i32 %20, i32* %10
br label %21
21: ; preds = %17, %13
br label %22
22: ; preds = %21, %3
%23 = load i8*, i8** %4
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = load i8*, i8** %4
%27 = call %struct.passwd* @getpwnam(i8* %26)
store %struct.passwd* %27, %struct.passwd** %7
%28 = icmp ne %struct.passwd* %27, null
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load %struct.passwd*, %struct.passwd** %7
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %30, i32 0, i32 0
%32 = load i32, i32* %31
store i32 %32, i32* %9
br label %33
33: ; preds = %29, %25
br label %34
34: ; preds = %33, %22
%35 = load i8*, i8** %6
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i8*, i8** %6
%39 = load i32, i32* %9
%40 = load i32, i32* %10
%41 = call i32 @chown(i8* %38, i32 %39, i32 %40)
br label %42
42: ; preds = %37, %34
ret void
}

LLVM IR (-O2):
%struct.group = type { i32 }
%struct.passwd = type { i32 }
define void @set_pid_ownership(i8* %0, i8* %1, i8* %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %11, label %5
5: ; preds = %3
%6 = tail call %struct.group* @getgrnam(i8* %1)
%7 = icmp eq %struct.group* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = getelementptr inbounds %struct.group, %struct.group* %6, i64 0, i32 0
%10 = load i32, i32* %9
br label %11
11: ; preds = %5, %8, %3
%12 = phi i32 [ %10, %8 ], [ 0, %5 ], [ 0, %3 ]
%13 = icmp eq i8* %0, null
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = tail call %struct.passwd* @getpwnam(i8* %0)
%16 = icmp eq %struct.passwd* %15, null
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i64 0, i32 0
%19 = load i32, i32* %18
br label %20
20: ; preds = %14, %17, %11
%21 = phi i32 [ %19, %17 ], [ 0, %14 ], [ 0, %11 ]
%22 = icmp eq i8* %2, null
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @chown(i8* %2, i32 %21, i32 %12)
br label %25
25: ; preds = %23, %20
ret void
}