#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 {
  long pw_uid;
  long pw_gid;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
int /*<<< orphan*/ free(char *);
int getpwnam_r(char const *, struct passwd *, char *, size_t, struct passwd **);
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ snip_log_fatal(int, char *, ...);
int /*<<< orphan*/ strerror(int);
int sysconf(int /*<<< orphan*/);
int get_uid_gid_for_username(const char *username, long *uid, long *gid) {
  struct passwd pwd;
  struct passwd *result;
  char *buffer;
  ssize_t buffer_size;
  int s;
  buffer_size = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (buffer_size == -1)
    buffer_size = 16384;
  buffer = malloc((size_t)buffer_size);
  memset(buffer, '\0', buffer_size);
  if (buffer == NULL) {
    snip_log_fatal(66, "Unable to allocate buffer.");
  }
  s = getpwnam_r(username, &pwd, buffer, (size_t)buffer_size, &result);
  if (result == NULL) {
    if (s == 0) {
      if (uid) {
        *uid = -1;
      }
      if (gid) {
        *gid = -1;
      }
      return 0;
    } else {
      snip_log_fatal(66, "Failed to resolve username '%s': (%d) %s", username,
                     strerror(s));
      return 0;
    }
  }
  if (uid) {
    *uid = pwd.pw_uid;
  }
  if (gid) {
    *gid = pwd.pw_gid;
  }
  free(buffer);
  return 1;
}


