#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_name;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_WARNING;
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ closelog();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *,
                          int /*<<< orphan*/);
void subsystem(const struct passwd *pw) {
  if (pw->pw_dir[0] != '/') {
    printf("Invalid root directory \"%s\"\n", pw->pw_dir);
    syslog(LOG_WARNING, "invalid root `%s' for user `%s'\n", pw->pw_dir,
           pw->pw_name);
    closelog();
    exit(1);
  }
  if (chdir(pw->pw_dir) || chroot(pw->pw_dir)) {
    printf("Can't change root directory to \"%s\"\n", pw->pw_dir);
    syslog(LOG_WARNING, "no subsystem root `%s' for user `%s'\n", pw->pw_dir,
           pw->pw_name);
    closelog();
    exit(1);
  }
}


x86 assembly (clang, -O0):
<subsystem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11c6 <subsystem+0x66>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xe73(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
lea    0xe6d(%rip),%rsi
call   1070 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jne    11f2 <subsystem+0x92>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <chroot@plt>
cmp    $0x0,%rax
je     1239 <subsystem+0xd9>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3e(%rip),%rdi
call   1060 <printf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
lea    0xe40(%rip),%rsi
call   1070 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<subsystem>:
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%rsi
cmpb   $0x2f,(%rsi)
jne    11af <subsystem+0x4f>
mov    %rsi,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    1188 <subsystem+0x28>
mov    (%rbx),%rdi
call   1040 <chroot@plt>
test   %rax,%rax
jne    1188 <subsystem+0x28>
pop    %rbx
ret
mov    (%rbx),%rsi
lea    0xeac(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
mov    0x8(%rbx),%ecx
lea    0xeb6(%rip),%rsi
jmp    11d1 <subsystem+0x71>
lea    0xe4a(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
mov    0x8(%rbx),%ecx
lea    0xe4c(%rip),%rsi
call   1070 <syslog@plt>
xor    %eax,%eax
call   1030 <closelog@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8*, i32 }
@.str = [29 x i8] c"Invalid root directory \22%s\22\0A\00"
@LOG_WARNING = global i32 0
@.str.1 = [33 x i8] c"invalid root `%s' for user `%s'\0A\00"
@.str.2 = [37 x i8] c"Can't change root directory to \22%s\22\0A\00"
@.str.3 = [38 x i8] c"no subsystem root `%s' for user `%s'\0A\00"
define void @subsystem(%struct.passwd* %0) {
%2 = alloca %struct.passwd*
store %struct.passwd* %0, %struct.passwd** %2
%3 = load %struct.passwd*, %struct.passwd** %2
%4 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i32 0, i32 0
%5 = load i8*, i8** %4
%6 = getelementptr inbounds i8, i8* %5, i64 0
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
%9 = icmp ne i32 %8, 47
br i1 %9, label %10, label %25
10: ; preds = %1
%11 = load %struct.passwd*, %struct.passwd** %2
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i32 0, i32 0
%13 = load i8*, i8** %12
%14 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @LOG_WARNING
%16 = load %struct.passwd*, %struct.passwd** %2
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 0
%18 = load i8*, i8** %17
%19 = load %struct.passwd*, %struct.passwd** %2
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 @syslog(i32 %15, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %21)
%23 = call i32 (...) @closelog()
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %1
%26 = load %struct.passwd*, %struct.passwd** %2
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i64 @chdir(i8* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %25
%32 = load %struct.passwd*, %struct.passwd** %2
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i64 @chroot(i8* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %52
37: ; preds = %31, %25
%38 = load %struct.passwd*, %struct.passwd** %2
%39 = getelementptr inbounds %struct.passwd, %struct.passwd* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* @LOG_WARNING
%43 = load %struct.passwd*, %struct.passwd** %2
%44 = getelementptr inbounds %struct.passwd, %struct.passwd* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = load %struct.passwd*, %struct.passwd** %2
%47 = getelementptr inbounds %struct.passwd, %struct.passwd* %46, i32 0, i32 1
%48 = load i32, i32* %47
%49 = call i32 @syslog(i32 %42, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %45, i32 %48)
%50 = call i32 (...) @closelog()
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %31
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i32 }
@.str = [29 x i8] c"Invalid root directory \22%s\22\0A\00"
@LOG_WARNING = global i32 0
@.str.1 = [33 x i8] c"invalid root `%s' for user `%s'\0A\00"
@.str.2 = [37 x i8] c"Can't change root directory to \22%s\22\0A\00"
@.str.3 = [38 x i8] c"no subsystem root `%s' for user `%s'\0A\00"
define void @subsystem(%struct.passwd* nocapture %0) {
%2 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 0
%3 = load i8*, i8** %2
%4 = load i8, i8* %3
%5 = icmp eq i8 %4, 47
br i1 %5, label %15, label %6
6: ; preds = %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %3)
%8 = load i32, i32* @LOG_WARNING
%9 = load i8*, i8** %2
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 1
%11 = load i32, i32* %10
%12 = tail call i32 @syslog(i32 %8, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %9, i32 %11)
%13 = tail call i32 (...) @closelog()
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = tail call i64 @chdir(i8* %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i8*, i8** %2
%20 = tail call i64 @chroot(i8* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %32, label %22
22: ; preds = %18, %15
%23 = load i8*, i8** %2
%24 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @LOG_WARNING
%26 = load i8*, i8** %2
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 1
%28 = load i32, i32* %27
%29 = tail call i32 @syslog(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %26, i32 %28)
%30 = tail call i32 (...) @closelog()
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %18
ret void
}