#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;
};

/* Variables and functions */
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
void set_ownership(char *user, char *group) {
  struct passwd *pwd;
  struct group *grp;
  if (group) {
    if ((grp = getgrnam(group)))
      setgid(grp->gr_gid);
  }
  if (user) {
    if ((pwd = getpwnam(user)))
      setuid(pwd->pw_uid);
  }
}


x86 assembly (clang, -O0):
<set_ownership>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1182 <set_ownership+0x42>
mov    -0x10(%rbp),%rdi
call   1040 <getgrnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     117d <set_ownership+0x3d>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <setgid@plt>
jmp    1182 <set_ownership+0x42>
cmpq   $0x0,-0x8(%rbp)
je     11b4 <set_ownership+0x74>
mov    -0x8(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11af <set_ownership+0x6f>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <setuid@plt>
jmp    11b4 <set_ownership+0x74>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_ownership>:
push   %rbx
mov    %rdi,%rbx
test   %rsi,%rsi
je     115d <set_ownership+0x1d>
mov    %rsi,%rdi
call   1040 <getgrnam@plt>
test   %rax,%rax
je     115d <set_ownership+0x1d>
mov    (%rax),%edi
call   1050 <setgid@plt>
test   %rbx,%rbx
je     1177 <set_ownership+0x37>
mov    %rbx,%rdi
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1177 <set_ownership+0x37>
mov    (%rax),%edi
pop    %rbx
jmp    1060 <setuid@plt>
pop    %rbx
ret

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

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