#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 */

/* Variables and functions */
int getuid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ setgid(int);
scalar_t__ setuid(int);
void drop_privileges() {
  if (getuid())
    return;
  setgid(1000);
  setuid(1000);
  if (setuid(0)) {
    puts("Managed to regain root.");
  } else {
    printf("Now running as %d\n", getuid());
  }
}


x86 assembly (clang, -O0):
<drop_privileges>:
push   %rbp
mov    %rsp,%rbp
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%eax
je     1169 <drop_privileges+0x19>
jmp    11b4 <drop_privileges+0x64>
mov    $0x3e8,%edi
call   1060 <setgid@plt>
mov    $0x3e8,%edi
call   1070 <setuid@plt>
xor    %edi,%edi
call   1070 <setuid@plt>
cmp    $0x0,%rax
je     119f <drop_privileges+0x4f>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
jmp    11b4 <drop_privileges+0x64>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%esi
lea    0xe69(%rip),%rdi
call   1050 <printf@plt>
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privileges>:
push   %rax
xor    %eax,%eax
call   1040 <getuid@plt>
test   %eax,%eax
je     115e <drop_privileges+0xe>
pop    %rax
ret
mov    $0x3e8,%edi
call   1060 <setgid@plt>
mov    $0x3e8,%edi
call   1070 <setuid@plt>
xor    %edi,%edi
call   1070 <setuid@plt>
test   %rax,%rax
je     118b <drop_privileges+0x3b>
lea    0xe7b(%rip),%rdi
pop    %rax
jmp    1030 <puts@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
lea    0xe7f(%rip),%rdi
mov    %eax,%esi
pop    %rax
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Managed to regain root.\00"
@.str.1 = [19 x i8] c"Now running as %d\0A\00"
define void @drop_privileges() {
%1 = call i32 (...) @getuid()
%2 = icmp ne i32 %1, 0
br i1 %2, label %3, label %4
3: ; preds = %0
br label %14
4: ; preds = %0
%5 = call i32 @setgid(i32 1000)
%6 = call i64 @setuid(i32 1000)
%7 = call i64 @setuid(i32 0)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %14
11: ; preds = %4
%12 = call i32 (...) @getuid()
%13 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %3, %11, %9
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"Managed to regain root.\00"
@.str.1 = [19 x i8] c"Now running as %d\0A\00"
define void @drop_privileges() {
%1 = tail call i32 (...) @getuid()
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %13
3: ; preds = %0
%4 = tail call i32 @setgid(i32 1000)
%5 = tail call i64 @setuid(i32 1000)
%6 = tail call i64 @setuid(i32 0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %13
10: ; preds = %3
%11 = tail call i32 (...) @getuid()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %0, %10, %8
ret void
}