#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 {
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
};

/* Variables and functions */
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ getpwnam_r(char const *, struct passwd *, char *, size_t,
                      struct passwd **);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
size_t sysconf(int /*<<< orphan*/);
int switch_to_user(const char *user) {
  struct passwd pwbuf;
  struct passwd *pwbufp;
  char *buf;
  size_t buflen;
  buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (buflen < 0) {
    perror("sysconf");
    return -1;
  }
  buf = (char *)malloc(buflen);
  if (!buf) {
    fprintf(stderr, "Couldn't allocate memory.\n");
    return -1;
  }
  if ((getpwnam_r(user, &pwbuf, buf, buflen, &pwbufp) != 0) || (!pwbufp)) {
    free(buf);
    fprintf(stderr, "Couldn't get password file entry for user: %s.\n", user);
    return -1;
  }
  if (setgid(pwbufp->pw_gid) < 0) {
    perror("setgid");
    free(buf);
    return -1;
  }
  if (setuid(pwbufp->pw_uid) < 0) {
    perror("setuid");
    free(buf);
    return -1;
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<switch_to_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    11c1 <switch_to_user+0x41>
lea    0xe50(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <switch_to_user+0x157>
mov    -0x30(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fc <switch_to_user+0x7c>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <switch_to_user+0x157>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%r8
call   1060 <getpwnam_r@plt>
cmp    $0x0,%rax
jne    122a <switch_to_user+0xaa>
cmpq   $0x0,-0x20(%rbp)
jne    125a <switch_to_user+0xda>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <switch_to_user+0x157>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
jge    1291 <switch_to_user+0x111>
lea    0xddc(%rip),%rdi
call   1080 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <switch_to_user+0x157>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   10a0 <setuid@plt>
cmp    $0x0,%rax
jge    12c7 <switch_to_user+0x147>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <switch_to_user+0x157>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switch_to_user>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11fa <switch_to_user+0x7a>
mov    %rax,%r15
lea    0x8(%rsp),%rsi
mov    %rsp,%r8
mov    %r14,%rdi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1060 <getpwnam_r@plt>
test   %rax,%rax
jne    1213 <switch_to_user+0x93>
mov    (%rsp),%rax
test   %rax,%rax
je     1213 <switch_to_user+0x93>
mov    0x4(%rax),%edi
call   1070 <setgid@plt>
test   %rax,%rax
js     1244 <switch_to_user+0xc4>
mov    (%rsp),%rax
mov    (%rax),%edi
call   10a0 <setuid@plt>
test   %rax,%rax
js     124d <switch_to_user+0xcd>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    123a <switch_to_user+0xba>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1235 <switch_to_user+0xb5>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe00(%rip),%rdi
jmp    1254 <switch_to_user+0xd4>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1235 <switch_to_user+0xb5>

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [8 x i8] c"sysconf\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"Couldn't allocate memory.\0A\00"
@.str.2 = [48 x i8] c"Couldn't get password file entry for user: %s.\0A\00"
@.str.3 = [7 x i8] c"setgid\00"
@.str.4 = [7 x i8] c"setuid\00"
define i32 @switch_to_user(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.passwd
%5 = alloca %struct.passwd*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%9 = call i64 @sysconf(i32 %8)
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = icmp ult i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %61
14: ; preds = %1
%15 = load i64, i64* %7
%16 = call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %61
23: ; preds = %14
%24 = load i8*, i8** %3
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = call i64 @getpwnam_r(i8* %24, %struct.passwd* %4, i8* %25, i64 %26, %struct.passwd** %5)
%28 = icmp ne i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %23
%30 = load %struct.passwd*, %struct.passwd** %5
%31 = icmp ne %struct.passwd* %30, null
br i1 %31, label %38, label %32
32: ; preds = %29, %23
%33 = load i8*, i8** %6
%34 = call i32 @free(i8* %33)
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %3
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %36)
store i32 -1, i32* %2
br label %61
38: ; preds = %29
%39 = load %struct.passwd*, %struct.passwd** %5
%40 = getelementptr inbounds %struct.passwd, %struct.passwd* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = call i64 @setgid(i32 %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%46 = load i8*, i8** %6
%47 = call i32 @free(i8* %46)
store i32 -1, i32* %2
br label %61
48: ; preds = %38
%49 = load %struct.passwd*, %struct.passwd** %5
%50 = getelementptr inbounds %struct.passwd, %struct.passwd* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i64 @setuid(i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%56 = load i8*, i8** %6
%57 = call i32 @free(i8* %56)
store i32 -1, i32* %2
br label %61
58: ; preds = %48
%59 = load i8*, i8** %6
%60 = call i32 @free(i8* %59)
store i32 0, i32* %2
br label %61
61: ; preds = %58, %54, %44, %32, %20, %12
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Couldn't allocate memory.\0A\00"
@.str.2 = [48 x i8] c"Couldn't get password file entry for user: %s.\0A\00"
@.str.3 = [7 x i8] c"setgid\00"
@.str.4 = [7 x i8] c"setuid\00"
define i32 @switch_to_user(i8* %0) {
%2 = alloca %struct.passwd
%3 = alloca %struct.passwd*
%4 = bitcast %struct.passwd* %2 to i8*
%5 = bitcast %struct.passwd** %3 to i8*
%6 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%7 = tail call i64 @sysconf(i32 %6)
%8 = tail call i64 @malloc(i64 %7)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %43
14: ; preds = %1
%15 = call i64 @getpwnam_r(i8* %0, %struct.passwd* %2, i8* %9, i64 %7, %struct.passwd** %3)
%16 = icmp eq i64 %15, 0
%17 = load %struct.passwd*, %struct.passwd** %3
%18 = icmp ne %struct.passwd* %17, null
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %24, label %20
20: ; preds = %14
%21 = call i32 @free(i8* %9)
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %43
24: ; preds = %14
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i64 0, i32 1
%26 = load i32, i32* %25
%27 = call i64 @setgid(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @free(i8* %9)
br label %43
32: ; preds = %24
%33 = load %struct.passwd*, %struct.passwd** %3
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i64 @setuid(i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @free(i8* %9)
br label %43
41: ; preds = %32
%42 = call i32 @free(i8* %9)
br label %43
43: ; preds = %41, %38, %29, %20, %11
%44 = phi i32 [ -1, %29 ], [ -1, %38 ], [ 0, %41 ], [ -1, %20 ], [ -1, %11 ]
ret i32 %44
}