#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 *, int,
                      struct passwd **);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strtoul(char const *, char **, int);
int 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;
  int 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    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
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    0xe5e(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11c4 <util_lookup_user+0x74>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12c0 <util_lookup_user+0x170>
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    11fb <util_lookup_user+0xab>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12c0 <util_lookup_user+0x170>
mov    -0x58(%rbp),%rdx
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%ecx
lea    -0x40(%rbp),%rsi
lea    -0x48(%rbp),%r8
call   1050 <getpwnam_r@plt>
mov    %rax,%rcx
mov    0x2dbb(%rip),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x48(%rbp)
je     1240 <util_lookup_user+0xf0>
mov    -0x48(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    12c0 <util_lookup_user+0x170>
mov    0x2d91(%rip),%rax
cmpq   $0x0,(%rax)
je     1285 <util_lookup_user+0x135>
mov    0x2d80(%rip),%rax
mov    (%rax),%rax
mov    0x2d66(%rip),%rcx
cmp    (%rcx),%rax
je     1285 <util_lookup_user+0x135>
mov    0x2d66(%rip),%rax
mov    (%rax),%rax
mov    0x2d44(%rip),%rcx
cmp    (%rcx),%rax
jne    129e <util_lookup_user+0x14e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd71(%rip),%rsi
call   1030 <udev_log_null@plt>
jmp    12b2 <util_lookup_user+0x162>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd75(%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    %eax,%r13d
mov    %rsp,%r14
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe6a(%rip),%rsi
mov    %r12,%rdi
call   1040 <strcmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     1230 <util_lookup_user+0xe0>
lea    -0x48(%rbp),%rsi
mov    %r12,%rdi
mov    $0xa,%edx
call   1070 <strtoul@plt>
mov    -0x48(%rbp),%rcx
cmpb   $0x0,(%rcx)
je     11f2 <util_lookup_user+0xa2>
lea    -0x40(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    %r12,%rdi
mov    %rbx,%rdx
mov    %r13d,%ecx
call   1050 <getpwnam_r@plt>
mov    0x2df7(%rip),%rcx
mov    %rax,(%rcx)
mov    -0x38(%rbp),%rcx
test   %rcx,%rcx
je     11f7 <util_lookup_user+0xa7>
mov    (%rcx),%r15d
jmp    1230 <util_lookup_user+0xe0>
mov    %eax,%r15d
jmp    1230 <util_lookup_user+0xe0>
test   %rax,%rax
je     121d <util_lookup_user+0xcd>
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
je     121d <util_lookup_user+0xcd>
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%rax
je     121d <util_lookup_user+0xcd>
lea    0xe07(%rip),%rsi
jmp    1224 <util_lookup_user+0xd4>
lea    0xde1(%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 i32
%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 i32 @sysconf(i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %8
%19 = alloca i8, i64 %17
store i64 %17, i64* %9
%20 = load i8*, i8** %5
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %13
br label %64
24: ; preds = %2
%25 = load i8*, i8** %5
%26 = call i32 @strtoul(i8* %25, i8** %6, i32 10)
store i32 %26, i32* %12
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = load i32, i32* %12
store i32 %33, i32* %3
store i32 1, i32* %13
br label %64
34: ; preds = %24
%35 = load i8*, i8** %5
%36 = load i32, i32* %7
%37 = call i64 @getpwnam_r(i8* %35, %struct.passwd* %10, i8* %19, i32 %36, %struct.passwd** %11)
store i64 %37, i64* @errno
%38 = load %struct.passwd*, %struct.passwd** %11
%39 = icmp ne %struct.passwd* %38, null
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load %struct.passwd*, %struct.passwd** %11
%42 = getelementptr inbounds %struct.passwd, %struct.passwd* %41, i32 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %3
store i32 1, i32* %13
br label %64
44: ; preds = %34
%45 = load i64, i64* @errno
%46 = icmp eq i64 %45, 0
br i1 %46, label %55, label %47
47: ; preds = %44
%48 = load i64, i64* @errno
%49 = load i64, i64* @ENOENT
%50 = icmp eq i64 %48, %49
br i1 %50, label %55, label %51
51: ; preds = %47
%52 = load i64, i64* @errno
%53 = load i64, i64* @ESRCH
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %59
55: ; preds = %51, %47, %44
%56 = load %struct.udev*, %struct.udev** %4
%57 = load i8*, i8** %5
%58 = call i32 @udev_log_null(%struct.udev* %56, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %57)
br label %63
59: ; preds = %51
%60 = load %struct.udev*, %struct.udev** %4
%61 = load i8*, i8** %5
%62 = call i32 @udev_log_null(%struct.udev* %60, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %59, %55
store i32 0, i32* %3
store i32 1, i32* %13
br label %64
64: ; preds = %63, %40, %32, %23
%65 = load i8*, i8** %8
%66 = load i32, i32* %3
ret i32 %66
}

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 i32 @sysconf(i32 %7)
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = bitcast %struct.passwd* %4 to i8*
%13 = bitcast %struct.passwd** %5 to i8*
%14 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %40, label %16
16: ; preds = %2
%17 = call i32 @strtoul(i8* %1, i8** %3, i32 10)
%18 = load i8*, i8** %3
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %40, label %21
21: ; preds = %16
%22 = call i64 @getpwnam_r(i8* %1, %struct.passwd* %4, i8* %11, i32 %8, %struct.passwd** %5)
store i64 %22, i64* @errno
%23 = load %struct.passwd*, %struct.passwd** %5
%24 = icmp eq %struct.passwd* %23, null
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i64 0, i32 0
%27 = load i32, i32* %26
br label %40
28: ; preds = %21
%29 = icmp eq i64 %22, 0
%30 = load i64, i64* @ENOENT
%31 = icmp eq i64 %22, %30
%32 = select i1 %29, i1 true, i1 %31
%33 = load i64, i64* @ESRCH
%34 = icmp eq i64 %22, %33
%35 = select i1 %32, i1 true, i1 %34
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = 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 %40
38: ; preds = %28
%39 = 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 %40
40: ; preds = %36, %38, %16, %2, %25
%41 = phi i32 [ %27, %25 ], [ 0, %2 ], [ %17, %16 ], [ 0, %38 ], [ 0, %36 ]
ret i32 %41
}