#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 gid_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int getegid();
int geteuid();
int getgid();
int getuid();
int /*<<< orphan*/ log_error(char *, int, int /*<<< orphan*/);
scalar_t__ setgroups(int, int *);
scalar_t__ setregid(int, int);
scalar_t__ setreuid(int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int set_gid_uid(int gid, int uid) {
  if (getgid() != gid || getegid() != gid) {
    gid_t group = gid;
    if (setgroups(1, &group) != 0) {
      log_error("Error: Could not set groups list to [%d] : %s", gid,
                strerror(errno));
      return -1;
    }
    if (setregid(gid, gid) != 0) {
      log_error("Error: Could not set real and effective group ID to %d : %s",
                gid, strerror(errno));
      return -1;
    }
  }
  if (getuid() != uid || geteuid() != uid) {
    if (setreuid(uid, uid) != 0) {
      log_error("Error: Could not set real and effective user ID to %d : %s",
                uid, strerror(errno));
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_gid_uid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1070 <getgid@plt>
cmp    -0x8(%rbp),%eax
jne    11be <set_gid_uid+0x2e>
mov    $0x0,%al
call   1080 <getegid@plt>
cmp    -0x8(%rbp),%eax
je     1258 <set_gid_uid+0xc8>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
lea    -0x10(%rbp),%rsi
call   1060 <setgroups@plt>
cmp    $0x0,%rax
je     120d <set_gid_uid+0x7d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe04(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <set_gid_uid+0x13a>
mov    -0x8(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10a0 <setregid@plt>
cmp    $0x0,%rax
je     1253 <set_gid_uid+0xc3>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xdec(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <set_gid_uid+0x13a>
jmp    1258 <set_gid_uid+0xc8>
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    -0xc(%rbp),%eax
jne    1278 <set_gid_uid+0xe8>
mov    $0x0,%al
call   1050 <geteuid@plt>
cmp    -0xc(%rbp),%eax
je     12c3 <set_gid_uid+0x133>
mov    -0xc(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1090 <setreuid@plt>
cmp    $0x0,%rax
je     12be <set_gid_uid+0x12e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xdbd(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <set_gid_uid+0x13a>
jmp    12c3 <set_gid_uid+0x133>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gid_uid>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <getgid@plt>
cmp    %ebp,%eax
jne    11ad <set_gid_uid+0x1d>
xor    %eax,%eax
call   1080 <getegid@plt>
cmp    %ebp,%eax
je     11f8 <set_gid_uid+0x68>
mov    %ebp,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x1,%edi
call   1060 <setgroups@plt>
test   %rax,%rax
je     11ce <set_gid_uid+0x3e>
lea    0xe34(%rip),%rbx
jmp    11e3 <set_gid_uid+0x53>
mov    %ebp,%edi
mov    %ebp,%esi
call   10a0 <setregid@plt>
test   %rax,%rax
je     11f8 <set_gid_uid+0x68>
lea    0xe4b(%rip),%rbx
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
jmp    123a <set_gid_uid+0xaa>
xor    %eax,%eax
call   1040 <getuid@plt>
cmp    %ebx,%eax
jne    1210 <set_gid_uid+0x80>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <geteuid@plt>
cmp    %ebx,%eax
je     1246 <set_gid_uid+0xb6>
mov    %ebx,%edi
mov    %ebx,%esi
call   1090 <setreuid@plt>
mov    $0x0,%ebp
test   %rax,%rax
je     1246 <set_gid_uid+0xb6>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe32(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <log_error@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"Error: Could not set groups list to [%d] : %s\00"
@errno = global i32 0
@.str.1 = [60 x i8] c"Error: Could not set real and effective group ID to %d : %s\00"
@.str.2 = [59 x i8] c"Error: Could not set real and effective user ID to %d : %s\00"
define i32 @set_gid_uid(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = call i32 (...) @getgid()
%8 = load i32, i32* %4
%9 = icmp ne i32 %7, %8
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = call i32 (...) @getegid()
%12 = load i32, i32* %4
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %34
14: ; preds = %10, %2
%15 = load i32, i32* %4
store i32 %15, i32* %6
%16 = call i64 @setgroups(i32 1, i32* %6)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* %4
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 @log_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %21)
store i32 -1, i32* %3
br label %54
23: ; preds = %14
%24 = load i32, i32* %4
%25 = load i32, i32* %4
%26 = call i64 @setregid(i32 %24, i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i32, i32* %4
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = call i32 @log_error(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %31)
store i32 -1, i32* %3
br label %54
33: ; preds = %23
br label %34
34: ; preds = %33, %10
%35 = call i32 (...) @getuid()
%36 = load i32, i32* %5
%37 = icmp ne i32 %35, %36
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = call i32 (...) @geteuid()
%40 = load i32, i32* %5
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %53
42: ; preds = %38, %34
%43 = load i32, i32* %5
%44 = load i32, i32* %5
%45 = call i64 @setreuid(i32 %43, i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load i32, i32* %5
%49 = load i32, i32* @errno
%50 = call i32 @strerror(i32 %49)
%51 = call i32 @log_error(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %50)
store i32 -1, i32* %3
br label %54
52: ; preds = %42
br label %53
53: ; preds = %52, %38
store i32 0, i32* %3
br label %54
54: ; preds = %53, %47, %28, %18
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [46 x i8] c"Error: Could not set groups list to [%d] : %s\00"
@errno = global i32 0
@.str.1 = [60 x i8] c"Error: Could not set real and effective group ID to %d : %s\00"
@.str.2 = [59 x i8] c"Error: Could not set real and effective user ID to %d : %s\00"
define i32 @set_gid_uid(i32 %0, i32 %1) {
%3 = alloca i32
%4 = tail call i32 (...) @getgid()
%5 = icmp eq i32 %4, %0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 (...) @getegid()
%8 = icmp eq i32 %7, %0
br i1 %8, label %22, label %9
9: ; preds = %6, %2
%10 = bitcast i32* %3 to i8*
store i32 %0, i32* %3
%11 = call i64 @setgroups(i32 1, i32* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = call i64 @setregid(i32 %0, i32 %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %13, %9
%17 = phi i8* [ getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), %13 ]
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @log_error(i8* %17, i32 %0, i32 %19)
br label %35
21: ; preds = %13
br label %22
22: ; preds = %21, %6
%23 = call i32 (...) @getuid()
%24 = icmp eq i32 %23, %1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 (...) @geteuid()
%27 = icmp eq i32 %26, %1
br i1 %27, label %35, label %28
28: ; preds = %25, %22
%29 = call i64 @setreuid(i32 %1, i32 %1)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 @log_error(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %33)
br label %35
35: ; preds = %16, %25, %28, %31
%36 = phi i32 [ -1, %31 ], [ 0, %28 ], [ 0, %25 ], [ -1, %16 ]
ret i32 %36
}