#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__ setregid(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ setreuid(int /*<<< orphan*/, int /*<<< orphan*/);
int runas(const char *user) {
  struct passwd *pw_ent = NULL;
  if (user != NULL) {
    pw_ent = getpwnam(user);
  }
  if (pw_ent != NULL) {
    if (setregid(pw_ent->pw_gid, pw_ent->pw_gid) != 0) {
      return -1;
    }
    if (setreuid(pw_ent->pw_uid, 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     11bc <runas+0x8c>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
call   1050 <setregid@plt>
cmp    $0x0,%rax
je     1190 <runas+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <runas+0x93>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1040 <setreuid@plt>
cmp    $0x0,%rax
je     11b7 <runas+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <runas+0x93>
jmp    11bc <runas+0x8c>
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     1169 <runas+0x39>
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1169 <runas+0x39>
mov    %rax,%r14
mov    0x4(%rax),%edi
mov    %edi,%esi
call   1050 <setregid@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    116b <runas+0x3b>
mov    (%r14),%edi
mov    %edi,%esi
call   1040 <setreuid@plt>
test   %rax,%rax
jne    116b <runas+0x3b>
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 %34
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 = load %struct.passwd*, %struct.passwd** %4
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i64 @setregid(i32 %16, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %13
store i32 -1, i32* %2
br label %35
23: ; preds = %13
%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 = load %struct.passwd*, %struct.passwd** %4
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @setreuid(i32 %26, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %23
store i32 -1, i32* %2
br label %35
33: ; preds = %23
br label %34
34: ; preds = %33, %10
store i32 0, i32* %2
br label %35
35: ; preds = %34, %32, %22
%36 = load i32, i32* %2
ret i32 %36
}

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 @setregid(i32 %8, 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 @setreuid(i32 %13, 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
}