#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 uid_t;
typedef int gid_t;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, char *);
int getegid();
int geteuid();
int setgid(int);
int setuid(int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int switch_to_user() {
  int ret;
  uid_t euid = geteuid();
  gid_t egid = getegid();
  ret = setuid(euid);
  if (ret != 0) {
    char *errorno_msg = strerror(errno);
    fprintf(stderr, "Failed to set UID to %i, error %i %s\n", euid, errno,
            errorno_msg);
  } else {
    ret = setgid(egid);
    if (ret != 0) {
      char *errorno_msg = strerror(errno);
      fprintf(stderr, "Failed to set GID to %i, error %i %s\n", egid, errno,
              errorno_msg);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<switch_to_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getegid@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <setuid@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11cd <switch_to_user+0x6d>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8
lea    0xe3d(%rip),%rsi
call   1040 <fprintf@plt>
jmp    121e <switch_to_user+0xbe>
mov    -0xc(%rbp),%edi
call   1060 <setgid@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1219 <switch_to_user+0xb9>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,-0x20(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    0x2dd1(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8
lea    0xe12(%rip),%rsi
call   1040 <fprintf@plt>
jmp    121e <switch_to_user+0xbe>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switch_to_user>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <geteuid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1050 <getegid@plt>
mov    %eax,%r14d
mov    %r15d,%edi
call   1070 <setuid@plt>
test   %eax,%eax
je     11b0 <switch_to_user+0x50>
mov    %eax,%ebp
mov    0x2e46(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
mov    0x2e40(%rip),%rcx
mov    (%rcx),%edi
mov    (%rbx),%ecx
lea    0xe55(%rip),%rsi
mov    %r15d,%edx
jmp    11e1 <switch_to_user+0x81>
mov    %r14d,%edi
call   1060 <setgid@plt>
test   %eax,%eax
je     11eb <switch_to_user+0x8b>
mov    %eax,%ebp
mov    0x2e13(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%edi
mov    (%rbx),%ecx
lea    0xe48(%rip),%rsi
mov    %r14d,%edx
mov    %rax,%r8
call   1040 <fprintf@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"Failed to set UID to %i, error %i %s\0A\00"
@.str.1 = [38 x i8] c"Failed to set GID to %i, error %i %s\0A\00"
define i32 @switch_to_user() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = call i32 (...) @geteuid()
store i32 %6, i32* %2
%7 = call i32 (...) @getegid()
store i32 %7, i32* %3
%8 = load i32, i32* %2
%9 = call i32 @setuid(i32 %8)
store i32 %9, i32* %1
%10 = load i32, i32* %1
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %0
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
store i8* %14, i8** %4
%15 = load i32, i32* @stderr
%16 = load i32, i32* %2
%17 = load i32, i32* @errno
%18 = load i8*, i8** %4
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17, i8* %18)
br label %34
20: ; preds = %0
%21 = load i32, i32* %3
%22 = call i32 @setgid(i32 %21)
store i32 %22, i32* %1
%23 = load i32, i32* %1
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
store i8* %27, i8** %5
%28 = load i32, i32* @stderr
%29 = load i32, i32* %3
%30 = load i32, i32* @errno
%31 = load i8*, i8** %5
%32 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30, i8* %31)
br label %33
33: ; preds = %25, %20
br label %34
34: ; preds = %33, %12
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@errno = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"Failed to set UID to %i, error %i %s\0A\00"
@.str.1 = [38 x i8] c"Failed to set GID to %i, error %i %s\0A\00"
define i32 @switch_to_user() {
%1 = tail call i32 (...) @geteuid()
%2 = tail call i32 (...) @getegid()
%3 = tail call i32 @setuid(i32 %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %11, label %5
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %9, i8* %7)
br label %20
11: ; preds = %0
%12 = tail call i32 @setgid(i32 %2)
%13 = icmp eq i32 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %18, i8* %16)
br label %20
20: ; preds = %11, %14, %5
%21 = phi i32 [ %3, %5 ], [ %12, %14 ], [ 0, %11 ]
ret i32 %21
}