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

/* Variables and functions */
int /*<<< orphan*/ UINT_MAX;
int /*<<< orphan*/ err(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ errx(int, char *, char const *, char const *);
char *getenv(char *);
int setgid(int /*<<< orphan*/);
int setuid(int /*<<< orphan*/);
int /*<<< orphan*/ strtonum(char const *, int, int /*<<< orphan*/,
                            char const **);
void droppriv(void) {
  const char *sudoid, *errstr;
  if ((sudoid = getenv("SUDO_GID")) != NULL) {
    gid_t gid;
    gid = strtonum(sudoid, 1, UINT_MAX, &errstr);
    if (errstr)
      errx(1, "SUDO_UID is %s: %s", errstr, sudoid);
    if (setgid(gid) == -1)
      err(1, "setgid %u", gid);
  }
  if ((sudoid = getenv("SUDO_UID")) != NULL) {
    uid_t uid;
    uid = strtonum(sudoid, 1, UINT_MAX, &errstr);
    if (errstr)
      errx(1, "SUDO_UID is %s: %s", errstr, sudoid);
    if (setuid(uid) == -1)
      err(1, "setuid %u", uid);
  }
}


x86 assembly (clang, -O0):
<droppriv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     11ee <droppriv+0x8e>
mov    -0x8(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
lea    -0x10(%rbp),%rcx
call   1060 <strtonum@plt>
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c4 <droppriv+0x64>
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
lea    0xe4a(%rip),%rsi
call   1040 <errx@plt>
mov    -0x14(%rbp),%edi
call   1070 <setgid@plt>
cmp    $0xffffffff,%eax
jne    11e9 <droppriv+0x89>
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
lea    0xe38(%rip),%rsi
call   1050 <err@plt>
jmp    11ee <droppriv+0x8e>
lea    0xe31(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     1274 <droppriv+0x114>
mov    -0x8(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
lea    -0x10(%rbp),%rcx
call   1060 <strtonum@plt>
mov    %eax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     124a <droppriv+0xea>
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
lea    0xdc4(%rip),%rsi
call   1040 <errx@plt>
mov    -0x18(%rbp),%edi
call   1080 <setuid@plt>
cmp    $0xffffffff,%eax
jne    126f <droppriv+0x10f>
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
lea    0xdc5(%rip),%rsi
call   1050 <err@plt>
jmp    1274 <droppriv+0x114>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<droppriv>:
push   %rbp
push   %rbx
push   %rax
lea    0xe96(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ce <droppriv+0x6e>
mov    %rax,%rbx
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1060 <strtonum@plt>
mov    %eax,%ebp
mov    (%rsp),%rdx
test   %rdx,%rdx
je     11af <droppriv+0x4f>
lea    0xe67(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rcx
call   1040 <errx@plt>
mov    %ebp,%edi
call   1070 <setgid@plt>
cmp    $0xffffffff,%eax
jne    11ce <droppriv+0x6e>
lea    0xe5a(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
call   1050 <err@plt>
lea    0xe51(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1239 <droppriv+0xd9>
mov    %rax,%rbp
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    $0x1,%esi
call   1060 <strtonum@plt>
mov    %eax,%ebx
mov    (%rsp),%rdx
test   %rdx,%rdx
je     121a <droppriv+0xba>
lea    0xdfc(%rip),%rsi
mov    $0x1,%edi
mov    %rbp,%rcx
call   1040 <errx@plt>
mov    %ebx,%edi
call   1080 <setuid@plt>
cmp    $0xffffffff,%eax
jne    1239 <droppriv+0xd9>
lea    0xe02(%rip),%rsi
mov    $0x1,%edi
mov    %ebx,%edx
call   1050 <err@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"SUDO_GID\00"
@UINT_MAX = global i32 0
@.str.1 = [19 x i8] c"SUDO_UID is %s: %s\00"
@.str.2 = [10 x i8] c"setgid %u\00"
@.str.3 = [9 x i8] c"SUDO_UID\00"
@.str.4 = [10 x i8] c"setuid %u\00"
define void @droppriv() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %1
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %25
7: ; preds = %0
%8 = load i8*, i8** %1
%9 = load i32, i32* @UINT_MAX
%10 = call i32 @strtonum(i8* %8, i32 1, i32 %9, i8** %2)
store i32 %10, i32* %3
%11 = load i8*, i8** %2
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = load i8*, i8** %2
%15 = load i8*, i8** %1
%16 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %15)
br label %17
17: ; preds = %13, %7
%18 = load i32, i32* %3
%19 = call i32 @setgid(i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %3
%23 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %21, %17
br label %25
25: ; preds = %24, %0
%26 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i8* %26, i8** %1
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %46
28: ; preds = %25
%29 = load i8*, i8** %1
%30 = load i32, i32* @UINT_MAX
%31 = call i32 @strtonum(i8* %29, i32 1, i32 %30, i8** %2)
store i32 %31, i32* %4
%32 = load i8*, i8** %2
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = load i8*, i8** %2
%36 = load i8*, i8** %1
%37 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %35, i8* %36)
br label %38
38: ; preds = %34, %28
%39 = load i32, i32* %4
%40 = call i32 @setuid(i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %4
%44 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %42, %38
br label %46
46: ; preds = %45, %25
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"SUDO_GID\00"
@UINT_MAX = global i32 0
@.str.1 = [19 x i8] c"SUDO_UID is %s: %s\00"
@.str.2 = [10 x i8] c"setgid %u\00"
@.str.3 = [9 x i8] c"SUDO_UID\00"
@.str.4 = [10 x i8] c"setuid %u\00"
define void @droppriv() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %17, label %5
5: ; preds = %0
%6 = load i32, i32* @UINT_MAX
%7 = call i32 @strtonum(i8* %3, i32 1, i32 %6, i8** %1)
%8 = load i8*, i8** %1
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %3)
br label %12
12: ; preds = %10, %5
%13 = call i32 @setgid(i32 %7)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %17
17: ; preds = %12, %15, %0
%18 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %32, label %20
20: ; preds = %17
%21 = load i32, i32* @UINT_MAX
%22 = call i32 @strtonum(i8* %18, i32 1, i32 %21, i8** %1)
%23 = load i8*, i8** %1
%24 = icmp eq i8* %23, null
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = call i32 @errx(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %18)
br label %27
27: ; preds = %25, %20
%28 = call i32 @setuid(i32 %22)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %22)
br label %32
32: ; preds = %27, %30, %17
ret void
}