#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 */

/* Variables and functions */
scalar_t__ chdir(char *);
scalar_t__ chroot(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ repl();
int /*<<< orphan*/ setgid(int);
int /*<<< orphan*/ setuid(int);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (chroot("/home/sabetts/src/nethack-el/gl"))
    perror("cannot change root directory");
  if (chdir("/"))
    perror("cannot chdir to root directory");
  setgid(1000);
  setuid(1000);
  printf("Welcome to the nethack-el server.\n");
  fflush(stdout);
  repl();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rdi
call   1030 <chroot@plt>
cmp    $0x0,%rax
je     11b8 <main+0x38>
lea    0xe6d(%rip),%rdi
call   1090 <perror@plt>
lea    0xe7e(%rip),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     11da <main+0x5a>
lea    0xe6a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3e8,%edi
call   1080 <setgid@plt>
mov    $0x3e8,%edi
call   10a0 <setuid@plt>
lea    0xe69(%rip),%rdi
call   1060 <printf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    $0x0,%al
call   1040 <repl@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xe78(%rip),%rdi
call   1030 <chroot@plt>
test   %rax,%rax
jne    11dc <main+0x5c>
lea    0xea4(%rip),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
jne    11ea <main+0x6a>
mov    $0x3e8,%edi
call   1080 <setgid@plt>
mov    $0x3e8,%edi
call   10a0 <setuid@plt>
lea    0xea0(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
xor    %eax,%eax
call   1050 <repl@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe3d(%rip),%rdi
call   1090 <perror@plt>
jmp    1192 <main+0x12>
lea    0xe4e(%rip),%rdi
call   1090 <perror@plt>
jmp    11a3 <main+0x23>

LLVM IR (-O0):
@.str = [32 x i8] c"/home/sabetts/src/nethack-el/gl\00"
@.str.1 = [29 x i8] c"cannot change root directory\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [31 x i8] c"cannot chdir to root directory\00"
@.str.4 = [35 x i8] c"Welcome to the nethack-el server.\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = call i64 @chroot(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = call i32 @setgid(i32 1000)
%17 = call i32 @setuid(i32 1000)
%18 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
%19 = load i32, i32* @stdout
%20 = call i32 @fflush(i32 %19)
%21 = call i32 (...) @repl()
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"/home/sabetts/src/nethack-el/gl\00"
@.str.1 = [29 x i8] c"cannot change root directory\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [31 x i8] c"cannot chdir to root directory\00"
@stdout = global i32 0
@str = [34 x i8] c"Welcome to the nethack-el server.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 @chroot(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = tail call i32 @setgid(i32 1000)
%14 = tail call i32 @setuid(i32 1000)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%16 = load i32, i32* @stdout
%17 = tail call i32 @fflush(i32 %16)
%18 = tail call i32 (...) @repl()
ret i32 0
}