#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 */
struct passwd *getpwnam(char const *);
int strtol(char const *, char **, int);
uid_t userid_from_name(const char *name) {
  struct passwd *pwd;
  uid_t u;
  char *endptr;
  if (name == NULL || *name == '\0') {
    return -1;
  }
  u = strtol(name, &endptr, 10);
  if (*endptr == '\0') {
    return u;
  }
  pwd = getpwnam(name);
  if (pwd == NULL) {
    return -1;
  }
  return pwd->pw_uid;
}


x86 assembly (clang, -O0):
<userid_from_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1147 <userid_from_name+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1153 <userid_from_name+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <userid_from_name+0x90>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1183 <userid_from_name+0x63>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b0 <userid_from_name+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a7 <userid_from_name+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <userid_from_name+0x90>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<userid_from_name>:
push   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1161 <userid_from_name+0x41>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     1161 <userid_from_name+0x41>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     115f <userid_from_name+0x3f>
mov    %rbx,%rdi
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1161 <userid_from_name+0x41>
mov    (%rax),%ebp
jmp    1161 <userid_from_name+0x41>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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