x86 assembly (clang, -O0):
<get_uid_gid_for_username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    11a6 <get_uid_gid_for_username+0x36>
movl   $0x4000,-0x44(%rbp)
movslq -0x44(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
cmpq   $0x0,-0x40(%rbp)
jne    11df <get_uid_gid_for_username+0x6f>
mov    $0x42,%edi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <snip_log_fatal@plt>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rdx
movslq -0x44(%rbp),%rcx
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%r8
call   1070 <getpwnam_r@plt>
mov    %eax,-0x48(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    127d <get_uid_gid_for_username+0x10d>
cmpl   $0x0,-0x48(%rbp)
jne    1248 <get_uid_gid_for_username+0xd8>
cmpq   $0x0,-0x18(%rbp)
je     1226 <get_uid_gid_for_username+0xb6>
mov    -0x18(%rbp),%rax
movq   $0xffffffffffffffff,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     123c <get_uid_gid_for_username+0xcc>
mov    -0x20(%rbp),%rax
movq   $0xffffffffffffffff,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12b9 <get_uid_gid_for_username+0x149>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x50(%rbp),%rdx
mov    %eax,%ecx
mov    $0x42,%edi
lea    0xdb1(%rip),%rsi
mov    $0x0,%al
call   1050 <snip_log_fatal@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b9 <get_uid_gid_for_username+0x149>
cmpq   $0x0,-0x18(%rbp)
je     1293 <get_uid_gid_for_username+0x123>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     12a9 <get_uid_gid_for_username+0x139>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_uid_gid_for_username>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
cmp    $0xffffffff,%eax
mov    $0x4000,%ecx
cmovne %eax,%ecx
movslq %ecx,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <memset@plt>
test   %r13,%r13
jne    11d1 <get_uid_gid_for_username+0x61>
lea    0xe3b(%rip),%rsi
mov    $0x42,%edi
xor    %eax,%eax
call   1050 <snip_log_fatal@plt>
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %r15,%rdi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1070 <getpwnam_r@plt>
cmpq   $0x0,0x8(%rsp)
je     121b <get_uid_gid_for_username+0xab>
test   %r12,%r12
je     11ff <get_uid_gid_for_username+0x8f>
mov    0x10(%rsp),%rax
mov    %rax,(%r12)
test   %r14,%r14
je     120c <get_uid_gid_for_username+0x9c>
mov    0x18(%rsp),%rax
mov    %rax,(%r14)
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x1,%ebx
jmp    125d <get_uid_gid_for_username+0xed>
test   %eax,%eax
je     1242 <get_uid_gid_for_username+0xd2>
mov    %eax,%edi
call   1090 <strerror@plt>
lea    0xdee(%rip),%rsi
xor    %ebx,%ebx
mov    $0x42,%edi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <snip_log_fatal@plt>
jmp    125d <get_uid_gid_for_username+0xed>
test   %r12,%r12
je     124f <get_uid_gid_for_username+0xdf>
movq   $0xffffffffffffffff,(%r12)
ff
xor    %ebx,%ebx
test   %r14,%r14
je     125d <get_uid_gid_for_username+0xed>
movq   $0xffffffffffffffff,(%r14)
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i64, i64 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [27 x i8] c"Unable to allocate buffer.\00"
@.str.1 = [41 x i8] c"Failed to resolve username '%s': (%d) %s\00"
define i32 @get_uid_gid_for_username(i8* %0, i64* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca %struct.passwd
%9 = alloca %struct.passwd*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i64* %1, i64** %6
store i64* %2, i64** %7
%13 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%14 = call i32 @sysconf(i32 %13)
store i32 %14, i32* %11
%15 = load i32, i32* %11
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 16384, i32* %11
br label %18
18: ; preds = %17, %3
%19 = load i32, i32* %11
%20 = sext i32 %19 to i64
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %10
%22 = load i8*, i8** %10
%23 = load i32, i32* %11
%24 = call i32 @memset(i8* %22, i8 signext 0, i32 %23)
%25 = load i8*, i8** %10
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = call i32 (i32, i8*, ...) @snip_log_fatal(i32 66, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %18
%30 = load i8*, i8** %5
%31 = load i8*, i8** %10
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = call i32 @getpwnam_r(i8* %30, %struct.passwd* %8, i8* %31, i64 %33, %struct.passwd** %9)
store i32 %34, i32* %12
%35 = load %struct.passwd*, %struct.passwd** %9
%36 = icmp eq %struct.passwd* %35, null
br i1 %36, label %37, label %56
37: ; preds = %29
%38 = load i32, i32* %12
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %51
40: ; preds = %37
%41 = load i64*, i64** %6
%42 = icmp ne i64* %41, null
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = load i64*, i64** %6
store i64 -1, i64* %44
br label %45
45: ; preds = %43, %40
%46 = load i64*, i64** %7
%47 = icmp ne i64* %46, null
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = load i64*, i64** %7
store i64 -1, i64* %49
br label %50
50: ; preds = %48, %45
store i32 0, i32* %4
br label %73
51: ; preds = %37
%52 = load i8*, i8** %5
%53 = load i32, i32* %12
%54 = call i32 @strerror(i32 %53)
%55 = call i32 (i32, i8*, ...) @snip_log_fatal(i32 66, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %52, i32 %54)
store i32 0, i32* %4
br label %73
56: ; preds = %29
%57 = load i64*, i64** %6
%58 = icmp ne i64* %57, null
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 0
%61 = load i64, i64* %60
%62 = load i64*, i64** %6
store i64 %61, i64* %62
br label %63
63: ; preds = %59, %56
%64 = load i64*, i64** %7
%65 = icmp ne i64* %64, null
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 1
%68 = load i64, i64* %67
%69 = load i64*, i64** %7
store i64 %68, i64* %69
br label %70
70: ; preds = %66, %63
%71 = load i8*, i8** %10
%72 = call i32 @free(i8* %71)
store i32 1, i32* %4
br label %73
73: ; preds = %70, %51, %50
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
%struct.passwd = type { i64, i64 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [27 x i8] c"Unable to allocate buffer.\00"
@.str.1 = [41 x i8] c"Failed to resolve username '%s': (%d) %s\00"
define i32 @get_uid_gid_for_username(i8* %0, i64* writeonly %1, i64* writeonly %2) {
%4 = alloca %struct.passwd
%5 = alloca %struct.passwd*
%6 = bitcast %struct.passwd* %4 to i8*
%7 = bitcast %struct.passwd** %5 to i8*
%8 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%9 = tail call i32 @sysconf(i32 %8)
%10 = icmp eq i32 %9, -1
%11 = select i1 %10, i32 16384, i32 %9
%12 = sext i32 %11 to i64
%13 = tail call i8* @malloc(i64 %12)
%14 = tail call i32 @memset(i8* %13, i8 signext 0, i32 %11)
%15 = icmp eq i8* %13, null
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = tail call i32 (i32, i8*, ...) @snip_log_fatal(i32 66, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %3
%19 = call i32 @getpwnam_r(i8* %0, %struct.passwd* %4, i8* %13, i64 %12, %struct.passwd** %5)
%20 = load %struct.passwd*, %struct.passwd** %5
%21 = icmp eq %struct.passwd* %20, null
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = icmp eq i32 %19, 0
br i1 %23, label %24, label %30
24: ; preds = %22
%25 = icmp eq i64* %1, null
br i1 %25, label %27, label %26
26: ; preds = %24
store i64 -1, i64* %1
br label %27
27: ; preds = %26, %24
%28 = icmp eq i64* %2, null
br i1 %28, label %45, label %29
29: ; preds = %27
store i64 -1, i64* %2
br label %45
30: ; preds = %22
%31 = call i32 @strerror(i32 %19)
%32 = call i32 (i32, i8*, ...) @snip_log_fatal(i32 66, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %31)
br label %45
33: ; preds = %18
%34 = icmp eq i64* %1, null
br i1 %34, label %38, label %35
35: ; preds = %33
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%37 = load i64, i64* %36
store i64 %37, i64* %1
br label %38
38: ; preds = %35, %33
%39 = icmp eq i64* %2, null
br i1 %39, label %43, label %40
40: ; preds = %38
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 1
%42 = load i64, i64* %41
store i64 %42, i64* %2
br label %43
43: ; preds = %40, %38
%44 = call i32 @free(i8* %13)
br label %45
45: ; preds = %27, %29, %43, %30
%46 = phi i32 [ 0, %30 ], [ 1, %43 ], [ 0, %29 ], [ 0, %27 ]
ret i32 %46
}