#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;
  int /*<<< orphan*/ pw_name;
};

/* Variables and functions */
int /*<<< orphan*/ endpwent();
int /*<<< orphan*/ geteuid();
struct passwd *getpwent();
scalar_t__ initgroups(char *, int /*<<< orphan*/);
int /*<<< orphan*/ log_err(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
scalar_t__ strncmp(int /*<<< orphan*/, char *, int);
void set_nobody(void) {
  struct passwd *pw;
  if (!geteuid()) {
    pw = getpwent();
    while ((pw != NULL) && strncmp(pw->pw_name, "nobody", sizeof("nobody")))
      pw = getpwent();
    endpwent();
    if (pw) {
      if (setgid(pw->pw_gid))
        log_err("WARNING - Unable to set gid to nobody.");
      if (initgroups("nobody", pw->pw_gid))
        log_err("WARNING - Unable to set additional groups to nobody.");
      if (setuid(pw->pw_uid))
        log_err("WARNING - Unable to set uid to nobody.");
    } else {
      log_err("WARNING - Unable to set effective uid to nobody.");
    }
  }
}


x86 assembly (clang, -O0):
<set_nobody>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <geteuid@plt>
cmp    $0x0,%eax
jne    1289 <set_nobody+0x109>
mov    $0x0,%al
call   1050 <getpwent@plt>
mov    %rax,-0x8(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x9(%rbp)
je     11d5 <set_nobody+0x55>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%edi
lea    0xe3f(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
test   $0x1,%al
jne    11e5 <set_nobody+0x65>
jmp    11f5 <set_nobody+0x75>
mov    $0x0,%al
call   1050 <getpwent@plt>
mov    %rax,-0x8(%rbp)
jmp    11a3 <set_nobody+0x23>
mov    $0x0,%al
call   1060 <endpwent@plt>
cmpq   $0x0,-0x8(%rbp)
je     1278 <set_nobody+0xf8>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
je     1229 <set_nobody+0xa9>
lea    0xde3(%rip),%rdi
call   10a0 <log_err@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%esi
lea    0xdc9(%rip),%rdi
call   1090 <initgroups@plt>
cmp    $0x0,%rax
je     1252 <set_nobody+0xd2>
lea    0xde1(%rip),%rdi
call   10a0 <log_err@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1080 <setuid@plt>
cmp    $0x0,%rax
je     1273 <set_nobody+0xf3>
lea    0xdf5(%rip),%rdi
call   10a0 <log_err@plt>
jmp    1284 <set_nobody+0x104>
lea    0xe0b(%rip),%rdi
call   10a0 <log_err@plt>
jmp    1289 <set_nobody+0x109>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_nobody>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <geteuid@plt>
test   %eax,%eax
je     1197 <set_nobody+0x17>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1050 <getpwent@plt>
test   %rax,%rax
je     11d4 <set_nobody+0x54>
mov    %rax,%rbx
lea    0xe53(%rip),%r14
nopl   (%rax)
mov    0x8(%rbx),%edi
mov    %r14,%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11e4 <set_nobody+0x64>
xor    %eax,%eax
call   1050 <getpwent@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b0 <set_nobody+0x30>
xor    %eax,%eax
call   1060 <endpwent@plt>
lea    0xea8(%rip),%rdi
jmp    123b <set_nobody+0xbb>
xor    %eax,%eax
call   1060 <endpwent@plt>
mov    0x4(%rbx),%edi
call   1070 <setgid@plt>
test   %rax,%rax
je     1204 <set_nobody+0x84>
lea    0xe08(%rip),%rdi
call   10a0 <log_err@plt>
mov    0x4(%rbx),%esi
lea    0xdf2(%rip),%rdi
call   1090 <initgroups@plt>
test   %rax,%rax
je     1224 <set_nobody+0xa4>
lea    0xe0f(%rip),%rdi
call   10a0 <log_err@plt>
mov    (%rbx),%edi
call   1080 <setuid@plt>
test   %rax,%rax
je     118f <set_nobody+0xf>
lea    0xe28(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    10a0 <log_err@plt>

LLVM IR (-O0):
%struct.passwd = type { i32, i32, i32 }
@.str = [7 x i8] c"nobody\00"
@.str.1 = [39 x i8] c"WARNING - Unable to set gid to nobody.\00"
@.str.2 = [53 x i8] c"WARNING - Unable to set additional groups to nobody.\00"
@.str.3 = [39 x i8] c"WARNING - Unable to set uid to nobody.\00"
@.str.4 = [49 x i8] c"WARNING - Unable to set effective uid to nobody.\00"
define void @set_nobody() {
%1 = alloca %struct.passwd*
%2 = call i32 (...) @geteuid()
%3 = icmp ne i32 %2, 0
br i1 %3, label %51, label %4
4: ; preds = %0
%5 = call %struct.passwd* (...) @getpwent()
store %struct.passwd* %5, %struct.passwd** %1
br label %6
6: ; preds = %17, %4
%7 = load %struct.passwd*, %struct.passwd** %1
%8 = icmp ne %struct.passwd* %7, null
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = load %struct.passwd*, %struct.passwd** %1
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i32 0, i32 2
%12 = load i32, i32* %11
%13 = call i64 @strncmp(i32 %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 7)
%14 = icmp ne i64 %13, 0
br label %15
15: ; preds = %9, %6
%16 = phi i1 [ false, %6 ], [ %14, %9 ]
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = call %struct.passwd* (...) @getpwent()
store %struct.passwd* %18, %struct.passwd** %1
br label %6
19: ; preds = %15
%20 = call i32 (...) @endpwent()
%21 = load %struct.passwd*, %struct.passwd** %1
%22 = icmp ne %struct.passwd* %21, null
br i1 %22, label %23, label %48
23: ; preds = %19
%24 = load %struct.passwd*, %struct.passwd** %1
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = call i64 @setgid(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @log_err(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
%32 = load %struct.passwd*, %struct.passwd** %1
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = call i64 @initgroups(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @log_err(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %31
%40 = load %struct.passwd*, %struct.passwd** %1
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i64 @setuid(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @log_err(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %39
br label %50
48: ; preds = %19
%49 = call i32 @log_err(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %47
br label %51
51: ; preds = %50, %0
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32, i32 }
@.str = [7 x i8] c"nobody\00"
@.str.1 = [39 x i8] c"WARNING - Unable to set gid to nobody.\00"
@.str.2 = [53 x i8] c"WARNING - Unable to set additional groups to nobody.\00"
@.str.3 = [39 x i8] c"WARNING - Unable to set uid to nobody.\00"
@.str.4 = [49 x i8] c"WARNING - Unable to set effective uid to nobody.\00"
define void @set_nobody() {
%1 = tail call i32 (...) @geteuid()
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %39
3: ; preds = %0
%4 = tail call %struct.passwd* (...) @getpwent()
%5 = icmp eq %struct.passwd* %4, null
br i1 %5, label %34, label %6
6: ; preds = %3, %12
%7 = phi %struct.passwd* [ %13, %12 ], [ %4, %3 ]
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 2
%9 = load i32, i32* %8
%10 = tail call i64 @strncmp(i32 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %6
%13 = tail call %struct.passwd* (...) @getpwent()
%14 = icmp eq %struct.passwd* %13, null
br i1 %14, label %34, label %6
15: ; preds = %6
%16 = tail call i32 (...) @endpwent()
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 1
%18 = load i32, i32* %17
%19 = tail call i64 @setgid(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %15
%22 = tail call i32 @log_err(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = load i32, i32* %17
%25 = tail call i64 @initgroups(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = tail call i32 @log_err(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%31 = load i32, i32* %30
%32 = tail call i64 @setuid(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %39, label %36
34: ; preds = %12, %3
%35 = tail call i32 (...) @endpwent()
br label %36
36: ; preds = %29, %34
%37 = phi i8* [ getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), %34 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), %29 ]
%38 = tail call i32 @log_err(i8* %37)
br label %39
39: ; preds = %36, %29, %0
ret void
}