#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 udev {
  int dummy;
};
struct passwd {
  int /*<<< orphan*/ pw_uid;
};

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ ESRCH;
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
scalar_t__ errno;
scalar_t__ getpwnam_r(char const *, struct passwd *, char *, size_t,
                      struct passwd **);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strtoul(char const *, char **, int);
size_t sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char const *);
uid_t util_lookup_user(struct udev *udev, const char *user) {
  char *endptr;
  size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
  char buf[buflen];
  struct passwd pwbuf;
  struct passwd *pw;
  uid_t uid;
  if (strcmp(user, "root") == 0)
    return 0;
  uid = strtoul(user, &endptr, 10);
  if (endptr[0] == '\0')
    return uid;
  errno = getpwnam_r(user, &pwbuf, buf, buflen, &pw);
  if (pw != NULL)
    return pw->pw_uid;
  if (errno == 0 || errno == ENOENT || errno == ESRCH)
    udev_log_null(udev, "specified user '%s' unknown\n", user);
  else
    udev_log_null(udev, "error resolving user '%s': %m\n", user);
  return 0;
}


x86 assembly (clang, -O0):
<util_lookup_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11c6 <util_lookup_user+0x76>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12c3 <util_lookup_user+0x173>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11fd <util_lookup_user+0xad>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12c3 <util_lookup_user+0x173>
mov    -0x58(%rbp),%rdx
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rcx
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%r8
call   1050 <getpwnam_r@plt>
mov    %rax,%rcx
mov    0x2db8(%rip),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x48(%rbp)
je     1243 <util_lookup_user+0xf3>
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12c3 <util_lookup_user+0x173>
mov    0x2d8e(%rip),%rax
cmpq   $0x0,(%rax)
je     1288 <util_lookup_user+0x138>
mov    0x2d7d(%rip),%rax
mov    (%rax),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
je     1288 <util_lookup_user+0x138>
mov    0x2d63(%rip),%rax
mov    (%rax),%rax
mov    0x2d41(%rip),%rcx
cmp    (%rcx),%rax
jne    12a1 <util_lookup_user+0x151>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd6e(%rip),%rsi
call   1030 <udev_log_null@plt>
jmp    12b5 <util_lookup_user+0x165>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd72(%rip),%rsi
call   1030 <udev_log_null@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_lookup_user>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r12
mov    %rdi,-0x30(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,%r13
mov    %rsp,%r14
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe6c(%rip),%rsi
mov    %r12,%rdi
call   1040 <strcmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     122e <util_lookup_user+0xde>
lea    -0x48(%rbp),%rsi
mov    %r12,%rdi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    -0x48(%rbp),%rcx
cmpb   $0x0,(%rcx)
je     11f0 <util_lookup_user+0xa0>
lea    -0x40(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    %r12,%rdi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1050 <getpwnam_r@plt>
mov    0x2df9(%rip),%rcx
mov    %rax,(%rcx)
mov    -0x38(%rbp),%rcx
test   %rcx,%rcx
je     11f5 <util_lookup_user+0xa5>
mov    (%rcx),%r15d
jmp    122e <util_lookup_user+0xde>
mov    %eax,%r15d
jmp    122e <util_lookup_user+0xde>
test   %rax,%rax
je     121b <util_lookup_user+0xcb>
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
je     121b <util_lookup_user+0xcb>
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%rax
je     121b <util_lookup_user+0xcb>
lea    0xe09(%rip),%rsi
jmp    1222 <util_lookup_user+0xd2>
lea    0xde3(%rip),%rsi
mov    -0x30(%rbp),%rdi
mov    %r12,%rdx
call   1030 <udev_log_null@plt>
mov    %r14,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [5 x i8] c"root\00"
@errno = global i64 0
@ENOENT = global i64 0
@ESRCH = global i64 0
@.str.1 = [29 x i8] c"specified user '%s' unknown\0A\00"
@.str.2 = [31 x i8] c"error resolving user '%s': %m\0A\00"
define i32 @util_lookup_user(%struct.udev* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.udev*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.passwd
%11 = alloca %struct.passwd*
%12 = alloca i32
%13 = alloca i32
store %struct.udev* %0, %struct.udev** %4
store i8* %1, i8** %5
%14 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%15 = call i64 @sysconf(i32 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %8
%18 = alloca i8, i64 %16
store i64 %16, i64* %9
%19 = load i8*, i8** %5
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %13
br label %63
23: ; preds = %2
%24 = load i8*, i8** %5
%25 = call i32 @strtoul(i8* %24, i8** %6, i32 10)
store i32 %25, i32* %12
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i32, i32* %12
store i32 %32, i32* %3
store i32 1, i32* %13
br label %63
33: ; preds = %23
%34 = load i8*, i8** %5
%35 = load i64, i64* %7
%36 = call i64 @getpwnam_r(i8* %34, %struct.passwd* %10, i8* %18, i64 %35, %struct.passwd** %11)
store i64 %36, i64* @errno
%37 = load %struct.passwd*, %struct.passwd** %11
%38 = icmp ne %struct.passwd* %37, null
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = load %struct.passwd*, %struct.passwd** %11
%41 = getelementptr inbounds %struct.passwd, %struct.passwd* %40, i32 0, i32 0
%42 = load i32, i32* %41
store i32 %42, i32* %3
store i32 1, i32* %13
br label %63
43: ; preds = %33
%44 = load i64, i64* @errno
%45 = icmp eq i64 %44, 0
br i1 %45, label %54, label %46
46: ; preds = %43
%47 = load i64, i64* @errno
%48 = load i64, i64* @ENOENT
%49 = icmp eq i64 %47, %48
br i1 %49, label %54, label %50
50: ; preds = %46
%51 = load i64, i64* @errno
%52 = load i64, i64* @ESRCH
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %58
54: ; preds = %50, %46, %43
%55 = load %struct.udev*, %struct.udev** %4
%56 = load i8*, i8** %5
%57 = call i32 @udev_log_null(%struct.udev* %55, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %56)
br label %62
58: ; preds = %50
%59 = load %struct.udev*, %struct.udev** %4
%60 = load i8*, i8** %5
%61 = call i32 @udev_log_null(%struct.udev* %59, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %58, %54
store i32 0, i32* %3
store i32 1, i32* %13
br label %63
63: ; preds = %62, %39, %31, %22
%64 = load i8*, i8** %8
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.udev = type { i32 }
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [5 x i8] c"root\00"
@errno = global i64 0
@ENOENT = global i64 0
@ESRCH = global i64 0
@.str.1 = [29 x i8] c"specified user '%s' unknown\0A\00"
@.str.2 = [31 x i8] c"error resolving user '%s': %m\0A\00"
define i32 @util_lookup_user(%struct.udev* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.passwd
%5 = alloca %struct.passwd*
%6 = bitcast i8** %3 to i8*
%7 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%8 = tail call i64 @sysconf(i32 %7)
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = bitcast %struct.passwd* %4 to i8*
%12 = bitcast %struct.passwd** %5 to i8*
%13 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %39, label %15
15: ; preds = %2
%16 = call i32 @strtoul(i8* %1, i8** %3, i32 10)
%17 = load i8*, i8** %3
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %39, label %20
20: ; preds = %15
%21 = call i64 @getpwnam_r(i8* %1, %struct.passwd* %4, i8* %10, i64 %8, %struct.passwd** %5)
store i64 %21, i64* @errno
%22 = load %struct.passwd*, %struct.passwd** %5
%23 = icmp eq %struct.passwd* %22, null
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %22, i64 0, i32 0
%26 = load i32, i32* %25
br label %39
27: ; preds = %20
%28 = icmp eq i64 %21, 0
%29 = load i64, i64* @ENOENT
%30 = icmp eq i64 %21, %29
%31 = select i1 %28, i1 true, i1 %30
%32 = load i64, i64* @ESRCH
%33 = icmp eq i64 %21, %32
%34 = select i1 %31, i1 true, i1 %33
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %39
37: ; preds = %27
%38 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %39
39: ; preds = %35, %37, %15, %2, %24
%40 = phi i32 [ %26, %24 ], [ 0, %2 ], [ %16, %15 ], [ 0, %37 ], [ 0, %35 ]
ret i32 %40
}