#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 uid_t;
struct passwd {
  int pw_uid;
};

/* Variables and functions */
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
scalar_t__ getpwnam_r(char const *, struct passwd *, char *, long const,
                      struct passwd **);
long sysconf(int /*<<< orphan*/);
uid_t name_to_uid(char const *name) {
  if (!name) {
    return -1;
  }
  long const buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (buflen == -1) {
    return -1;
  }
  char buf[buflen];
  struct passwd pwbuf, *pwbufp;
  if (0 != getpwnam_r(name, &pwbuf, buf, buflen, &pwbufp) || !pwbufp) {
    return -1;
  }
  return pwbufp->pw_uid;
}


x86 assembly (clang, -O0):
<name_to_uid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1143 <name_to_uid+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <name_to_uid+0xc7>
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    116c <name_to_uid+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <name_to_uid+0xc7>
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%r8
call   1030 <getpwnam_r@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11bd <name_to_uid+0x9d>
cmpq   $0x0,-0x38(%rbp)
jne    11d0 <name_to_uid+0xb0>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    11e0 <name_to_uid+0xc0>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<name_to_uid>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     1190 <name_to_uid+0x70>
mov    %rdi,%r14
mov    0x2ea7(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
cmp    $0xffffffffffffffff,%rax
je     1190 <name_to_uid+0x70>
mov    %rsp,%r15
mov    %rsp,%rdx
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rdx
mov    %rdx,%rsp
lea    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    %r14,%rdi
mov    %rax,%rcx
call   1030 <getpwnam_r@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    118d <name_to_uid+0x6d>
mov    -0x20(%rbp),%rax
test   %rax,%rax
je     118d <name_to_uid+0x6d>
mov    (%rax),%ebx
mov    %r15,%rsp
mov    %ebx,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
define i32 @name_to_uid(i8* %0) {
%2 = alloca %struct.passwd
%3 = alloca %struct.passwd*
%4 = icmp eq i8* %0, null
br i1 %4, label %24, label %5
5: ; preds = %1
%6 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%7 = tail call i64 @sysconf(i32 %6)
%8 = icmp eq i64 %7, -1
br i1 %8, label %24, label %9
9: ; preds = %5
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %7
%12 = bitcast %struct.passwd* %2 to i8*
%13 = bitcast %struct.passwd** %3 to i8*
%14 = call i64 @getpwnam_r(i8* %0, %struct.passwd* %2, i8* %11, i64 %7, %struct.passwd** %3)
%15 = icmp eq i64 %14, 0
%16 = load %struct.passwd*, %struct.passwd** %3
%17 = icmp ne %struct.passwd* %16, null
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i64 0, i32 0
%21 = load i32, i32* %20
br label %22
22: ; preds = %9, %19
%23 = phi i32 [ %21, %19 ], [ -1, %9 ]
br label %24
24: ; preds = %22, %5, %1
%25 = phi i32 [ -1, %1 ], [ %23, %22 ], [ -1, %5 ]
ret i32 %25
}