#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 {
  char *pw_dir;
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int chdir(char *);
int chroot(char *);
int /*<<< orphan*/ logit(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ logitm(int /*<<< orphan*/, char *, ...);
int setgroups(int, int /*<<< orphan*/ *);
int setresgid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setresuid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setsid();
int drop_privs(struct passwd *pw, int do_chroot) {
  logit(LOG_DEBUG, "drop_privs: dropping privs %s chroot",
        do_chroot ? "with" : "without");
  if (setsid() == -1) {
    logitm(LOG_ERR, "setsid");
    return (-1);
  }
  if (do_chroot) {
    if (chdir(pw->pw_dir) == -1) {
      logitm(LOG_ERR, "chdir(%s)", pw->pw_dir);
      return (-1);
    }
    if (chroot(pw->pw_dir) == -1) {
      logitm(LOG_ERR, "chroot(%s)", pw->pw_dir);
      return (-1);
    }
  }
  if (chdir("/") == -1) {
    logitm(LOG_ERR, "chdir(/)");
    return (-1);
  }
  if (setgroups(1, &pw->pw_gid) == -1) {
    logitm(LOG_ERR, "setgroups");
    return (-1);
  }
  if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1) {
    logitm(LOG_ERR, "setresgid");
    return (-1);
  }
  if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1) {
    logitm(LOG_ERR, "setresuid");
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<drop_privs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%ecx
lea    0xe88(%rip),%rdx
lea    0xe7c(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
lea    0xe49(%rip),%rsi
call   1080 <logit@plt>
mov    $0x0,%al
call   10a0 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11ef <drop_privs+0x6f>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1050 <logitm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <drop_privs+0x205>
cmpl   $0x0,-0x14(%rbp)
je     127c <drop_privs+0xfc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1238 <drop_privs+0xb8>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1050 <logitm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <drop_privs+0x205>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
jne    1277 <drop_privs+0xf7>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1050 <logitm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <drop_privs+0x205>
jmp    127c <drop_privs+0xfc>
lea    0xdcb(%rip),%rdi
call   1060 <chdir@plt>
cmp    $0xffffffff,%eax
jne    12b4 <drop_privs+0x134>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1050 <logitm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <drop_privs+0x205>
mov    -0x10(%rbp),%rsi
add    $0xc,%rsi
mov    $0x1,%edi
call   1090 <setgroups@plt>
cmp    $0xffffffff,%eax
jne    12f2 <drop_privs+0x172>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1050 <logitm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <drop_privs+0x205>
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%edx
call   1070 <setresgid@plt>
cmp    $0xffffffff,%eax
jne    1338 <drop_privs+0x1b8>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
mov    $0x0,%al
call   1050 <logitm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <drop_privs+0x205>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1040 <setresuid@plt>
cmp    $0xffffffff,%eax
jne    137e <drop_privs+0x1fe>
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
lea    0xd02(%rip),%rsi
mov    $0x0,%al
call   1050 <logitm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <drop_privs+0x205>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privs>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
test   %esi,%esi
lea    0xe90(%rip),%rax
lea    0xe84(%rip),%rdx
cmove  %rax,%rdx
lea    0xe54(%rip),%rsi
call   1080 <logit@plt>
xor    %eax,%eax
call   10a0 <setsid@plt>
cmp    $0xffffffff,%eax
je     1256 <drop_privs+0xd6>
test   %ebp,%ebp
je     11e7 <drop_privs+0x67>
mov    (%rbx),%rdi
call   1060 <chdir@plt>
cmp    $0xffffffff,%eax
je     1268 <drop_privs+0xe8>
mov    (%rbx),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
je     128f <drop_privs+0x10f>
lea    0xe60(%rip),%rdi
call   1060 <chdir@plt>
cmp    $0xffffffff,%eax
je     127d <drop_privs+0xfd>
lea    0xc(%rbx),%rbp
mov    $0x1,%edi
mov    %rbp,%rsi
call   1090 <setgroups@plt>
cmp    $0xffffffff,%eax
je     12ab <drop_privs+0x12b>
mov    0x0(%rbp),%edi
mov    %edi,%esi
mov    %edi,%edx
call   1070 <setresgid@plt>
cmp    $0xffffffff,%eax
je     12bd <drop_privs+0x13d>
mov    0x8(%rbx),%edi
mov    %edi,%esi
mov    %edi,%edx
call   1040 <setresuid@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    12d9 <drop_privs+0x159>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
jmp    12cd <drop_privs+0x14d>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
jmp    12cd <drop_privs+0x14d>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdbe(%rip),%rsi
jmp    12a2 <drop_privs+0x122>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
jmp    12cd <drop_privs+0x14d>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xda1(%rip),%rsi
xor    %eax,%eax
call   1050 <logitm@plt>
jmp    12d4 <drop_privs+0x154>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
jmp    12cd <drop_privs+0x14d>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
xor    %eax,%eax
call   1050 <logitm@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8*, i32, i32 }
@LOG_DEBUG = global i32 0
@.str = [37 x i8] c"drop_privs: dropping privs %s chroot\00"
@.str.1 = [5 x i8] c"with\00"
@.str.2 = [8 x i8] c"without\00"
@LOG_ERR = global i32 0
@.str.3 = [7 x i8] c"setsid\00"
@.str.4 = [10 x i8] c"chdir(%s)\00"
@.str.5 = [11 x i8] c"chroot(%s)\00"
@.str.6 = [2 x i8] c"/\00"
@.str.7 = [9 x i8] c"chdir(/)\00"
@.str.8 = [10 x i8] c"setgroups\00"
@.str.9 = [10 x i8] c"setresgid\00"
@.str.10 = [10 x i8] c"setresuid\00"
define i32 @drop_privs(%struct.passwd* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.passwd*
%5 = alloca i32
store %struct.passwd* %0, %struct.passwd** %4
store i32 %1, i32* %5
%6 = load i32, i32* @LOG_DEBUG
%7 = load i32, i32* %5
%8 = icmp ne i32 %7, 0
%9 = zext i1 %8 to i64
%10 = select i1 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0)
%11 = call i32 @logit(i32 %6, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 (...) @setsid()
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @LOG_ERR
%16 = call i32 (i32, i8*, ...) @logitm(i32 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
17: ; preds = %2
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %45
20: ; preds = %17
%21 = load %struct.passwd*, %struct.passwd** %4
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %21, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 @chdir(i8* %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = load i32, i32* @LOG_ERR
%28 = load %struct.passwd*, %struct.passwd** %4
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i32 (i32, i8*, ...) @logitm(i32 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %30)
store i32 -1, i32* %3
br label %90
32: ; preds = %20
%33 = load %struct.passwd*, %struct.passwd** %4
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @chroot(i8* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %32
%39 = load i32, i32* @LOG_ERR
%40 = load %struct.passwd*, %struct.passwd** %4
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i32 (i32, i8*, ...) @logitm(i32 %39, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %42)
store i32 -1, i32* %3
br label %90
44: ; preds = %32
br label %45
45: ; preds = %44, %17
%46 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* @LOG_ERR
%50 = call i32 (i32, i8*, ...) @logitm(i32 %49, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
51: ; preds = %45
%52 = load %struct.passwd*, %struct.passwd** %4
%53 = getelementptr inbounds %struct.passwd, %struct.passwd* %52, i32 0, i32 2
%54 = call i32 @setgroups(i32 1, i32* %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = load i32, i32* @LOG_ERR
%58 = call i32 (i32, i8*, ...) @logitm(i32 %57, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
59: ; preds = %51
%60 = load %struct.passwd*, %struct.passwd** %4
%61 = getelementptr inbounds %struct.passwd, %struct.passwd* %60, i32 0, i32 2
%62 = load i32, i32* %61
%63 = load %struct.passwd*, %struct.passwd** %4
%64 = getelementptr inbounds %struct.passwd, %struct.passwd* %63, i32 0, i32 2
%65 = load i32, i32* %64
%66 = load %struct.passwd*, %struct.passwd** %4
%67 = getelementptr inbounds %struct.passwd, %struct.passwd* %66, i32 0, i32 2
%68 = load i32, i32* %67
%69 = call i32 @setresgid(i32 %62, i32 %65, i32 %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %74
71: ; preds = %59
%72 = load i32, i32* @LOG_ERR
%73 = call i32 (i32, i8*, ...) @logitm(i32 %72, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
74: ; preds = %59
%75 = load %struct.passwd*, %struct.passwd** %4
%76 = getelementptr inbounds %struct.passwd, %struct.passwd* %75, i32 0, i32 1
%77 = load i32, i32* %76
%78 = load %struct.passwd*, %struct.passwd** %4
%79 = getelementptr inbounds %struct.passwd, %struct.passwd* %78, i32 0, i32 1
%80 = load i32, i32* %79
%81 = load %struct.passwd*, %struct.passwd** %4
%82 = getelementptr inbounds %struct.passwd, %struct.passwd* %81, i32 0, i32 1
%83 = load i32, i32* %82
%84 = call i32 @setresuid(i32 %77, i32 %80, i32 %83)
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %89
86: ; preds = %74
%87 = load i32, i32* @LOG_ERR
%88 = call i32 (i32, i8*, ...) @logitm(i32 %87, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0))
store i32 -1, i32* %3
br label %90
89: ; preds = %74
store i32 0, i32* %3
br label %90
90: ; preds = %89, %86, %71, %56, %48, %38, %26, %14
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i32, i32 }
@LOG_DEBUG = global i32 0
@.str = [37 x i8] c"drop_privs: dropping privs %s chroot\00"
@.str.1 = [5 x i8] c"with\00"
@.str.2 = [8 x i8] c"without\00"
@LOG_ERR = global i32 0
@.str.3 = [7 x i8] c"setsid\00"
@.str.4 = [10 x i8] c"chdir(%s)\00"
@.str.5 = [11 x i8] c"chroot(%s)\00"
@.str.6 = [2 x i8] c"/\00"
@.str.7 = [9 x i8] c"chdir(/)\00"
@.str.8 = [10 x i8] c"setgroups\00"
@.str.9 = [10 x i8] c"setresgid\00"
@.str.10 = [10 x i8] c"setresuid\00"
define i32 @drop_privs(%struct.passwd* %0, i32 %1) {
%3 = load i32, i32* @LOG_DEBUG
%4 = icmp eq i32 %1, 0
%5 = select i1 %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0)
%6 = tail call i32 @logit(i32 %3, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 (...) @setsid()
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @LOG_ERR
%11 = tail call i32 (i32, i8*, ...) @logitm(i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %58
12: ; preds = %2
br i1 %4, label %30, label %13
13: ; preds = %12
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = tail call i32 @chdir(i8* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i32, i32* @LOG_ERR
%20 = load i8*, i8** %14
%21 = tail call i32 (i32, i8*, ...) @logitm(i32 %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %20)
br label %58
22: ; preds = %13
%23 = load i8*, i8** %14
%24 = tail call i32 @chroot(i8* %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @LOG_ERR
%28 = load i8*, i8** %14
%29 = tail call i32 (i32, i8*, ...) @logitm(i32 %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %28)
br label %58
30: ; preds = %22, %12
%31 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* @LOG_ERR
%35 = tail call i32 (i32, i8*, ...) @logitm(i32 %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %58
36: ; preds = %30
%37 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 2
%38 = tail call i32 @setgroups(i32 1, i32* %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* @LOG_ERR
%42 = tail call i32 (i32, i8*, ...) @logitm(i32 %41, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %58
43: ; preds = %36
%44 = load i32, i32* %37
%45 = tail call i32 @setresgid(i32 %44, i32 %44, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @LOG_ERR
%49 = tail call i32 (i32, i8*, ...) @logitm(i32 %48, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0))
br label %58
50: ; preds = %43
%51 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 1
%52 = load i32, i32* %51
%53 = tail call i32 @setresuid(i32 %52, i32 %52, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = load i32, i32* @LOG_ERR
%57 = tail call i32 (i32, i8*, ...) @logitm(i32 %56, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0))
br label %58
58: ; preds = %50, %55, %47, %40, %33, %26, %18, %9
%59 = phi i32 [ -1, %9 ], [ -1, %18 ], [ -1, %26 ], [ -1, %33 ], [ -1, %40 ], [ -1, %47 ], [ -1, %55 ], [ 0, %50 ]
ret i32 %59
}