#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct passwd *getpwnam(char *);
int setgid(int /*<<< orphan*/);
int setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int set_uidgid(char *username) {
  struct passwd *pwdfile = NULL;
  pwdfile = getpwnam(username);
  if (pwdfile == NULL) {
    fprintf(stderr, "Function getpwnam() failed: %s\n", strerror(errno));
    return -1;
  }
  if (setgid(pwdfile->pw_gid) == -1) {
    fprintf(stderr, "Function setgid() failed: %s\n", strerror(errno));
    return -1;
  }
  if (setuid(pwdfile->pw_uid) == -1) {
    fprintf(stderr, "Function setuid() failed: %s\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_uidgid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b4 <set_uidgid+0x64>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe5d(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <set_uidgid+0x104>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <setgid@plt>
cmp    $0xffffffff,%eax
jne    1201 <set_uidgid+0xb1>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe30(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <set_uidgid+0x104>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <setuid@plt>
cmp    $0xffffffff,%eax
jne    124d <set_uidgid+0xfd>
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe02(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <set_uidgid+0x104>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_uidgid>:
push   %rbp
push   %rbx
push   %rax
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1186 <set_uidgid+0x36>
mov    %rax,%rbx
mov    0x4(%rax),%edi
call   1050 <setgid@plt>
cmp    $0xffffffff,%eax
je     118f <set_uidgid+0x3f>
mov    (%rbx),%edi
call   1060 <setuid@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11bf <set_uidgid+0x6f>
lea    0xeba(%rip),%rbx
jmp    1196 <set_uidgid+0x46>
lea    0xe73(%rip),%rbx
jmp    1196 <set_uidgid+0x46>
lea    0xe8a(%rip),%rbx
mov    0x2e43(%rip),%rax
mov    (%rax),%ebp
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [32 x i8] c"Function getpwnam() failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [30 x i8] c"Function setgid() failed: %s\0A\00"
@.str.2 = [30 x i8] c"Function setuid() failed: %s\0A\00"
define i32 @set_uidgid(i8* nocapture %0) {
%2 = tail call %struct.passwd* @getpwnam(i8* %0)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 1
%6 = load i32, i32* %5
%7 = tail call i32 @setgid(i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @setuid(i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %9, %4, %1
%15 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), %9 ]
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* %15, i8* %18)
br label %20
20: ; preds = %14, %9
%21 = phi i32 [ 0, %9 ], [ -1, %14 ]
ret i32 %21
}