#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;
};
struct group {
  int /*<<< orphan*/ gr_gid;
};

/* Variables and functions */
scalar_t__ chdir(char *);
struct group *getgrnam(char *);
struct passwd *getpwnam(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int drop(char *username, char *groupname) {
  int status = 0;
  struct passwd *userinfo;
  struct group *groupinfo;
  if (username != NULL) {
    userinfo = getpwnam(username);
    if (userinfo) {
      if (setuid(userinfo->pw_uid)) {
        status = -1;
      }
      if (chdir(userinfo->pw_dir)) {
        if (chdir("/")) {
          status = -1;
        }
      }
    } else {
      status = -1;
    }
  }
  if (groupname != NULL) {
    groupinfo = getgrnam(groupname);
    if (groupinfo) {
      if (setgid(groupinfo->gr_gid)) {
        status = -1;
      }
    } else {
      status = -1;
    }
  }
  return status;
}


x86 assembly (clang, -O0):
<drop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11f0 <drop+0xa0>
mov    -0x8(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e4 <drop+0x94>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
call   1070 <setuid@plt>
cmp    $0x0,%rax
je     11a7 <drop+0x57>
movl   $0xffffffff,-0x14(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     11df <drop+0x8f>
lea    0xe3c(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     11da <drop+0x8a>
movl   $0xffffffff,-0x14(%rbp)
jmp    11df <drop+0x8f>
jmp    11eb <drop+0x9b>
movl   $0xffffffff,-0x14(%rbp)
jmp    11f0 <drop+0xa0>
cmpq   $0x0,-0x10(%rbp)
je     1240 <drop+0xf0>
mov    -0x10(%rbp),%rdi
call   1050 <getgrnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1234 <drop+0xe4>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1060 <setgid@plt>
cmp    $0x0,%rax
je     122f <drop+0xdf>
movl   $0xffffffff,-0x14(%rbp)
jmp    123b <drop+0xeb>
movl   $0xffffffff,-0x14(%rbp)
jmp    1240 <drop+0xf0>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
test   %rdi,%rdi
je     119d <drop+0x4d>
call   1040 <getpwnam@plt>
test   %rax,%rax
je     11c1 <drop+0x71>
mov    %rax,%rbx
mov    0x8(%rax),%edi
call   1070 <setuid@plt>
neg    %rax
sbb    %ebp,%ebp
mov    (%rbx),%rdi
call   1030 <chdir@plt>
test   %rax,%rax
je     119f <drop+0x4f>
lea    0xe76(%rip),%rdi
call   1030 <chdir@plt>
neg    %rax
sbb    %eax,%eax
or     %eax,%ebp
test   %r14,%r14
jne    11a4 <drop+0x54>
jmp    11d2 <drop+0x82>
xor    %ebp,%ebp
test   %r14,%r14
je     11d2 <drop+0x82>
mov    %r14,%rdi
call   1050 <getgrnam@plt>
test   %rax,%rax
je     11cd <drop+0x7d>
mov    (%rax),%edi
call   1060 <setgid@plt>
neg    %rax
sbb    %eax,%eax
or     %eax,%ebp
jmp    11d2 <drop+0x82>
mov    $0xffffffff,%ebp
test   %r14,%r14
jne    11a4 <drop+0x54>
jmp    11d2 <drop+0x82>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8*, i32 }
%struct.group = type { i32 }
@.str = [2 x i8] c"/\00"
define i32 @drop(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.passwd*
%7 = alloca %struct.group*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %36
10: ; preds = %2
%11 = load i8*, i8** %3
%12 = call %struct.passwd* @getpwnam(i8* %11)
store %struct.passwd* %12, %struct.passwd** %6
%13 = load %struct.passwd*, %struct.passwd** %6
%14 = icmp ne %struct.passwd* %13, null
br i1 %14, label %15, label %34
15: ; preds = %10
%16 = load %struct.passwd*, %struct.passwd** %6
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i64 @setuid(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 -1, i32* %5
br label %22
22: ; preds = %21, %15
%23 = load %struct.passwd*, %struct.passwd** %6
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i64 @chdir(i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 -1, i32* %5
br label %32
32: ; preds = %31, %28
br label %33
33: ; preds = %32, %22
br label %35
34: ; preds = %10
store i32 -1, i32* %5
br label %35
35: ; preds = %34, %33
br label %36
36: ; preds = %35, %2
%37 = load i8*, i8** %4
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %54
39: ; preds = %36
%40 = load i8*, i8** %4
%41 = call %struct.group* @getgrnam(i8* %40)
store %struct.group* %41, %struct.group** %7
%42 = load %struct.group*, %struct.group** %7
%43 = icmp ne %struct.group* %42, null
br i1 %43, label %44, label %52
44: ; preds = %39
%45 = load %struct.group*, %struct.group** %7
%46 = getelementptr inbounds %struct.group, %struct.group* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i64 @setgid(i32 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %44
store i32 -1, i32* %5
br label %51
51: ; preds = %50, %44
br label %53
52: ; preds = %39
store i32 -1, i32* %5
br label %53
53: ; preds = %52, %51
br label %54
54: ; preds = %53, %36
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i32 }
%struct.group = type { i32 }
@.str = [2 x i8] c"/\00"
define i32 @drop(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %21, label %4
4: ; preds = %2
%5 = tail call %struct.passwd* @getpwnam(i8* %0)
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %21, label %7
7: ; preds = %4
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 1
%9 = load i32, i32* %8
%10 = tail call i64 @setuid(i32 %9)
%11 = icmp ne i64 %10, 0
%12 = sext i1 %11 to i32
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = tail call i64 @chdir(i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %7
%18 = tail call i64 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
%20 = select i1 %19, i32 %12, i32 -1
br label %21
21: ; preds = %17, %4, %7, %2
%22 = phi i32 [ %12, %7 ], [ 0, %2 ], [ %20, %17 ], [ -1, %4 ]
%23 = icmp eq i8* %1, null
br i1 %23, label %33, label %24
24: ; preds = %21
%25 = tail call %struct.group* @getgrnam(i8* %1)
%26 = icmp eq %struct.group* %25, null
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = getelementptr inbounds %struct.group, %struct.group* %25, i64 0, i32 0
%29 = load i32, i32* %28
%30 = tail call i64 @setgid(i32 %29)
%31 = icmp eq i64 %30, 0
%32 = select i1 %31, i32 %22, i32 -1
br label %33
33: ; preds = %27, %24, %21
%34 = phi i32 [ %22, %21 ], [ %32, %27 ], [ -1, %24 ]
ret i32 %34
}