#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getgid();
struct passwd *getpwnam(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void drop_root() {
  if (getgid() != 0) {
    return;
  }
  struct passwd *nobody_user = getpwnam("nobody");
  if (nobody_user == NULL) {
    fprintf(stderr, "nobody group not found, continuing as root");
    return;
  }
  if (setgid(nobody_user->pw_gid) != 0) {
    fprintf(stderr, "failed setting nobody group, exiting");
    exit(1);
  }
  if (setuid(nobody_user->pw_uid) != 0) {
    fprintf(stderr, "failed setting nobody group, exiting");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<drop_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1050 <getgid@plt>
cmp    $0x0,%rax
je     117e <drop_root+0x1e>
jmp    121c <drop_root+0xbc>
lea    0xe7b(%rip),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b3 <drop_root+0x53>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1040 <fprintf@plt>
jmp    121c <drop_root+0xbc>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <setgid@plt>
cmp    $0x0,%rax
je     11e8 <drop_root+0x88>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1080 <setuid@plt>
cmp    $0x0,%rax
je     121c <drop_root+0xbc>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_root>:
push   %rbx
xor    %eax,%eax
call   1050 <getgid@plt>
test   %rax,%rax
jne    119a <drop_root+0x3a>
lea    0xe8c(%rip),%rdi
call   1030 <getpwnam@plt>
test   %rax,%rax
je     119c <drop_root+0x3c>
mov    %rax,%rbx
mov    0x4(%rax),%edi
call   1060 <setgid@plt>
test   %rax,%rax
jne    11b2 <drop_root+0x52>
mov    (%rbx),%edi
call   1080 <setuid@plt>
test   %rax,%rax
jne    11b2 <drop_root+0x52>
pop    %rbx
ret
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
pop    %rbx
jmp    1040 <fprintf@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [7 x i8] c"nobody\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"nobody group not found, continuing as root\00"
@.str.2 = [37 x i8] c"failed setting nobody group, exiting\00"
define void @drop_root() {
%1 = alloca %struct.passwd*
%2 = call i64 (...) @getgid()
%3 = icmp ne i64 %2, 0
br i1 %3, label %4, label %5
4: ; preds = %0
br label %32
5: ; preds = %0
%6 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store %struct.passwd* %6, %struct.passwd** %1
%7 = load %struct.passwd*, %struct.passwd** %1
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %32
12: ; preds = %5
%13 = load %struct.passwd*, %struct.passwd** %1
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i64 @setgid(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = load %struct.passwd*, %struct.passwd** %1
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @setuid(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %4, %9, %22
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [7 x i8] c"nobody\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"nobody group not found, continuing as root\00"
@.str.2 = [37 x i8] c"failed setting nobody group, exiting\00"
define void @drop_root() {
%1 = tail call i64 (...) @getgid()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %27
3: ; preds = %0
%4 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq %struct.passwd* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %27
9: ; preds = %3
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 1
%11 = load i32, i32* %10
%12 = tail call i64 @setgid(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %9
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i64 @setuid(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %6, %18, %0
ret void
}