#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_name;
};

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct passwd *getpwnam(char const *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
char *strerror(int /*<<< orphan*/);
int runas(const char *user) {
  struct passwd *passwd = getpwuid(getuid());
  if (strcmp(passwd->pw_name, user) == 0) {
    return 0;
  }
  passwd = getpwnam(user);
  if (passwd == NULL) {
    return -1;
  }
  if (setuid(passwd->pw_uid) != 0) {
    fprintf(stderr, "setuid failed,: %s\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<runas>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11b4 <runas+0x44>
movl   $0x0,-0x4(%rbp)
jmp    122c <runas+0xbc>
mov    -0x10(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d8 <runas+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <runas+0xbc>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1080 <setuid@plt>
cmp    $0x0,%rax
je     1225 <runas+0xb5>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xdec(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <runas+0xbc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runas>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    0x4(%rax),%edi
mov    %rbx,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11dc <runas+0x6c>
mov    %rbx,%rdi
call   1060 <getpwnam@plt>
test   %rax,%rax
je     11d7 <runas+0x67>
mov    (%rax),%edi
call   1080 <setuid@plt>
test   %rax,%rax
je     11dc <runas+0x6c>
mov    0x2e2a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe33(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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