// File: /workspace/github_cpp_makefile_star2_O3/tylerkaraszewski[P]Argyle/main.cpp
// Function: setUser(char const*, Logger&)
bool setUser(const char* runAs, Logger& logger)
{
  bool retval = true;
  struct passwd pwd;
  struct passwd* pwdPtr = 0;

  size_t bufsize = 32;
  char* buffer = static_cast<char*>(malloc(bufsize));
  if (buffer == NULL)
  {
    logger.logError("Setuser malloc error: Out of Memory");
    return false;
  }
  int e;
  while ((e = getpwnam_r(runAs, &pwd, buffer, bufsize, &pwdPtr)) == ERANGE)
  {
    bufsize *= 2;
    // this needs to do a better job of freeing things if it fails.
    buffer = static_cast<char*>(realloc(static_cast<void*>(buffer), bufsize));
    if (buffer == NULL)
    {
      logger.logError("Setuser realloc error: Out of Memory");
      return false;
    }
  }
  if (e != 0)
  {
    logger.logError("Getpwdnam_r error(" + Utils::llToString(errno) + "): " + strerror(errno));
    retval = false;
  }
  else if (pwdPtr == 0)
  {
    logger.logError(string("Getpwdnam_r failed, couldn't find user ") + runAs);
    retval = false;
  }

  // Haven't failed yet.
  if (retval)
  {
    // Set group first or we lose the abilty to after we're not superuser.
    if (setegid(pwd.pw_gid) == -1)
    {
      logger.logError("Setegid error(" + Utils::llToString(errno) + "): " + strerror(errno));
      retval = false;
    }
    else if (seteuid(pwd.pw_uid) == -1)
    {
      logger.logError("Seteuid error(" + Utils::llToString(errno) + "): " + strerror(errno));
      retval = false;
    }
  }
  free(buffer);
  return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq $0x0, 0x80(%rsp)
movl $0x20, %r12d
movl $0x20, %edi
callq 0x3380
testq %rax, %rax
je 0x631d
movq %rax, %rbx
leaq 0x88(%rsp), %r13
leaq 0x80(%rsp), %rbp
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r12, %rcx
movq %rbp, %r8
callq 0x3510
cmpl $0x22, %eax
jne 0x627a
addq %r12, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x34d0
movq %rax, %rbx
testq %rax, %rax
jne 0x621c
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x5f59(%rip), %rsi      # 0xc1b4
leaq 0x5f76(%rip), %rdx      # 0xc1d8
movq %rsp, %rdi
callq 0x513a
movq %rsp, %rsi
movq %r14, %rdi
callq 0xabae
jmp 0x6347
testl %eax, %eax
jne 0x6364
cmpq $0x0, 0x80(%rsp)
je 0x63bc
movl 0x9c(%rsp), %edi
callq 0x3370
cmpl $-0x1, %eax
je 0x6555
movl 0x98(%rsp), %edi
callq 0x3150
movb $0x1, %bpl
cmpl $-0x1, %eax
jne 0x6609
callq 0x3040
movq %rax, %r15
movslq (%rax), %rsi
leaq 0x60(%rsp), %r12
movq %r12, %rdi
callq 0xabbc
leaq 0x5f46(%rip), %rcx      # 0xc223
movl $0xe, %r8d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32e0
leaq 0x50(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x6707
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x670f
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x5e63(%rip), %rsi      # 0xc190
leaq 0x5e7f(%rip), %rdx      # 0xc1b3
movq %rsp, %rdi
callq 0x513a
movq %rsp, %rsi
movq %r14, %rdi
callq 0xabae
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x635d
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3290
xorl %ebp, %ebp
jmp 0x6611
callq 0x3040
movq %rax, %r15
movslq (%rax), %rsi
leaq 0x60(%rsp), %r12
movq %r12, %rdi
callq 0xabbc
leaq 0x5e56(%rip), %rcx      # 0xc1d9
movl $0x12, %r8d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32e0
leaq 0x50(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x6418
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x6420
leaq 0x30(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x5e1f(%rip), %rsi      # 0xc1ec
leaq 0x5e3f(%rip), %rdx      # 0xc213
leaq 0x20(%rsp), %rdi
callq 0x513a
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x3560
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x65b1
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0x65b8
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq 0x40(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x5beb(%rip), %rsi      # 0xc02e
callq 0x3560
leaq 0x30(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x646f
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x6476
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
leaq 0x20(%rsp), %r12
movq %rdx, 0x8(%r12)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl (%r15), %edi
callq 0x3120
movq %r12, %rdi
movq %rax, %rsi
callq 0x3560
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x64cc
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0x64d3
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rsp, %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0xabae
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x650b
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x20(%rsp), %rdi
cmpq %rbp, %rdi
je 0x6522
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0x6539
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6607
movq 0x70(%rsp), %rsi
jmp 0x65ff
callq 0x3040
movq %rax, %r15
movslq (%rax), %rsi
leaq 0x60(%rsp), %r12
movq %r12, %rdi
callq 0xabbc
leaq 0x5ca0(%rip), %rcx      # 0xc214
movl $0xe, %r8d
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32e0
leaq 0x50(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x6625
movq %rdx, 0x40(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x50(%rsp)
jmp 0x662d
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rsp, %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0xabae
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x65f0
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x6607
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3290
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x3330
movl %ebp, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq 0x40(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x59de(%rip), %rsi      # 0xc02e
callq 0x3560
leaq 0x30(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x667c
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x6683
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
leaq 0x20(%rsp), %r12
movq %rdx, 0x8(%r12)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl (%r15), %edi
callq 0x3120
movq %r12, %rdi
movq %rax, %rsi
callq 0x3560
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x66d9
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0x66e0
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rsp, %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0xabae
jmp 0x64f5
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq 0x40(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x58fc(%rip), %rsi      # 0xc02e
callq 0x3560
leaq 0x30(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x675e
movq %rdx, 0x20(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x30(%rsp)
jmp 0x6765
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
leaq 0x20(%rsp), %r12
movq %rdx, 0x8(%r12)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl (%r15), %edi
callq 0x3120
movq %r12, %rdi
movq %rax, %rsi
callq 0x3560
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x67bb
movq %rdx, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0x67c2
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rsp, %rsi
movq %rdx, 0x8(%rsi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0xabae
jmp 0x64f5
jmp 0x6844
jmp 0x685f
jmp 0x687b
jmp 0x6897
jmp 0x6844
jmp 0x685f
jmp 0x687b
jmp 0x6897
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x6820
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x6820
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x68cd
movq 0x30(%rsp), %rsi
jmp 0x68c0
jmp 0x68ca
jmp 0x68af
jmp 0x68ca
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x6862
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x6862
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %rbp, %rdi
je 0x687e
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x687e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0x689a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x689a
movq %rax, %rbx
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x68cd
movq 0x70(%rsp), %rsi
jmp 0x68c0
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x68cd
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x68cd
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3520