#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 */
typedef int uid_t;
typedef int gid_t;

/* Variables and functions */
int errno;
int get_gid_by_name(char const *);
int get_uid_by_name(char const *);
int /*<<< orphan*/ nxweb_log_error(char *, int, int);
int setresgid(int, int, int);
int setresuid(int, int, int);
int nxweb_drop_privileges(const char *group_name, const char *user_name) {
  if (user_name && group_name) {
    uid_t uid = get_uid_by_name(user_name);
    gid_t gid = get_gid_by_name(group_name);
    if (uid == -1 || gid == -1) {
      nxweb_log_error("uid=%d gid=%d can't set uid&gid", uid, gid);
      return -1;
    } else {
      if (setresgid(gid, gid, gid) == -1) {
        nxweb_log_error("can't set gid=%d errno=%d", gid, errno);
        return -1;
      }
      if (setresuid(uid, uid, uid) == -1) {
        nxweb_log_error("can't set uid=%d errno=%d", uid, errno);
        return -1;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<nxweb_drop_privileges>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1240 <nxweb_drop_privileges+0xf0>
cmpq   $0x0,-0x10(%rbp)
je     1240 <nxweb_drop_privileges+0xf0>
mov    -0x18(%rbp),%rdi
call   1050 <get_uid_by_name@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <get_gid_by_name@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     11a2 <nxweb_drop_privileges+0x52>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11c0 <nxweb_drop_privileges+0x70>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe51(%rip),%rdi
call   1070 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <nxweb_drop_privileges+0xf7>
mov    -0x20(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1040 <setresgid@plt>
cmp    $0xffffffff,%eax
jne    11fb <nxweb_drop_privileges+0xab>
mov    -0x20(%rbp),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
lea    0xe36(%rip),%rdi
call   1070 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <nxweb_drop_privileges+0xf7>
mov    -0x1c(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1030 <setresuid@plt>
cmp    $0xffffffff,%eax
jne    1236 <nxweb_drop_privileges+0xe6>
mov    -0x1c(%rbp),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
lea    0xe15(%rip),%rdi
call   1070 <nxweb_log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <nxweb_drop_privileges+0xf7>
jmp    123b <nxweb_drop_privileges+0xeb>
jmp    1240 <nxweb_drop_privileges+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nxweb_drop_privileges>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
test   %rdi,%rdi
je     11ed <nxweb_drop_privileges+0x9d>
test   %rsi,%rsi
je     11ed <nxweb_drop_privileges+0x9d>
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1050 <get_uid_by_name@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1060 <get_gid_by_name@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%r14d
je     11c3 <nxweb_drop_privileges+0x73>
cmp    $0xffffffff,%ebp
je     11c3 <nxweb_drop_privileges+0x73>
mov    %ebp,%edi
mov    %ebp,%esi
mov    %ebp,%edx
call   1040 <setresgid@plt>
cmp    $0xffffffff,%eax
je     11d1 <nxweb_drop_privileges+0x81>
mov    %r14d,%edi
mov    %r14d,%esi
mov    %r14d,%edx
call   1030 <setresuid@plt>
cmp    $0xffffffff,%eax
jne    11ed <nxweb_drop_privileges+0x9d>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
lea    0xe7c(%rip),%rdi
mov    %r14d,%esi
jmp    11e3 <nxweb_drop_privileges+0x93>
lea    0xe36(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
jmp    11e3 <nxweb_drop_privileges+0x93>
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
lea    0xe3f(%rip),%rdi
mov    %ebp,%esi
call   1070 <nxweb_log_error@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"uid=%d gid=%d can't set uid&gid\00"
@.str.1 = [26 x i8] c"can't set gid=%d errno=%d\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"can't set uid=%d errno=%d\00"
define i32 @nxweb_drop_privileges(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %49
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %49
13: ; preds = %10
%14 = load i8*, i8** %5
%15 = call i32 @get_uid_by_name(i8* %14)
store i32 %15, i32* %6
%16 = load i8*, i8** %4
%17 = call i32 @get_gid_by_name(i8* %16)
store i32 %17, i32* %7
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %23, label %20
20: ; preds = %13
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %20, %13
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = call i32 @nxweb_log_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
store i32 -1, i32* %3
br label %50
27: ; preds = %20
%28 = load i32, i32* %7
%29 = load i32, i32* %7
%30 = load i32, i32* %7
%31 = call i32 @setresgid(i32 %28, i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i32, i32* %7
%35 = load i32, i32* @errno
%36 = call i32 @nxweb_log_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 %35)
store i32 -1, i32* %3
br label %50
37: ; preds = %27
%38 = load i32, i32* %6
%39 = load i32, i32* %6
%40 = load i32, i32* %6
%41 = call i32 @setresuid(i32 %38, i32 %39, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = load i32, i32* %6
%45 = load i32, i32* @errno
%46 = call i32 @nxweb_log_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %45)
store i32 -1, i32* %3
br label %50
47: ; preds = %37
br label %48
48: ; preds = %47
br label %49
49: ; preds = %48, %10, %2
store i32 0, i32* %3
br label %50
50: ; preds = %49, %43, %33, %23
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [32 x i8] c"uid=%d gid=%d can't set uid&gid\00"
@.str.1 = [26 x i8] c"can't set gid=%d errno=%d\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"can't set uid=%d errno=%d\00"
define i32 @nxweb_drop_privileges(i8* %0, i8* %1) {
%3 = icmp ne i8* %1, null
%4 = icmp ne i8* %0, null
%5 = and i1 %4, %3
br i1 %5, label %6, label %26
6: ; preds = %2
%7 = tail call i32 @get_uid_by_name(i8* %1)
%8 = tail call i32 @get_gid_by_name(i8* %0)
%9 = icmp eq i32 %7, -1
%10 = icmp eq i32 %8, -1
%11 = select i1 %9, i1 true, i1 %10
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @nxweb_log_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
br label %26
14: ; preds = %6
%15 = tail call i32 @setresgid(i32 %8, i32 %8, i32 %8)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = tail call i32 @nxweb_log_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %18)
br label %26
20: ; preds = %14
%21 = tail call i32 @setresuid(i32 %7, i32 %7, i32 %7)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = tail call i32 @nxweb_log_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %7, i32 %24)
br label %26
26: ; preds = %23, %17, %12, %2, %20
%27 = phi i32 [ 0, %20 ], [ 0, %2 ], [ -1, %12 ], [ -1, %17 ], [ -1, %23 ]
ret i32 %27
}