#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 */
scalar_t__ errno;
struct passwd *getpwnam(char const *);
int strtoull(char const *, char **, int);
uid_t user_to_uid(const char *user) {
  char *endptr;
  errno = 0;
  uid_t uid = strtoull(user, &endptr, 10);
  if (!errno && !*endptr)
    return uid;
  errno = 0;
  struct passwd *passwd = getpwnam(user);
  if (!passwd)
    return -1;
  return passwd->pw_uid;
}


x86 assembly (clang, -O0):
<user_to_uid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ead(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtoull@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e8a(%rip),%rax
cmpq   $0x0,(%rax)
jne    1178 <user_to_uid+0x58>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1178 <user_to_uid+0x58>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b3 <user_to_uid+0x93>
mov    0x2e61(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11aa <user_to_uid+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <user_to_uid+0x93>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<user_to_uid>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eb2(%rip),%r14
movq   $0x0,(%r14)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtoull@plt>
cmpq   $0x0,(%r14)
jne    1151 <user_to_uid+0x31>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     116e <user_to_uid+0x4e>
movq   $0x0,(%r14)
mov    %rbx,%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     1169 <user_to_uid+0x49>
mov    (%rax),%eax
jmp    116e <user_to_uid+0x4e>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@errno = global i64 0
define i32 @user_to_uid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.passwd*
store i8* %0, i8** %3
store i64 0, i64* @errno
%7 = load i8*, i8** %3
%8 = call i32 @strtoull(i8* %7, i8** %4, i32 10)
store i32 %8, i32* %5
%9 = load i64, i64* @errno
%10 = icmp ne i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = load i32, i32* %5
store i32 %16, i32* %2
br label %27
17: ; preds = %11, %1
store i64 0, i64* @errno
%18 = load i8*, i8** %3
%19 = call %struct.passwd* @getpwnam(i8* %18)
store %struct.passwd* %19, %struct.passwd** %6
%20 = load %struct.passwd*, %struct.passwd** %6
%21 = icmp ne %struct.passwd* %20, null
br i1 %21, label %23, label %22
22: ; preds = %17
store i32 -1, i32* %2
br label %27
23: ; preds = %17
%24 = load %struct.passwd*, %struct.passwd** %6
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %2
br label %27
27: ; preds = %23, %22, %15
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@errno = global i64 0
define i32 @user_to_uid(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i32 @strtoull(i8* %0, i8** %2, i32 10)
%5 = load i64, i64* @errno
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %7, %1
store i64 0, i64* @errno
%12 = tail call %struct.passwd* @getpwnam(i8* %0)
%13 = icmp eq %struct.passwd* %12, null
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 0
%16 = load i32, i32* %15
br label %17
17: ; preds = %14, %11, %7
%18 = phi i32 [ %4, %7 ], [ %16, %14 ], [ -1, %11 ]
ret i32 %18
}