#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 */
struct passwd *getpwnam(char const *);
scalar_t__ setegid(int /*<<< orphan*/);
scalar_t__ seteuid(int /*<<< orphan*/);
int runas(const char *user) {
  struct passwd *pw_ent = NULL;
  if (user != NULL) {
    pw_ent = getpwnam(user);
  }
  if (pw_ent != NULL) {
    if (setegid(pw_ent->pw_gid) != 0) {
      return -1;
    }
    if (seteuid(pw_ent->pw_uid) != 0) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<runas>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     115c <runas+0x2c>
mov    -0x10(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11af <runas+0x7f>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <setegid@plt>
cmp    $0x0,%rax
je     1189 <runas+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <runas+0x86>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <seteuid@plt>
cmp    $0x0,%rax
je     11aa <runas+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <runas+0x86>
jmp    11af <runas+0x7f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runas>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1165 <runas+0x35>
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1165 <runas+0x35>
mov    %rax,%r14
mov    0x4(%rax),%edi
call   1040 <setegid@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1167 <runas+0x37>
mov    (%r14),%edi
call   1050 <seteuid@plt>
test   %rax,%rax
jne    1167 <runas+0x37>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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