#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;
  int /*<<< orphan*/ pw_gid;
};

/* Variables and functions */
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, char const *);
struct passwd *getpwnam(char const *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
void change_user(const char *name) {
  struct passwd *user = getpwnam(name);
  if (!user)
    errx(1, "User %s not found", name);
  if (setgid(user->pw_gid) < 0)
    err(1, "Changing group");
  if (setuid(user->pw_uid) < 0)
    err(1, "Changing user");
}


x86 assembly (clang, -O0):
<change_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1189 <change_user+0x39>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe7c(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <setgid@plt>
cmp    $0x0,%rax
jge    11b0 <change_user+0x60>
mov    $0x1,%edi
lea    0xe67(%rip),%rsi
call   1050 <err@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <setuid@plt>
cmp    $0x0,%rax
jge    11d6 <change_user+0x86>
mov    $0x1,%edi
lea    0xe50(%rip),%rsi
call   1050 <err@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_user>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <getpwnam@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1178 <change_user+0x28>
lea    0xe95(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
call   1030 <errx@plt>
mov    0x4(%rbx),%edi
call   1060 <setgid@plt>
test   %rax,%rax
jns    1196 <change_user+0x46>
lea    0xe86(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    (%rbx),%edi
call   1070 <setuid@plt>
test   %rax,%rax
js     11aa <change_user+0x5a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe70(%rip),%rsi
mov    $0x1,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <err@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [18 x i8] c"User %s not found\00"
@.str.1 = [15 x i8] c"Changing group\00"
@.str.2 = [14 x i8] c"Changing user\00"
define void @change_user(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.passwd*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call %struct.passwd* @getpwnam(i8* %4)
store %struct.passwd* %5, %struct.passwd** %3
%6 = load %struct.passwd*, %struct.passwd** %3
%7 = icmp ne %struct.passwd* %6, null
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %11
11: ; preds = %8, %1
%12 = load %struct.passwd*, %struct.passwd** %3
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = call i64 @setgid(i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = load %struct.passwd*, %struct.passwd** %3
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i64 @setuid(i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %19
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [18 x i8] c"User %s not found\00"
@.str.1 = [15 x i8] c"Changing group\00"
@.str.2 = [14 x i8] c"Changing user\00"
define void @change_user(i8* %0) {
%2 = tail call %struct.passwd* @getpwnam(i8* %0)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %6
6: ; preds = %4, %1
%7 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = tail call i64 @setgid(i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %6
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i64 @setuid(i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
ret void
}