#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 */
struct passwd *getpwnam(char const *);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int serverChangeUser(const char *user) {
  struct passwd *rec = getpwnam(user);
  if (rec != NULL) {
    if (setgid(rec->pw_gid) == 0) {
      if (setuid(rec->pw_uid) == 0) {
        return 1;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<serverChangeUser>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1195 <serverChangeUser+0x65>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <setgid@plt>
cmp    $0x0,%rax
jne    1190 <serverChangeUser+0x60>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <setuid@plt>
cmp    $0x0,%rax
jne    118b <serverChangeUser+0x5b>
movl   $0x1,-0x4(%rbp)
jmp    119c <serverChangeUser+0x6c>
jmp    1190 <serverChangeUser+0x60>
jmp    1195 <serverChangeUser+0x65>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverChangeUser>:
push   %rbx
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1157 <serverChangeUser+0x27>
mov    %rax,%rbx
mov    0x4(%rax),%edi
call   1040 <setgid@plt>
test   %rax,%rax
jne    1157 <serverChangeUser+0x27>
mov    (%rbx),%edi
call   1050 <setuid@plt>
test   %rax,%rax
je     115b <serverChangeUser+0x2b>
xor    %eax,%eax
pop    %rbx
ret
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
define i32 @serverChangeUser(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.passwd*
store i8* %0, i8** %3
%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 ne %struct.passwd* %7, null
br i1 %8, label %9, label %24
9: ; preds = %1
%10 = load %struct.passwd*, %struct.passwd** %4
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i32 0, i32 1
%12 = load i32, i32* %11
%13 = call i64 @setgid(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %9
%16 = load %struct.passwd*, %struct.passwd** %4
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i64 @setuid(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 1, i32* %2
br label %25
22: ; preds = %15
br label %23
23: ; preds = %22, %9
br label %24
24: ; preds = %23, %1
store i32 0, i32* %2
br label %25
25: ; preds = %24, %21
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
define i32 @serverChangeUser(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 i64 @setgid(i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %4
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i64 @setuid(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %4, %9, %1
br label %15
15: ; preds = %9, %14
%16 = phi i32 [ 0, %14 ], [ 1, %9 ]
ret i32 %16
}