#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 scalar_t__ uid_t;
struct passwd {
  scalar_t__ pw_gid;
  scalar_t__ pw_uid;
};
typedef scalar_t__ gid_t;

/* Variables and functions */
int ENONET;
scalar_t__ chdir(char *);
scalar_t__ chroot(char const *);
int errno;
struct passwd *getpwnam(char const *);
scalar_t__ initgroups(char const *, scalar_t__);
scalar_t__ setgid(scalar_t__);
scalar_t__ setuid(scalar_t__);
int /*<<< orphan*/ zinfo(char *, ...);
int zchroot_user(const char *root_dir, const char *user_name) {
  int errno2;
  struct passwd *pwd;
  uid_t uid;
  gid_t gid;
  uid = 0;
  if (user_name != 0) {
    if ((pwd = getpwnam(user_name)) == 0) {
      {
        errno2 = errno;
        zinfo("chroot_user: "
              "unknown user: %s",
              user_name);
        errno = errno2;
      };
      errno = ENONET;
      return -1;
    }
    uid = pwd->pw_uid;
    gid = pwd->pw_gid;
    if (setgid(gid) < 0) {
      {
        errno2 = errno;
        zinfo("chroot_user: "
              "setgid %ld : %m",
              (long)gid);
        errno = errno2;
      };
      return -1;
    }
    if (initgroups(user_name, gid) < 0) {
      {
        errno2 = errno;
        zinfo("chroot_user: "
              "initgroups: %m");
        errno = errno2;
      };
      return -1;
    }
  }
  if (root_dir) {
    if (chroot(root_dir)) {
      {
        errno2 = errno;
        zinfo("chroot_user: "
              "chroot (%s) : %m",
              root_dir);
        errno = errno2;
      };
      return -1;
    }
    if (chdir("/")) {
      {
        errno2 = errno;
        zinfo("chroot_user: "
              "chdir (/): %m");
        errno = errno2;
      };
      return -1;
    }
  }
  if (user_name != 0) {
    if (setuid(uid) < 0) {
      {
        errno2 = errno;
        zinfo("chroot_user: "
              "setuid %ld: %m",
              (long)uid);
        errno = errno2;
      };
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<zchroot_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12a0 <zchroot_user+0x130>
mov    -0x18(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11f2 <zchroot_user+0x82>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1060 <zinfo@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2e06(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    0x2df4(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a1 <zchroot_user+0x231>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <setgid@plt>
cmp    $0x0,%rax
jge    1252 <zchroot_user+0xe2>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1060 <zinfo@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2d94(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a1 <zchroot_user+0x231>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <initgroups@plt>
cmp    $0x0,%rax
jge    129b <zchroot_user+0x12b>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1060 <zinfo@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2d4b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a1 <zchroot_user+0x231>
jmp    12a0 <zchroot_user+0x130>
cmpq   $0x0,-0x10(%rbp)
je     1341 <zchroot_user+0x1d1>
mov    -0x10(%rbp),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
je     12f4 <zchroot_user+0x184>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1060 <zinfo@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2cf2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a1 <zchroot_user+0x231>
lea    0xd7a(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     133c <zchroot_user+0x1cc>
mov    0x2cc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1060 <zinfo@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2caa(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a1 <zchroot_user+0x231>
jmp    1341 <zchroot_user+0x1d1>
cmpq   $0x0,-0x18(%rbp)
je     139a <zchroot_user+0x22a>
mov    -0x30(%rbp),%rdi
call   1080 <setuid@plt>
cmp    $0x0,%rax
jge    1395 <zchroot_user+0x225>
mov    0x2c72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xd1c(%rip),%rdi
mov    $0x0,%al
call   1060 <zinfo@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2c51(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a1 <zchroot_user+0x231>
jmp    139a <zchroot_user+0x22a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zchroot_user>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r15
test   %rsi,%rsi
je     11bf <zchroot_user+0x4f>
mov    %rbx,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     11e9 <zchroot_user+0x79>
mov    (%rax),%r12
mov    0x8(%rax),%r14
mov    %r12,%rdi
call   1070 <setgid@plt>
test   %rax,%rax
js     1205 <zchroot_user+0x95>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <initgroups@plt>
test   %rax,%rax
js     126d <zchroot_user+0xfd>
test   %r15,%r15
jne    11c7 <zchroot_user+0x57>
jmp    123d <zchroot_user+0xcd>
xor    %r14d,%r14d
test   %r15,%r15
je     123d <zchroot_user+0xcd>
mov    %r15,%rdi
call   1030 <chroot@plt>
test   %rax,%rax
je     121a <zchroot_user+0xaa>
mov    0x2dfd(%rip),%rax
mov    (%rax),%ebx
lea    0xe73(%rip),%rdi
mov    %r15,%rsi
jmp    1264 <zchroot_user+0xf4>
lea    0xe10(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <zinfo@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%ebx
jmp    1284 <zchroot_user+0x114>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ebx
lea    0xe09(%rip),%rdi
mov    %r12,%rsi
jmp    1264 <zchroot_user+0xf4>
lea    0xe54(%rip),%rdi
call   1040 <chdir@plt>
test   %rax,%rax
je     123d <zchroot_user+0xcd>
mov    0x2da6(%rip),%rax
mov    (%rax),%ebx
lea    0xe3c(%rip),%rdi
jmp    127d <zchroot_user+0x10d>
xor    %ebp,%ebp
test   %rbx,%rbx
je     1292 <zchroot_user+0x122>
mov    %r14,%rdi
call   1080 <setuid@plt>
test   %rax,%rax
jns    1292 <zchroot_user+0x122>
mov    0x2d80(%rip),%rax
mov    (%rax),%ebx
lea    0xe31(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <zinfo@plt>
jmp    1284 <zchroot_user+0x114>
mov    0x2d64(%rip),%rax
mov    (%rax),%ebx
lea    0xdbe(%rip),%rdi
xor    %eax,%eax
call   1060 <zinfo@plt>
mov    0x2d4d(%rip),%rax
mov    %ebx,(%rax)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i64, i64 }
@errno = global i32 0
@.str = [30 x i8] c"chroot_user: unknown user: %s\00"
@ENONET = global i32 0
@.str.1 = [29 x i8] c"chroot_user: setgid %ld : %m\00"
@.str.2 = [28 x i8] c"chroot_user: initgroups: %m\00"
@.str.3 = [30 x i8] c"chroot_user: chroot (%s) : %m\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [27 x i8] c"chroot_user: chdir (/): %m\00"
@.str.6 = [28 x i8] c"chroot_user: setuid %ld: %m\00"
define i32 @zchroot_user(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.passwd*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 0, i64* %8
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %47
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call %struct.passwd* @getpwnam(i8* %13)
store %struct.passwd* %14, %struct.passwd** %7
%15 = icmp eq %struct.passwd* %14, null
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* @errno
store i32 %17, i32* %6
%18 = load i8*, i8** %5
%19 = call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %6
store i32 %20, i32* @errno
%21 = load i32, i32* @ENONET
store i32 %21, i32* @errno
store i32 -1, i32* %3
br label %81
22: ; preds = %12
%23 = load %struct.passwd*, %struct.passwd** %7
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 1
%25 = load i64, i64* %24
store i64 %25, i64* %8
%26 = load %struct.passwd*, %struct.passwd** %7
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i32 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* %9
%29 = load i64, i64* %9
%30 = call i64 @setgid(i64 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %22
%33 = load i32, i32* @errno
store i32 %33, i32* %6
%34 = load i64, i64* %9
%35 = call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i64 %34)
%36 = load i32, i32* %6
store i32 %36, i32* @errno
store i32 -1, i32* %3
br label %81
37: ; preds = %22
%38 = load i8*, i8** %5
%39 = load i64, i64* %9
%40 = call i64 @initgroups(i8* %38, i64 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = load i32, i32* @errno
store i32 %43, i32* %6
%44 = call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* %6
store i32 %45, i32* @errno
store i32 -1, i32* %3
br label %81
46: ; preds = %37
br label %47
47: ; preds = %46, %2
%48 = load i8*, i8** %4
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %67
50: ; preds = %47
%51 = load i8*, i8** %4
%52 = call i64 @chroot(i8* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = load i32, i32* @errno
store i32 %55, i32* %6
%56 = load i8*, i8** %4
%57 = call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %56)
%58 = load i32, i32* %6
store i32 %58, i32* @errno
store i32 -1, i32* %3
br label %81
59: ; preds = %50
%60 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %59
%63 = load i32, i32* @errno
store i32 %63, i32* %6
%64 = call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%65 = load i32, i32* %6
store i32 %65, i32* @errno
store i32 -1, i32* %3
br label %81
66: ; preds = %59
br label %67
67: ; preds = %66, %47
%68 = load i8*, i8** %5
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %80
70: ; preds = %67
%71 = load i64, i64* %8
%72 = call i64 @setuid(i64 %71)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i32, i32* @errno
store i32 %75, i32* %6
%76 = load i64, i64* %8
%77 = call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i64 %76)
%78 = load i32, i32* %6
store i32 %78, i32* @errno
store i32 -1, i32* %3
br label %81
79: ; preds = %70
br label %80
80: ; preds = %79, %67
store i32 0, i32* %3
br label %81
81: ; preds = %80, %74, %62, %54, %42, %32, %16
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.passwd = type { i64, i64 }
@errno = global i32 0
@.str = [30 x i8] c"chroot_user: unknown user: %s\00"
@ENONET = global i32 0
@.str.1 = [29 x i8] c"chroot_user: setgid %ld : %m\00"
@.str.2 = [28 x i8] c"chroot_user: initgroups: %m\00"
@.str.3 = [30 x i8] c"chroot_user: chroot (%s) : %m\00"
@.str.4 = [2 x i8] c"/\00"
@.str.5 = [27 x i8] c"chroot_user: chdir (/): %m\00"
@.str.6 = [28 x i8] c"chroot_user: setuid %ld: %m\00"
define i32 @zchroot_user(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %26, label %4
4: ; preds = %2
%5 = tail call %struct.passwd* @getpwnam(i8* %1)
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %1)
%9 = load i32, i32* @ENONET
br label %48
10: ; preds = %4
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 1
%12 = load i64, i64* %11
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%14 = load i64, i64* %13
%15 = tail call i64 @setgid(i64 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32, i32* @errno
%19 = tail call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i64 %14)
br label %48
20: ; preds = %10
%21 = tail call i64 @initgroups(i8* %1, i64 %14)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = tail call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %48
26: ; preds = %20, %2
%27 = phi i64 [ %12, %20 ], [ 0, %2 ]
%28 = icmp eq i8* %0, null
br i1 %28, label %41, label %29
29: ; preds = %26
%30 = tail call i64 @chroot(i8* %0)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* @errno
%34 = tail call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %48
35: ; preds = %29
%36 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = tail call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %48
41: ; preds = %35, %26
br i1 %3, label %50, label %42
42: ; preds = %41
%43 = tail call i64 @setuid(i64 %27)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i32, i32* @errno
%47 = tail call i32 (i8*, ...) @zinfo(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i64 %27)
br label %48
48: ; preds = %7, %17, %23, %32, %38, %45
%49 = phi i32 [ %46, %45 ], [ %39, %38 ], [ %33, %32 ], [ %24, %23 ], [ %18, %17 ], [ %9, %7 ]
store i32 %49, i32* @errno
br label %50
50: ; preds = %48, %41, %42
%51 = phi i32 [ 0, %42 ], [ 0, %41 ], [ -1, %48 ]
ret i32 %51
}