#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 scalar_t__ uid_t;
struct passwd {
  scalar_t__ pw_uid;
  int /*<<< orphan*/ pw_gid;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
scalar_t__ geteuid();
struct passwd *getpwnam(char const *);
int seteuid(int /*<<< orphan*/);
int /*<<< orphan*/ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setuid(scalar_t__);
int username_set(const char *name) {
  struct passwd *p;
  int result;
  uid_t uid;
  gid_t gid;
  p = getpwnam(name);
  if (!p) {
    return 0;
  }
  uid = p->pw_uid;
  gid = p->pw_gid;
  if (geteuid() == uid)
    return 1;
  result = seteuid(0);
  if (result < 0)
    return 0;
  setuid(uid);
  setgid(gid);
  return 1;
}


x86 assembly (clang, -O0):
<username_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1180 <username_set+0x30>
movl   $0x0,-0x4(%rbp)
jmp    11ea <username_set+0x9a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <geteuid@plt>
cmp    -0x28(%rbp),%rax
jne    11b2 <username_set+0x62>
movl   $0x1,-0x4(%rbp)
jmp    11ea <username_set+0x9a>
xor    %edi,%edi
call   1070 <seteuid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d2 <username_set+0x82>
movl   $0x0,-0x4(%rbp)
jmp    11ea <username_set+0x9a>
mov    -0x28(%rbp),%rdi
call   1060 <setuid@plt>
mov    -0x2c(%rbp),%edi
call   1050 <setgid@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<username_set>:
push   %rbp
push   %r14
push   %rbx
call   1040 <getpwnam@plt>
test   %rax,%rax
je     1195 <username_set+0x45>
mov    (%rax),%rbx
mov    0x8(%rax),%r14d
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <geteuid@plt>
cmp    %rbx,%rax
je     118e <username_set+0x3e>
xor    %edi,%edi
call   1070 <seteuid@plt>
test   %eax,%eax
js     1197 <username_set+0x47>
mov    %rbx,%rdi
call   1060 <setuid@plt>
mov    %r14d,%edi
call   1050 <setgid@plt>
mov    $0x1,%ebp
jmp    1197 <username_set+0x47>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i64, i32 }
define i32 @username_set(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.passwd*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call %struct.passwd* @getpwnam(i8* %8)
store %struct.passwd* %9, %struct.passwd** %4
%10 = load %struct.passwd*, %struct.passwd** %4
%11 = icmp ne %struct.passwd* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i32 0, i32* %2
br label %34
13: ; preds = %1
%14 = load %struct.passwd*, %struct.passwd** %4
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 0
%16 = load i64, i64* %15
store i64 %16, i64* %6
%17 = load %struct.passwd*, %struct.passwd** %4
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i32 0, i32 1
%19 = load i32, i32* %18
store i32 %19, i32* %7
%20 = call i64 (...) @geteuid()
%21 = load i64, i64* %6
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %13
store i32 1, i32* %2
br label %34
24: ; preds = %13
%25 = call i32 @seteuid(i32 0)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 0, i32* %2
br label %34
29: ; preds = %24
%30 = load i64, i64* %6
%31 = call i32 @setuid(i64 %30)
%32 = load i32, i32* %7
%33 = call i32 @setgid(i32 %32)
store i32 1, i32* %2
br label %34
34: ; preds = %29, %28, %23, %12
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.passwd = type { i64, i32 }
define i32 @username_set(i8* nocapture %0) {
%2 = tail call %struct.passwd* @getpwnam(i8* %0)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%6 = load i64, i64* %5
%7 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = tail call i64 (...) @geteuid()
%10 = icmp eq i64 %9, %6
br i1 %10, label %17, label %11
11: ; preds = %4
%12 = tail call i32 @seteuid(i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @setuid(i64 %6)
%16 = tail call i32 @setgid(i32 %8)
br label %17
17: ; preds = %11, %4, %1, %14
%18 = phi i32 [ 1, %14 ], [ 0, %1 ], [ 1, %4 ], [ 0, %11 ]
ret i32 %18
}