// File: /workspace/github_cpp_makefile_star2_O2/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
andq $0x0, 0x60(%rsp)
pushq $0x20
popq %r12
movq %r12, %rdi
callq 0x33b0
testq %rax, %rax
je 0x53b1
movq %rax, %rbx
leaq 0x88(%rsp), %r13
leaq 0x60(%rsp), %rbp
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r12, %rcx
movq %rbp, %r8
callq 0x3500
cmpl $0x22, %eax
jne 0x5306
addq %r12, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x34e0
movq %rax, %rbx
testq %rax, %rax
jne 0x52b3
leaq 0x3ecb(%rip), %rsi      # 0x91b4
movq %rsp, %rdi
leaq 0x20(%rsp), %rdx
callq 0x438c
movq %rsp, %rsi
movq %r14, %rdi
callq 0x80f6
jmp 0x53d0
testl %eax, %eax
jne 0x53df
cmpq $0x0, 0x60(%rsp)
je 0x5467
movl 0x9c(%rsp), %edi
callq 0x33a0
cmpl $-0x1, %eax
je 0x54c8
movl 0x98(%rsp), %edi
callq 0x3140
movb $0x1, %bpl
cmpl $-0x1, %eax
jne 0x54ac
callq 0x3040
movq %rax, %r15
movslq (%rax), %rsi
leaq 0x68(%rsp), %r12
movq %r12, %rdi
callq 0x8104
leaq 0x3ebd(%rip), %rsi      # 0x9223
leaq 0x40(%rsp), %rdi
movq %r12, %rdx
callq 0x42fb
leaq 0x3cb4(%rip), %rdx      # 0x902e
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x42dc
movl (%r15), %edi
callq 0x3110
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
movq %rax, %rdx
callq 0x42dc
movq %rsp, %rsi
movq %r14, %rdi
callq 0x80f6
jmp 0x5444
leaq 0x3dd8(%rip), %rsi      # 0x9190
movq %rsp, %rdi
leaq 0x20(%rsp), %rdx
callq 0x438c
movq %rsp, %rsi
movq %r14, %rdi
callq 0x80f6
movq %rsp, %rdi
callq 0x35d8
xorl %ebp, %ebp
jmp 0x54b4
callq 0x3040
movq %rax, %r15
movslq (%rax), %rsi
leaq 0x68(%rsp), %r12
movq %r12, %rdi
callq 0x8104
leaq 0x3ddb(%rip), %rsi      # 0x91d9
leaq 0x40(%rsp), %rdi
movq %r12, %rdx
callq 0x42fb
leaq 0x3c1c(%rip), %rdx      # 0x902e
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x42dc
movl (%r15), %edi
callq 0x3110
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
movq %rax, %rdx
callq 0x42dc
movq %rsp, %rsi
movq %r14, %rdi
callq 0x80f6
movq %rsp, %rdi
callq 0x35d8
leaq 0x20(%rsp), %rdi
callq 0x35d8
leaq 0x40(%rsp), %rdi
callq 0x35d8
leaq 0x68(%rsp), %rdi
jmp 0x54a5
leaq 0x3d7e(%rip), %rsi      # 0x91ec
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x438c
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x42dc
movq %rsp, %rsi
movq %r14, %rdi
callq 0x80f6
movq %rsp, %rdi
callq 0x35d8
leaq 0x20(%rsp), %rdi
callq 0x35d8
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x3350
movl %ebp, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3040
movq %rax, %r15
movslq (%rax), %rsi
leaq 0x68(%rsp), %r12
movq %r12, %rdi
callq 0x8104
leaq 0x3d2d(%rip), %rsi      # 0x9214
leaq 0x40(%rsp), %rdi
movq %r12, %rdx
callq 0x42fb
leaq 0x3b33(%rip), %rdx      # 0x902e
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x42dc
movl (%r15), %edi
callq 0x3110
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
movq %rax, %rdx
callq 0x42dc
movq %rsp, %rsi
movq %r14, %rdi
callq 0x80f6
jmp 0x5444
jmp 0x555f
jmp 0x556c
jmp 0x557b
jmp 0x558a
jmp 0x555f
jmp 0x556c
jmp 0x557b
jmp 0x558a
movq %rax, %rbx
movq %rsp, %rdi
callq 0x35d8
jmp 0x5552
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
jmp 0x559a
jmp 0x55a1
jmp 0x5594
jmp 0x55a1
movq %rax, %rbx
movq %rsp, %rdi
callq 0x35d8
jmp 0x556f
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x35d8
jmp 0x557e
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x35d8
jmp 0x558d
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0x559a
movq %rax, %rbx
movq %rsp, %rdi
callq 0x35d8
jmp 0x55a4
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3510