#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 pw_uid;
};
struct group {
  int gr_gid;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int chdir(char *);
int chroot(char *);
int /*<<< orphan*/ exit(int);
int field_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ get_field(int /*<<< orphan*/ *, char *, int);
struct group *getgrnam(char *);
int getppid();
struct passwd *getpwnam(char *);
char *malloc_and_zero(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int setgid(int);
scalar_t__ setreuid(int, int /*<<< orphan*/);
int setuid(int);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
void la_preinit(uintptr_t *cookie) {
  char path_to_cmdline[256], *chroot_dir, *user, *group;
  int i = 0, c, chroot_dir_size, user_size, group_size;
  FILE *f;
  memset(path_to_cmdline, 0, 256);
  if (snprintf(path_to_cmdline, 256, "/proc/%d/cmdline", getppid()) >= 256) {
    fprintf(stderr,
            "Not enough space to copy /proc/%d/cmdline to buffer.  Only 255 "
            "characters are available.\n",
            getppid());
    exit(1);
  }
  if ((f = fopen(path_to_cmdline, "r")) == NULL) {
    perror("fopen()");
    exit(1);
  }
  field_size(f);
  chroot_dir_size = field_size(f);
  user_size = field_size(f);
  group_size = field_size(f);
  if (chroot_dir_size == 0 || user_size == 0 || group_size == 0) {
    fprintf(stderr, "usage: dchroot <chroot_dir> <user> <group> "
                    "<program_to_execute> <args...>\n");
    exit(1);
  }
  chroot_dir = malloc_and_zero((chroot_dir_size + 1) * sizeof(char));
  user = malloc_and_zero((user_size + 1) * sizeof(char));
  group = malloc_and_zero((group_size + 1) * sizeof(char));
  rewind(f);
  field_size(f);
  get_field(f, chroot_dir, chroot_dir_size);
  get_field(f, user, user_size);
  get_field(f, group, group_size);
  struct passwd *pwd = getpwnam(user);
  if (pwd == NULL) {
    fprintf(stderr, "User %s not found in /etc/passwd\n", user);
    exit(1);
  }
  struct group *grp = getgrnam(group);
  if (grp == NULL) {
    fprintf(stderr, "Group %s not found in /etc/group\n", group);
    exit(1);
  }
  if (chdir(chroot_dir) == -1) {
    fprintf(stderr, "chroot(\"%s\"): ", chroot_dir);
    perror("");
    exit(1);
  }
  if (chroot(chroot_dir) == -1) {
    fprintf(stderr, "chroot(\"%s\"): ", chroot_dir);
    perror("");
    exit(1);
  }
  if (chdir("/") == -1) {
    fprintf(stderr, "chroot(\"%s\"): ", chroot_dir);
    perror("");
    exit(1);
  }
  if (setgid(grp->gr_gid) == -1) {
    fprintf(stderr, "setgid(%d): ", grp->gr_gid);
    perror("");
    exit(1);
  }
  if (setuid(pwd->pw_uid) == -1) {
    fprintf(stderr, "setuid(%d): ", pwd->pw_uid);
    perror("");
    exit(1);
  }
  if (setreuid(-1, 0) == 0) {
    fprintf(stderr, "ERROR: could not drop root privileges appropriately.\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<la_preinit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x12c(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    -0x110(%rbp),%rax
mov    %rax,-0x160(%rbp)
mov    $0x0,%al
call   1120 <getppid@plt>
mov    -0x160(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xd8a(%rip),%rdx
call   1070 <snprintf@plt>
cmp    $0x100,%eax
jl     12bc <la_preinit+0x9c>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x164(%rbp)
mov    $0x0,%al
call   1120 <getppid@plt>
mov    -0x164(%rbp),%edi
mov    %eax,%edx
lea    0xd66(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
lea    -0x110(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x148(%rbp)
cmp    $0x0,%rax
jne    12f6 <la_preinit+0xd6>
lea    0xd86(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x148(%rbp),%rdi
call   1040 <field_size@plt>
mov    -0x148(%rbp),%rdi
call   1040 <field_size@plt>
mov    %eax,-0x134(%rbp)
mov    -0x148(%rbp),%rdi
call   1040 <field_size@plt>
mov    %eax,-0x138(%rbp)
mov    -0x148(%rbp),%rdi
call   1040 <field_size@plt>
mov    %eax,-0x13c(%rbp)
cmpl   $0x0,-0x134(%rbp)
je     135f <la_preinit+0x13f>
cmpl   $0x0,-0x138(%rbp)
je     135f <la_preinit+0x13f>
cmpl   $0x0,-0x13c(%rbp)
jne    1380 <la_preinit+0x160>
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
lea    0xd06(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x134(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1110 <malloc_and_zero@plt>
mov    %rax,-0x118(%rbp)
mov    -0x138(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1110 <malloc_and_zero@plt>
mov    %rax,-0x120(%rbp)
mov    -0x13c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1110 <malloc_and_zero@plt>
mov    %rax,-0x128(%rbp)
mov    -0x148(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x148(%rbp),%rdi
call   1040 <field_size@plt>
mov    -0x148(%rbp),%rdi
mov    -0x118(%rbp),%rsi
mov    -0x134(%rbp),%edx
call   1090 <get_field@plt>
mov    -0x148(%rbp),%rdi
mov    -0x120(%rbp),%rsi
mov    -0x138(%rbp),%edx
call   1090 <get_field@plt>
mov    -0x148(%rbp),%rdi
mov    -0x128(%rbp),%rsi
mov    -0x13c(%rbp),%edx
call   1090 <get_field@plt>
mov    -0x120(%rbp),%rdi
call   10a0 <getpwnam@plt>
mov    %rax,-0x150(%rbp)
cmpq   $0x0,-0x150(%rbp)
jne    1483 <la_preinit+0x263>
mov    0x2b7e(%rip),%rax
mov    (%rax),%edi
mov    -0x120(%rbp),%rdx
lea    0xc4e(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x128(%rbp),%rdi
call   10c0 <getgrnam@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
jne    14cc <la_preinit+0x2ac>
mov    0x2b35(%rip),%rax
mov    (%rax),%edi
mov    -0x128(%rbp),%rdx
lea    0xc27(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x118(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    1515 <la_preinit+0x2f5>
mov    0x2af8(%rip),%rax
mov    (%rax),%edi
mov    -0x118(%rbp),%rdx
lea    0xc0c(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
lea    0xb64(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x118(%rbp),%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
jne    155e <la_preinit+0x33e>
mov    0x2aaf(%rip),%rax
mov    (%rax),%edi
mov    -0x118(%rbp),%rdx
lea    0xbc3(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
lea    0xb1b(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
lea    0xbae(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
jne    15a7 <la_preinit+0x387>
mov    0x2a66(%rip),%rax
mov    (%rax),%edi
mov    -0x118(%rbp),%rdx
lea    0xb7a(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
lea    0xad2(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x158(%rbp),%rax
mov    (%rax),%edi
call   10e0 <setgid@plt>
cmp    $0xffffffff,%eax
jne    15f4 <la_preinit+0x3d4>
mov    0x2a1b(%rip),%rax
mov    (%rax),%edi
mov    -0x158(%rbp),%rax
mov    (%rax),%edx
lea    0xb3e(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
lea    0xa85(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x150(%rbp),%rax
mov    (%rax),%edi
call   1140 <setuid@plt>
cmp    $0xffffffff,%eax
jne    1641 <la_preinit+0x421>
mov    0x29ce(%rip),%rax
mov    (%rax),%edi
mov    -0x150(%rbp),%rax
mov    (%rax),%edx
lea    0xafe(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
lea    0xa38(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    $0xffffffff,%edi
xor    %esi,%esi
call   10d0 <setreuid@plt>
cmp    $0x0,%rax
jne    1678 <la_preinit+0x458>
mov    0x2982(%rip),%rax
mov    (%rax),%edi
lea    0xac8(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<la_preinit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
xor    %eax,%eax
call   1120 <getppid@plt>
lea    0xdad(%rip),%rdx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1070 <snprintf@plt>
cmp    $0x100,%eax
jge    13fb <la_preinit+0x1db>
lea    0xdf7(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10f0 <fopen@plt>
test   %rax,%rax
je     1427 <la_preinit+0x207>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <field_size@plt>
mov    %rbx,%rdi
call   1040 <field_size@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <field_size@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1040 <field_size@plt>
test   %r14d,%r14d
je     1433 <la_preinit+0x213>
test   %r12d,%r12d
je     1433 <la_preinit+0x213>
mov    %eax,%r15d
test   %eax,%eax
je     1433 <la_preinit+0x213>
mov    %r14d,%edi
add    $0x1,%edi
call   1110 <malloc_and_zero@plt>
mov    %rax,%r13
mov    %r12d,%edi
add    $0x1,%edi
call   1110 <malloc_and_zero@plt>
mov    %rax,%rbp
mov    %r15d,%edi
add    $0x1,%edi
call   1110 <malloc_and_zero@plt>
mov    %rax,0x8(%rsp)
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %rbx,%rdi
call   1040 <field_size@plt>
mov    %rbx,%rdi
mov    %r13,(%rsp)
mov    %r13,%rsi
mov    %r14d,%edx
call   1090 <get_field@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1090 <get_field@plt>
mov    %rbx,%rdi
mov    0x8(%rsp),%rbx
mov    %rbx,%rsi
mov    %r15d,%edx
call   1090 <get_field@plt>
mov    %rbp,%rdi
call   10a0 <getpwnam@plt>
test   %rax,%rax
je     1448 <la_preinit+0x228>
mov    %rax,%r15
mov    %rbx,%rdi
call   10c0 <getgrnam@plt>
test   %rax,%rax
je     145d <la_preinit+0x23d>
mov    %rax,%rbp
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
je     13dc <la_preinit+0x1bc>
mov    %rbx,%rdi
call   1030 <chroot@plt>
cmp    $0xffffffff,%eax
je     13dc <la_preinit+0x1bc>
lea    0xd8a(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0xffffffff,%eax
je     13dc <la_preinit+0x1bc>
mov    0x0(%rbp),%edi
call   10e0 <setgid@plt>
cmp    $0xffffffff,%eax
je     1481 <la_preinit+0x261>
mov    (%r15),%edi
call   1140 <setuid@plt>
cmp    $0xffffffff,%eax
je     1496 <la_preinit+0x276>
mov    $0xffffffff,%edi
xor    %esi,%esi
call   10d0 <setreuid@plt>
test   %rax,%rax
je     14c6 <la_preinit+0x2a6>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2bfd(%rip),%rax
mov    (%rax),%edi
lea    0xd18(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    14b0 <la_preinit+0x290>
mov    0x2bde(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1120 <getppid@plt>
lea    0xbff(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
lea    0xc3f(%rip),%rdi
jmp    14b7 <la_preinit+0x297>
mov    0x2ba6(%rip),%rax
mov    (%rax),%edi
lea    0xc32(%rip),%rsi
jmp    14d6 <la_preinit+0x2b6>
mov    0x2b91(%rip),%rax
mov    (%rax),%edi
lea    0xc68(%rip),%rsi
mov    %rbp,%rdx
jmp    1470 <la_preinit+0x250>
mov    0x2b7c(%rip),%rax
mov    (%rax),%edi
lea    0xc75(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    0x2b58(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%edx
lea    0xc81(%rip),%rsi
jmp    14a9 <la_preinit+0x289>
mov    0x2b43(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%edx
lea    0xc79(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
lea    0xbb3(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    0x2b13(%rip),%rax
mov    (%rax),%edi
lea    0xc59(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@stderr = global i32 0
@.str.1 = [90 x i8] c"Not enough space to copy /proc/%d/cmdline to buffer. Only 255 characters are available.\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [8 x i8] c"fopen()\00"
@.str.4 = [75 x i8] c"usage: dchroot <chroot_dir> <user> <group> <program_to_execute> <args...>\0A\00"
@.str.5 = [34 x i8] c"User %s not found in /etc/passwd\0A\00"
@.str.6 = [34 x i8] c"Group %s not found in /etc/group\0A\00"
@.str.7 = [15 x i8] c"chroot(\22%s\22): \00"
@.str.8 = [1 x i8] zeroinitializer
@.str.9 = [2 x i8] c"/\00"
@.str.10 = [13 x i8] c"setgid(%d): \00"
@.str.11 = [13 x i8] c"setuid(%d): \00"
@.str.12 = [54 x i8] c"ERROR: could not drop root privileges appropriately.\0A\00"
define void @la_preinit(i64* %0) {
%2 = alloca i64*
%3 = alloca [256 x i8]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca %struct.passwd*
%14 = alloca %struct.group*
store i64* %0, i64** %2
store i32 0, i32* %7
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 256)
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%18 = call i32 (...) @getppid()
%19 = call i32 @snprintf(i8* %17, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp sge i32 %19, 256
br i1 %20, label %21, label %26
21: ; preds = %1
%22 = load i32, i32* @stderr
%23 = call i32 (...) @getppid()
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %1
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %28, i32** %12
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = load i32*, i32** %12
%35 = call i32 @field_size(i32* %34)
%36 = load i32*, i32** %12
%37 = call i32 @field_size(i32* %36)
store i32 %37, i32* %9
%38 = load i32*, i32** %12
%39 = call i32 @field_size(i32* %38)
store i32 %39, i32* %10
%40 = load i32*, i32** %12
%41 = call i32 @field_size(i32* %40)
store i32 %41, i32* %11
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %33
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %44
%48 = load i32, i32* %11
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %47, %44, %33
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %47
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = mul i64 %57, 1
%59 = trunc i64 %58 to i32
%60 = call i8* @malloc_and_zero(i32 %59)
store i8* %60, i8** %4
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = mul i64 %63, 1
%65 = trunc i64 %64 to i32
%66 = call i8* @malloc_and_zero(i32 %65)
store i8* %66, i8** %5
%67 = load i32, i32* %11
%68 = add nsw i32 %67, 1
%69 = sext i32 %68 to i64
%70 = mul i64 %69, 1
%71 = trunc i64 %70 to i32
%72 = call i8* @malloc_and_zero(i32 %71)
store i8* %72, i8** %6
%73 = load i32*, i32** %12
%74 = call i32 @rewind(i32* %73)
%75 = load i32*, i32** %12
%76 = call i32 @field_size(i32* %75)
%77 = load i32*, i32** %12
%78 = load i8*, i8** %4
%79 = load i32, i32* %9
%80 = call i32 @get_field(i32* %77, i8* %78, i32 %79)
%81 = load i32*, i32** %12
%82 = load i8*, i8** %5
%83 = load i32, i32* %10
%84 = call i32 @get_field(i32* %81, i8* %82, i32 %83)
%85 = load i32*, i32** %12
%86 = load i8*, i8** %6
%87 = load i32, i32* %11
%88 = call i32 @get_field(i32* %85, i8* %86, i32 %87)
%89 = load i8*, i8** %5
%90 = call %struct.passwd* @getpwnam(i8* %89)
store %struct.passwd* %90, %struct.passwd** %13
%91 = load %struct.passwd*, %struct.passwd** %13
%92 = icmp eq %struct.passwd* %91, null
br i1 %92, label %93, label %98
93: ; preds = %54
%94 = load i32, i32* @stderr
%95 = load i8*, i8** %5
%96 = call i32 (i32, i8*, ...) @fprintf(i32 %94, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %95)
%97 = call i32 @exit(i32 1)
unreachable
98: ; preds = %54
%99 = load i8*, i8** %6
%100 = call %struct.group* @getgrnam(i8* %99)
store %struct.group* %100, %struct.group** %14
%101 = load %struct.group*, %struct.group** %14
%102 = icmp eq %struct.group* %101, null
br i1 %102, label %103, label %108
103: ; preds = %98
%104 = load i32, i32* @stderr
%105 = load i8*, i8** %6
%106 = call i32 (i32, i8*, ...) @fprintf(i32 %104, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i8* %105)
%107 = call i32 @exit(i32 1)
unreachable
108: ; preds = %98
%109 = load i8*, i8** %4
%110 = call i32 @chdir(i8* %109)
%111 = icmp eq i32 %110, -1
br i1 %111, label %112, label %118
112: ; preds = %108
%113 = load i32, i32* @stderr
%114 = load i8*, i8** %4
%115 = call i32 (i32, i8*, ...) @fprintf(i32 %113, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %114)
%116 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%117 = call i32 @exit(i32 1)
unreachable
118: ; preds = %108
%119 = load i8*, i8** %4
%120 = call i32 @chroot(i8* %119)
%121 = icmp eq i32 %120, -1
br i1 %121, label %122, label %128
122: ; preds = %118
%123 = load i32, i32* @stderr
%124 = load i8*, i8** %4
%125 = call i32 (i32, i8*, ...) @fprintf(i32 %123, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %124)
%126 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%127 = call i32 @exit(i32 1)
unreachable
128: ; preds = %118
%129 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%130 = icmp eq i32 %129, -1
br i1 %130, label %131, label %137
131: ; preds = %128
%132 = load i32, i32* @stderr
%133 = load i8*, i8** %4
%134 = call i32 (i32, i8*, ...) @fprintf(i32 %132, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %133)
%135 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%136 = call i32 @exit(i32 1)
unreachable
137: ; preds = %128
%138 = load %struct.group*, %struct.group** %14
%139 = getelementptr inbounds %struct.group, %struct.group* %138, i32 0, i32 0
%140 = load i32, i32* %139
%141 = call i32 @setgid(i32 %140)
%142 = icmp eq i32 %141, -1
br i1 %142, label %143, label %151
143: ; preds = %137
%144 = load i32, i32* @stderr
%145 = load %struct.group*, %struct.group** %14
%146 = getelementptr inbounds %struct.group, %struct.group* %145, i32 0, i32 0
%147 = load i32, i32* %146
%148 = call i32 (i32, i8*, ...) @fprintf(i32 %144, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0), i32 %147)
%149 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%150 = call i32 @exit(i32 1)
unreachable
151: ; preds = %137
%152 = load %struct.passwd*, %struct.passwd** %13
%153 = getelementptr inbounds %struct.passwd, %struct.passwd* %152, i32 0, i32 0
%154 = load i32, i32* %153
%155 = call i32 @setuid(i32 %154)
%156 = icmp eq i32 %155, -1
br i1 %156, label %157, label %165
157: ; preds = %151
%158 = load i32, i32* @stderr
%159 = load %struct.passwd*, %struct.passwd** %13
%160 = getelementptr inbounds %struct.passwd, %struct.passwd* %159, i32 0, i32 0
%161 = load i32, i32* %160
%162 = call i32 (i32, i8*, ...) @fprintf(i32 %158, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0), i32 %161)
%163 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%164 = call i32 @exit(i32 1)
unreachable
165: ; preds = %151
%166 = call i64 @setreuid(i32 -1, i32 0)
%167 = icmp eq i64 %166, 0
br i1 %167, label %168, label %172
168: ; preds = %165
%169 = load i32, i32* @stderr
%170 = call i32 (i32, i8*, ...) @fprintf(i32 %169, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.12, i64 0, i64 0))
%171 = call i32 @exit(i32 1)
unreachable
172: ; preds = %165
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.group = type { i32 }
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@stderr = global i32 0
@.str.1 = [90 x i8] c"Not enough space to copy /proc/%d/cmdline to buffer. Only 255 characters are available.\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [8 x i8] c"fopen()\00"
@.str.4 = [75 x i8] c"usage: dchroot <chroot_dir> <user> <group> <program_to_execute> <args...>\0A\00"
@.str.5 = [34 x i8] c"User %s not found in /etc/passwd\0A\00"
@.str.6 = [34 x i8] c"Group %s not found in /etc/group\0A\00"
@.str.7 = [15 x i8] c"chroot(\22%s\22): \00"
@.str.8 = [1 x i8] zeroinitializer
@.str.9 = [2 x i8] c"/\00"
@.str.10 = [13 x i8] c"setgid(%d): \00"
@.str.11 = [13 x i8] c"setuid(%d): \00"
@.str.12 = [54 x i8] c"ERROR: could not drop root privileges appropriately.\0A\00"
define void @la_preinit(i64* nocapture readnone %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 256)
%5 = call i32 (...) @getppid()
%6 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp sgt i32 %6, 255
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = call i32 (...) @getppid()
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = call i32 @field_size(i32* %14)
%21 = call i32 @field_size(i32* %14)
%22 = call i32 @field_size(i32* %14)
%23 = call i32 @field_size(i32* %14)
%24 = icmp eq i32 %21, 0
%25 = icmp eq i32 %22, 0
%26 = select i1 %24, i1 true, i1 %25
%27 = icmp eq i32 %23, 0
%28 = select i1 %26, i1 true, i1 %27
br i1 %28, label %29, label %33
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %19
%34 = add nsw i32 %21, 1
%35 = call i8* @malloc_and_zero(i32 %34)
%36 = add nsw i32 %22, 1
%37 = call i8* @malloc_and_zero(i32 %36)
%38 = add nsw i32 %23, 1
%39 = call i8* @malloc_and_zero(i32 %38)
%40 = call i32 @rewind(i32* %14)
%41 = call i32 @field_size(i32* %14)
%42 = call i32 @get_field(i32* %14, i8* %35, i32 %21)
%43 = call i32 @get_field(i32* %14, i8* %37, i32 %22)
%44 = call i32 @get_field(i32* %14, i8* %39, i32 %23)
%45 = call %struct.passwd* @getpwnam(i8* %37)
%46 = icmp eq %struct.passwd* %45, null
br i1 %46, label %47, label %51
47: ; preds = %33
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %37)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %33
%52 = call %struct.group* @getgrnam(i8* %39)
%53 = icmp eq %struct.group* %52, null
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i8* %39)
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %51
%59 = call i32 @chdir(i8* %35)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %35)
%64 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %58
%67 = call i32 @chroot(i8* %35)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %74
69: ; preds = %66
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %35)
%72 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %66
%75 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %82
77: ; preds = %74
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %35)
%80 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %74
%83 = getelementptr inbounds %struct.group, %struct.group* %52, i64 0, i32 0
%84 = load i32, i32* %83
%85 = call i32 @setgid(i32 %84)
%86 = icmp eq i32 %85, -1
br i1 %86, label %87, label %93
87: ; preds = %82
%88 = load i32, i32* @stderr
%89 = load i32, i32* %83
%90 = call i32 (i32, i8*, ...) @fprintf(i32 %88, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0), i32 %89)
%91 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%92 = call i32 @exit(i32 1)
unreachable
93: ; preds = %82
%94 = getelementptr inbounds %struct.passwd, %struct.passwd* %45, i64 0, i32 0
%95 = load i32, i32* %94
%96 = call i32 @setuid(i32 %95)
%97 = icmp eq i32 %96, -1
br i1 %97, label %98, label %104
98: ; preds = %93
%99 = load i32, i32* @stderr
%100 = load i32, i32* %94
%101 = call i32 (i32, i8*, ...) @fprintf(i32 %99, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0), i32 %100)
%102 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.8, i64 0, i64 0))
%103 = call i32 @exit(i32 1)
unreachable
104: ; preds = %93
%105 = call i64 @setreuid(i32 -1, i32 0)
%106 = icmp eq i64 %105, 0
br i1 %106, label %107, label %111
107: ; preds = %104
%108 = load i32, i32* @stderr
%109 = call i32 (i32, i8*, ...) @fprintf(i32 %108, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.12, i64 0, i64 0))
%110 = call i32 @exit(i32 1)
unreachable
111: ; preds = %104
ret void
}