#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 */
scalar_t__ chdir(char *);
scalar_t__ errno;
int /*<<< orphan*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
struct passwd *getpwnam(char *);
int getuid();
scalar_t__ initgroups(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setenv(char *, char *, int);
scalar_t__ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setpgrp();
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ showError(char *);
int /*<<< orphan*/ showHelp();
scalar_t__ strnlen(char *, int);
int main(int argc, char **argv) {
  if (argc < 2) {
    showHelp();
    exit(1);
  };
  char *username = argv[1];
  if (!username || strnlen(username, 32) == 0) {
    showError("Username must not be blank");
    exit(1);
  }
  struct passwd *pw;
  errno = 0;
  int uid = getuid();
  pw = getpwnam(username);
  if (errno) {
    showError("getpwnam()");
    exit(1);
  }
  if (!pw) {
    showError("Unknown user");
    exit(1);
  }
  if (setgid(pw->pw_gid)) {
    showError("Failed to set gid");
    exit(1);
  }
  if (initgroups(username, 0)) {
  }
  if (setuid(pw->pw_uid)) {
    showError("Failed to set uid");
    exit(1);
  }
  setpgrp();
  setenv("USER", username, 1);
  setenv("HOME", pw->pw_dir, 1);
  if (uid) {
    execlp("nodejs", "nodejs", "bin/nodex", NULL);
  } else {
    if (chdir(pw->pw_dir)) {
      showError("chdir()");
      exit(1);
    }
    setenv("NODE_PATH", "/usr/share/nodes/node_modules", 1);
    execlp("nodejs", "nodejs", "/usr/sbin/nodex", NULL);
  }
  showError("exec()");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1201 <main+0x31>
mov    $0x0,%al
call   1030 <showHelp@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1230 <main+0x60>
mov    -0x18(%rbp),%rdi
mov    $0x20,%esi
call   1080 <strnlen@plt>
cmp    $0x0,%rax
jne    1246 <main+0x76>
lea    0xdc9(%rip),%rdi
call   1070 <showError@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2d93(%rip),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d6e(%rip),%rax
cmpq   $0x0,(%rax)
je     1292 <main+0xc2>
lea    0xd98(%rip),%rdi
call   1070 <showError@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    12b3 <main+0xe3>
lea    0xd82(%rip),%rdi
call   1070 <showError@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%edi
call   10a0 <setgid@plt>
cmp    $0x0,%rax
je     12df <main+0x10f>
lea    0xd63(%rip),%rdi
call   1070 <showError@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   10e0 <initgroups@plt>
cmp    $0x0,%rax
je     12f9 <main+0x129>
jmp    12f9 <main+0x129>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
call   10c0 <setuid@plt>
cmp    $0x0,%rax
je     1325 <main+0x155>
lea    0xd2f(%rip),%rdi
call   1070 <showError@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10f0 <setpgrp@plt>
mov    -0x18(%rbp),%rsi
lea    0xd20(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xd0d(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmpl   $0x0,-0x24(%rbp)
je     1382 <main+0x1b2>
lea    0xcf7(%rip),%rsi
lea    0xd2d(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10d0 <execlp@plt>
jmp    13e0 <main+0x210>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     13ae <main+0x1de>
lea    0xcc9(%rip),%rdi
call   1070 <showError@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xcbb(%rip),%rdi
lea    0xcbe(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    0xc94(%rip),%rsi
lea    0xcc4(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10d0 <execlp@plt>
lea    0xcc1(%rip),%rdi
call   1070 <showError@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    1307 <main+0x137>
mov    0x8(%rsi),%rbx
test   %rbx,%rbx
je     12fe <main+0x12e>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1080 <strnlen@plt>
test   %rax,%rax
je     12fe <main+0x12e>
mov    0x2dd6(%rip),%r15
movq   $0x0,(%r15)
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1090 <getpwnam@plt>
cmpq   $0x0,(%r15)
jne    1318 <main+0x148>
mov    %rax,%rbp
test   %rax,%rax
je     1321 <main+0x151>
mov    0xc(%rbp),%edi
call   10a0 <setgid@plt>
test   %rax,%rax
jne    132a <main+0x15a>
mov    %rbx,%rdi
xor    %esi,%esi
call   10e0 <initgroups@plt>
mov    0x8(%rbp),%edi
call   10c0 <setuid@plt>
test   %rax,%rax
jne    1333 <main+0x163>
xor    %eax,%eax
call   10f0 <setpgrp@plt>
lea    0xde4(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    0x0(%rbp),%rsi
lea    0xdd1(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
test   %r14d,%r14d
je     12a3 <main+0xd3>
lea    0xdfd(%rip),%rdx
jmp    12d4 <main+0x104>
mov    0x0(%rbp),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    133c <main+0x16c>
lea    0xdb4(%rip),%rdi
lea    0xdb7(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    0xdc4(%rip),%rdx
lea    0xd86(%rip),%rdi
mov    %rdi,%rsi
xor    %ecx,%ecx
call   10d0 <execlp@plt>
lea    0xdbc(%rip),%rdi
call   1070 <showError@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfb(%rip),%rdi
jmp    1343 <main+0x173>
xor    %eax,%eax
call   1030 <showHelp@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xcfc(%rip),%rdi
jmp    1343 <main+0x173>
lea    0xcfe(%rip),%rdi
jmp    1343 <main+0x173>
lea    0xd02(%rip),%rdi
jmp    1343 <main+0x173>
lea    0xd0b(%rip),%rdi
jmp    1343 <main+0x173>
lea    0xd25(%rip),%rdi
call   1070 <showError@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8*, i32, i32 }
@.str = [27 x i8] c"Username must not be blank\00"
@errno = global i64 0
@.str.1 = [11 x i8] c"getpwnam()\00"
@.str.2 = [13 x i8] c"Unknown user\00"
@.str.3 = [18 x i8] c"Failed to set gid\00"
@.str.4 = [18 x i8] c"Failed to set uid\00"
@.str.5 = [5 x i8] c"USER\00"
@.str.6 = [5 x i8] c"HOME\00"
@.str.7 = [7 x i8] c"nodejs\00"
@.str.8 = [10 x i8] c"bin/nodex\00"
@.str.9 = [8 x i8] c"chdir()\00"
@.str.10 = [10 x i8] c"NODE_PATH\00"
@.str.11 = [30 x i8] c"/usr/share/nodes/node_modules\00"
@.str.12 = [16 x i8] c"/usr/sbin/nodex\00"
@.str.13 = [7 x i8] c"exec()\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.passwd*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (...) @showHelp()
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i8*, i8** %6
%22 = call i64 @strnlen(i8* %21, i32 32)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20, %14
%25 = call i32 @showError(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20
store i64 0, i64* @errno
%28 = call i32 (...) @getuid()
store i32 %28, i32* %8
%29 = load i8*, i8** %6
%30 = call %struct.passwd* @getpwnam(i8* %29)
store %struct.passwd* %30, %struct.passwd** %7
%31 = load i64, i64* @errno
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @showError(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = load %struct.passwd*, %struct.passwd** %7
%38 = icmp ne %struct.passwd* %37, null
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = call i32 @showError(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %36
%43 = load %struct.passwd*, %struct.passwd** %7
%44 = getelementptr inbounds %struct.passwd, %struct.passwd* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = call i64 @setgid(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = call i32 @showError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %42
%52 = load i8*, i8** %6
%53 = call i64 @initgroups(i8* %52, i32 0)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %51
br label %56
56: ; preds = %55, %51
%57 = load %struct.passwd*, %struct.passwd** %7
%58 = getelementptr inbounds %struct.passwd, %struct.passwd* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = call i64 @setuid(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %56
%63 = call i32 @showError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %56
%66 = call i32 (...) @setpgrp()
%67 = load i8*, i8** %6
%68 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %67, i32 1)
%69 = load %struct.passwd*, %struct.passwd** %7
%70 = getelementptr inbounds %struct.passwd, %struct.passwd* %69, i32 0, i32 0
%71 = load i8*, i8** %70
%72 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %71, i32 1)
%73 = load i32, i32* %8
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %65
%76 = call i32 @execlp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32* null)
br label %89
77: ; preds = %65
%78 = load %struct.passwd*, %struct.passwd** %7
%79 = getelementptr inbounds %struct.passwd, %struct.passwd* %78, i32 0, i32 0
%80 = load i8*, i8** %79
%81 = call i64 @chdir(i8* %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %77
%84 = call i32 @showError(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
%85 = call i32 @exit(i32 1)
unreachable
86: ; preds = %77
%87 = call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0), i32 1)
%88 = call i32 @execlp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.12, i64 0, i64 0), i32* null)
br label %89
89: ; preds = %86, %75
%90 = call i32 @showError(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.13, i64 0, i64 0))
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i32, i32 }
@.str = [27 x i8] c"Username must not be blank\00"
@errno = global i64 0
@.str.1 = [11 x i8] c"getpwnam()\00"
@.str.2 = [13 x i8] c"Unknown user\00"
@.str.3 = [18 x i8] c"Failed to set gid\00"
@.str.4 = [18 x i8] c"Failed to set uid\00"
@.str.5 = [5 x i8] c"USER\00"
@.str.6 = [5 x i8] c"HOME\00"
@.str.7 = [7 x i8] c"nodejs\00"
@.str.8 = [10 x i8] c"bin/nodex\00"
@.str.9 = [8 x i8] c"chdir()\00"
@.str.10 = [10 x i8] c"NODE_PATH\00"
@.str.11 = [30 x i8] c"/usr/share/nodes/node_modules\00"
@.str.12 = [16 x i8] c"/usr/sbin/nodex\00"
@.str.13 = [7 x i8] c"exec()\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 (...) @showHelp()
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = tail call i64 @strnlen(i8* %9, i32 32)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11, %7
%15 = tail call i32 @showError(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %11
store i64 0, i64* @errno
%18 = tail call i32 (...) @getuid()
%19 = tail call %struct.passwd* @getpwnam(i8* %9)
%20 = load i64, i64* @errno
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = tail call i32 @showError(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = icmp eq %struct.passwd* %19, null
br i1 %26, label %27, label %30
27: ; preds = %25
%28 = tail call i32 @showError(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %25
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i64 0, i32 2
%32 = load i32, i32* %31
%33 = tail call i64 @setgid(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = tail call i32 @showError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = tail call i64 @initgroups(i8* %9, i32 0)
%40 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i64 0, i32 1
%41 = load i32, i32* %40
%42 = tail call i64 @setuid(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %38
%45 = tail call i32 @showError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%46 = tail call i32 @exit(i32 1)
unreachable
47: ; preds = %38
%48 = tail call i32 (...) @setpgrp()
%49 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* %9, i32 1)
%50 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i64 0, i32 0
%51 = load i8*, i8** %50
%52 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* %51, i32 1)
%53 = icmp eq i32 %18, 0
br i1 %53, label %54, label %63
54: ; preds = %47
%55 = load i8*, i8** %50
%56 = tail call i64 @chdir(i8* %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %54
%59 = tail call i32 @showError(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
%60 = tail call i32 @exit(i32 1)
unreachable
61: ; preds = %54
%62 = tail call i32 @setenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0), i32 1)
br label %63
63: ; preds = %47, %61
%64 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.12, i64 0, i64 0), %61 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), %47 ]
%65 = tail call i32 @execlp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* %64, i32* null)
%66 = tail call i32 @showError(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.13, i64 0, i64 0))
ret i32 0
}