#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 */
typedef int /*<<< orphan*/ uid_t;
struct passwd {
  int /*<<< orphan*/ pw_gid;
  int /*<<< orphan*/ pw_uid;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
struct passwd *getpwnam(char const *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setgroups(int, int /*<<< orphan*/ *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *);
int /*<<< orphan*/ warnx(char *, char const *);
int drop_privileges(void) {
  static const char user[] = "_timedc";
  const struct passwd *pw;
  uid_t uid;
  gid_t gid;
  if ((pw = getpwnam(user)) == NULL) {
    warnx("getpwnam(\"%s\") failed", user);
    return -1;
  }
  uid = pw->pw_uid;
  gid = pw->pw_gid;
  if (setgroups(1, &gid)) {
    warn("setgroups");
    return -1;
  }
  if (setgid(gid)) {
    warn("setgid");
    return -1;
  }
  if (setuid(uid)) {
    warn("setuid");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<drop_privileges>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a1 <drop_privileges+0x41>
lea    0xe7f(%rip),%rdi
lea    0xe70(%rip),%rsi
call   1040 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <drop_privileges+0xdf>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1,%edi
lea    -0x18(%rbp),%rsi
call   1050 <setgroups@plt>
cmp    $0x0,%rax
je     11e4 <drop_privileges+0x84>
lea    0xe4b(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <drop_privileges+0xdf>
mov    -0x18(%rbp),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
je     120e <drop_privileges+0xae>
lea    0xe2b(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <drop_privileges+0xdf>
mov    -0x14(%rbp),%edi
call   1080 <setuid@plt>
cmp    $0x0,%rax
je     1238 <drop_privileges+0xd8>
lea    0xe08(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123f <drop_privileges+0xdf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privileges>:
push   %rbx
sub    $0x10,%rsp
lea    0xe94(%rip),%rdi
call   1060 <getpwnam@plt>
test   %rax,%rax
je     119c <drop_privileges+0x3c>
mov    (%rax),%ecx
mov    0x4(%rax),%ebx
mov    %ecx,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    $0x1,%edi
call   1050 <setgroups@plt>
test   %rax,%rax
je     11b1 <drop_privileges+0x51>
lea    0xe84(%rip),%rdi
jmp    11db <drop_privileges+0x7b>
lea    0xe65(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1040 <warnx@plt>
jmp    11e0 <drop_privileges+0x80>
mov    0xc(%rsp),%edi
call   1070 <setgid@plt>
test   %rax,%rax
je     11c8 <drop_privileges+0x68>
lea    0xe62(%rip),%rdi
jmp    11db <drop_privileges+0x7b>
mov    %ebx,%edi
call   1080 <setuid@plt>
test   %rax,%rax
je     11eb <drop_privileges+0x8b>
lea    0xe54(%rip),%rdi
call   1030 <warn@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
jmp    11e5 <drop_privileges+0x85>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@drop_privileges.user = internal constant [8 x i8] c"_timedc\00"
@.str = [22 x i8] c"getpwnam(\22%s\22) failed\00"
@.str.1 = [10 x i8] c"setgroups\00"
@.str.2 = [7 x i8] c"setgid\00"
@.str.3 = [7 x i8] c"setuid\00"
define i32 @drop_privileges() {
%1 = alloca i32
%2 = alloca %struct.passwd*
%3 = alloca i32
%4 = alloca i32
%5 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @drop_privileges.user, i64 0, i64 0))
store %struct.passwd* %5, %struct.passwd** %2
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @warnx(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @drop_privileges.user, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
9: ; preds = %0
%10 = load %struct.passwd*, %struct.passwd** %2
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i32 0, i32 1
%12 = load i32, i32* %11
store i32 %12, i32* %3
%13 = load %struct.passwd*, %struct.passwd** %2
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 0
%15 = load i32, i32* %14
store i32 %15, i32* %4
%16 = call i64 @setgroups(i32 1, i32* %4)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %9
%19 = call i32 @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
20: ; preds = %9
%21 = load i32, i32* %4
%22 = call i64 @setgid(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
26: ; preds = %20
%27 = load i32, i32* %3
%28 = call i64 @setuid(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
32: ; preds = %26
store i32 0, i32* %1
br label %33
33: ; preds = %32, %30, %24, %18, %7
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@drop_privileges.user = internal constant [8 x i8] c"_timedc\00"
@.str = [22 x i8] c"getpwnam(\22%s\22) failed\00"
@.str.1 = [10 x i8] c"setgroups\00"
@.str.2 = [7 x i8] c"setgid\00"
@.str.3 = [7 x i8] c"setuid\00"
define i32 @drop_privileges() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @drop_privileges.user, i64 0, i64 0))
%4 = icmp eq %struct.passwd* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @warnx(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @drop_privileges.user, i64 0, i64 0))
br label %27
7: ; preds = %0
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %1
%12 = call i64 @setgroups(i32 1, i32* %1)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %7
%15 = call i32 @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %27
16: ; preds = %7
%17 = load i32, i32* %1
%18 = call i64 @setgid(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %27
22: ; preds = %16
%23 = call i64 @setuid(i32 %9)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %22, %25, %20, %14, %5
%28 = phi i32 [ -1, %5 ], [ -1, %14 ], [ -1, %20 ], [ -1, %25 ], [ 0, %22 ]
ret i32 %28
}