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

/* Variables and functions */
int /*<<< orphan*/ errno;
int getpwnam_r(char const *, struct passwd *, char *, size_t, struct passwd **);
int /*<<< orphan*/ log_debug(char *, char const *, ...);
int /*<<< orphan*/ log_error(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ log_warn(char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int resolve_user_uidgid(const char *username, uid_t *uid, gid_t *gid) {
  struct passwd pw;
  struct passwd *result;
  char buf[10000];
  size_t bufsize = 10000;
  int rc;
  if (username == NULL) {
    log_warn("resolve_user_uidgid: username is NULL");
    return -1;
  }
  log_debug("resolve_user_uidgid for %s", username);
  rc = getpwnam_r(username, &pw, buf, bufsize, &result);
  if (rc == 0 && result != NULL) {
    *uid = pw.pw_uid;
    *gid = pw.pw_gid;
    log_debug("resolve_user_uidgid: uid=%d, gid=%d", pw.pw_uid, pw.pw_gid);
    return 0;
  } else {
    log_error("failed to resolve POSIX uid/gid for %s: %s", username,
              strerror(errno));
    return -2;
  }
}


x86 assembly (clang, -O0):
<resolve_user_uidgid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2760,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x2710,-0x2748(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1195 <resolve_user_uidgid+0x45>
lea    0xe7c(%rip),%rdi
call   1060 <log_warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <resolve_user_uidgid+0xfc>
mov    -0x10(%rbp),%rsi
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1040 <log_debug@plt>
mov    -0x10(%rbp),%rdi
lea    -0x2740(%rbp),%rdx
mov    -0x2748(%rbp),%rcx
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%r8
call   1050 <getpwnam_r@plt>
mov    %eax,-0x274c(%rbp)
cmpl   $0x0,-0x274c(%rbp)
jne    1217 <resolve_user_uidgid+0xc7>
cmpq   $0x0,-0x30(%rbp)
je     1217 <resolve_user_uidgid+0xc7>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movslq -0x24(%rbp),%rsi
mov    -0x28(%rbp),%edx
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <log_debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    124c <resolve_user_uidgid+0xfc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x2758(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2758(%rbp),%rsi
mov    %eax,%edx
lea    0xe25(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2760,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_user_uidgid>:
push   %r15
push   %r14
push   %rbx
sub    $0x2720,%rsp
test   %rdi,%rdi
je     11c1 <resolve_user_uidgid+0x71>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xeb5(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <log_debug@plt>
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%r8
mov    $0x2710,%ecx
mov    %rbx,%rdi
call   1050 <getpwnam_r@plt>
test   %eax,%eax
jne    11d4 <resolve_user_uidgid+0x84>
cmpq   $0x0,0x8(%rsp)
je     11d4 <resolve_user_uidgid+0x84>
movslq 0x4(%rsp),%rsi
mov    %esi,(%r15)
mov    (%rsp),%edx
mov    %edx,(%r14)
lea    0xe8b(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <log_debug@plt>
jmp    11f8 <resolve_user_uidgid+0xa8>
lea    0xe38(%rip),%rdi
call   1060 <log_warn@plt>
mov    $0xffffffff,%ebx
jmp    11f8 <resolve_user_uidgid+0xa8>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe7c(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <log_error@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x2720,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [38 x i8] c"resolve_user_uidgid: username is NULL\00"
@.str.1 = [27 x i8] c"resolve_user_uidgid for %s\00"
@.str.2 = [36 x i8] c"resolve_user_uidgid: uid=%d, gid=%d\00"
@.str.3 = [43 x i8] c"failed to resolve POSIX uid/gid for %s: %s\00"
@errno = global i32 0
define i32 @resolve_user_uidgid(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.passwd
%9 = alloca %struct.passwd*
%10 = alloca [10000 x i8]
%11 = alloca i64
%12 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i64 10000, i64* %11
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @log_warn(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %5
%21 = getelementptr inbounds [10000 x i8], [10000 x i8]* %10, i64 0, i64 0
%22 = load i64, i64* %11
%23 = call i32 @getpwnam_r(i8* %20, %struct.passwd* %8, i8* %21, i64 %22, %struct.passwd** %9)
store i32 %23, i32* %12
%24 = load i32, i32* %12
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %43
26: ; preds = %17
%27 = load %struct.passwd*, %struct.passwd** %9
%28 = icmp ne %struct.passwd* %27, null
br i1 %28, label %29, label %43
29: ; preds = %26
%30 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load i32*, i32** %6
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32*, i32** %7
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 1
%37 = load i32, i32* %36
%38 = sext i32 %37 to i64
%39 = inttoptr i64 %38 to i8*
%40 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %39, i32 %41)
store i32 0, i32* %4
br label %48
43: ; preds = %26, %17
%44 = load i8*, i8** %5
%45 = load i32, i32* @errno
%46 = call i32 @strerror(i32 %45)
%47 = call i32 @log_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %46)
store i32 -2, i32* %4
br label %48
48: ; preds = %43, %29, %15
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [38 x i8] c"resolve_user_uidgid: username is NULL\00"
@.str.1 = [27 x i8] c"resolve_user_uidgid for %s\00"
@.str.2 = [36 x i8] c"resolve_user_uidgid: uid=%d, gid=%d\00"
@.str.3 = [43 x i8] c"failed to resolve POSIX uid/gid for %s: %s\00"
@errno = global i32 0
define i32 @resolve_user_uidgid(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.passwd
%5 = alloca %struct.passwd*
%6 = alloca [10000 x i8]
%7 = bitcast %struct.passwd* %4 to i8*
%8 = bitcast %struct.passwd** %5 to i8*
%9 = getelementptr inbounds [10000 x i8], [10000 x i8]* %6, i64 0, i64 0
%10 = icmp eq i8* %0, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @log_warn(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %32
13: ; preds = %3
%14 = tail call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%15 = call i32 @getpwnam_r(i8* %0, %struct.passwd* %4, i8* %9, i64 10000, %struct.passwd** %5)
%16 = icmp eq i32 %15, 0
%17 = load %struct.passwd*, %struct.passwd** %5
%18 = icmp ne %struct.passwd* %17, null
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %20, label %28
20: ; preds = %13
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 1
%22 = load i32, i32* %21
store i32 %22, i32* %1
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %2
%25 = sext i32 %22 to i64
%26 = inttoptr i64 %25 to i8*
%27 = call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %26, i32 %24)
br label %32
28: ; preds = %13
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @log_error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %30)
br label %32
32: ; preds = %28, %20, %11
%33 = phi i32 [ -1, %11 ], [ 0, %20 ], [ -2, %28 ]
ret i32 %33
}