#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_gid;
  int /*<<< orphan*/ pw_uid;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setgroups(int, int /*<<< orphan*/ *);
scalar_t__ setuid(int /*<<< orphan*/);
void set_user(struct passwd *pw, gid_t *groups, int n_groups) {
  if (setgroups(n_groups, groups))
    err(EXIT_FAILURE, "setgroups");
  if (setuid(pw->pw_uid))
    err(EXIT_FAILURE, "setuid to user");
  if (setgid(pw->pw_gid))
    err(EXIT_FAILURE, "setgid");
}


x86 assembly (clang, -O0):
<set_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <setgroups@plt>
cmp    $0x0,%rax
je     117e <set_user+0x3e>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
call   1040 <err@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <setuid@plt>
cmp    $0x0,%rax
je     11a9 <set_user+0x69>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1040 <err@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1050 <setgid@plt>
cmp    $0x0,%rax
je     11d3 <set_user+0x93>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1040 <err@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_user>:
push   %rbx
mov    %rdi,%rbx
mov    %edx,%edi
call   1030 <setgroups@plt>
test   %rax,%rax
je     1165 <set_user+0x25>
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
lea    0xea0(%rip),%rsi
call   1040 <err@plt>
mov    0x4(%rbx),%edi
call   1060 <setuid@plt>
test   %rax,%rax
je     1187 <set_user+0x47>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1040 <err@plt>
mov    (%rbx),%edi
call   1050 <setgid@plt>
test   %rax,%rax
je     11a9 <set_user+0x69>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
pop    %rbx
jmp    1040 <err@plt>
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@EXIT_FAILURE = global i32 0
@.str = [10 x i8] c"setgroups\00"
@.str.1 = [15 x i8] c"setuid to user\00"
@.str.2 = [7 x i8] c"setgid\00"
define void @set_user(%struct.passwd* %0, i32* %1, i32 %2) {
%4 = alloca %struct.passwd*
%5 = alloca i32*
%6 = alloca i32
store %struct.passwd* %0, %struct.passwd** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%7 = load i32, i32* %6
%8 = load i32*, i32** %5
%9 = call i64 @setgroups(i32 %7, i32* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @err(i32 %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %3
%15 = load %struct.passwd*, %struct.passwd** %4
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i32 0, i32 1
%17 = load i32, i32* %16
%18 = call i64 @setuid(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @err(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %20, %14
%24 = load %struct.passwd*, %struct.passwd** %4
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @setgid(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @err(i32 %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %29, %23
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@EXIT_FAILURE = global i32 0
@.str = [10 x i8] c"setgroups\00"
@.str.1 = [15 x i8] c"setuid to user\00"
@.str.2 = [7 x i8] c"setgid\00"
define void @set_user(%struct.passwd* nocapture %0, i32* %1, i32 %2) {
%4 = tail call i64 @setgroups(i32 %2, i32* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @err(i32 %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %6, %3
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 1
%11 = load i32, i32* %10
%12 = tail call i64 @setuid(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @err(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %14, %9
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i64 @setgid(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @err(i32 %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %22, %17
ret void
}