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

/* Variables and functions */
scalar_t__ ERANGE;
long INT_MAX;
long LONG_MAX;
long LONG_MIN;
scalar_t__ _getpwnam_r(char *, struct passwd *, char *, int, struct passwd **);
scalar_t__ errno;
scalar_t__ slurm_getpwuid_r(long, struct passwd *, char *, int,
                            struct passwd **);
long strtol(char *, char **, int);
int uid_from_string(char *name, uid_t *uidp) {
  struct passwd pwd, *result;
  char buffer[65536], *p = NULL;
  long l;
  if (!name)
    return -1;
  if ((_getpwnam_r(name, &pwd, buffer, 65536, &result) == 0) &&
      result != NULL) {
    *uidp = result->pw_uid;
    return 0;
  }
  errno = 0;
  l = strtol(name, &p, 10);
  if (((errno == ERANGE) && ((l == LONG_MIN) || (l == LONG_MAX))) ||
      (name == p) || (*p != '\0') || (l < 0) || (l > INT_MAX))
    return -1;
  if (slurm_getpwuid_r(l, &pwd, buffer, 65536, &result) != 0)
    return -1;
  *uidp = (uid_t)l;
  return 0;
}


x86 assembly (clang, -O0):
<uid_from_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x10038(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1165 <uid_from_string+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <uid_from_string+0x193>
mov    -0x10(%rbp),%rdi
lea    -0x10030(%rbp),%rdx
lea    -0x20(%rbp),%rsi
mov    $0x10000,%ecx
lea    -0x28(%rbp),%r8
call   1030 <_getpwnam_r@plt>
cmp    $0x0,%rax
jne    11b1 <uid_from_string+0x81>
cmpq   $0x0,-0x28(%rbp)
je     11b1 <uid_from_string+0x81>
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12c3 <uid_from_string+0x193>
mov    0x2e08(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x10038(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x10040(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%rax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%rax
jne    1223 <uid_from_string+0xf3>
mov    -0x10040(%rbp),%rax
mov    0x2ddd(%rip),%rcx
cmp    (%rcx),%rax
je     126c <uid_from_string+0x13c>
mov    -0x10040(%rbp),%rax
mov    0x2dae(%rip),%rcx
cmp    (%rcx),%rax
je     126c <uid_from_string+0x13c>
mov    -0x10(%rbp),%rax
cmp    -0x10038(%rbp),%rax
je     126c <uid_from_string+0x13c>
mov    -0x10038(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    126c <uid_from_string+0x13c>
cmpq   $0x0,-0x10040(%rbp)
jl     126c <uid_from_string+0x13c>
mov    -0x10040(%rbp),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
jle    1278 <uid_from_string+0x148>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <uid_from_string+0x193>
mov    -0x10040(%rbp),%rdi
lea    -0x10030(%rbp),%rdx
lea    -0x20(%rbp),%rsi
mov    $0x10000,%ecx
lea    -0x28(%rbp),%r8
call   1040 <slurm_getpwuid_r@plt>
cmp    $0x0,%rax
je     12ae <uid_from_string+0x17e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <uid_from_string+0x193>
mov    -0x10040(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uid_from_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10020,%rsp
movq   $0x0,0x8(%rsp)
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     1224 <uid_from_string+0xf4>
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x18(%rsp),%rsi
lea    0x20(%rsp),%rdx
lea    0x10(%rsp),%r8
mov    $0x10000,%ecx
call   1030 <_getpwnam_r@plt>
test   %rax,%rax
jne    1193 <uid_from_string+0x63>
mov    0x10(%rsp),%rax
test   %rax,%rax
je     1193 <uid_from_string+0x63>
mov    (%rax),%rbp
mov    %rbp,(%r14)
xor    %r15d,%r15d
jmp    1224 <uid_from_string+0xf4>
mov    0x2e26(%rip),%r12
movq   $0x0,(%r12)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,%rbp
mov    (%r12),%rax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%rax
jne    11df <uid_from_string+0xaf>
mov    0x2e12(%rip),%rax
cmp    (%rax),%rbp
je     1224 <uid_from_string+0xf4>
mov    0x2dee(%rip),%rax
cmp    (%rax),%rbp
je     1224 <uid_from_string+0xf4>
mov    0x8(%rsp),%rax
cmp    %rbx,%rax
je     1224 <uid_from_string+0xf4>
cmpb   $0x0,(%rax)
jne    1224 <uid_from_string+0xf4>
test   %rbp,%rbp
js     1224 <uid_from_string+0xf4>
mov    0x2dee(%rip),%rax
cmp    (%rax),%rbp
jg     1224 <uid_from_string+0xf4>
lea    0x18(%rsp),%rsi
lea    0x20(%rsp),%rdx
lea    0x10(%rsp),%r8
mov    %rbp,%rdi
mov    $0x10000,%ecx
call   1040 <slurm_getpwuid_r@plt>
test   %rax,%rax
je     1188 <uid_from_string+0x58>
mov    %r15d,%eax
add    $0x10020,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i64 }
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
@INT_MAX = global i64 0
define i32 @uid_from_string(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.passwd
%7 = alloca %struct.passwd*
%8 = alloca [65536 x i8]
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
store i8* null, i8** %9
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 -1, i32* %3
br label %67
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = getelementptr inbounds [65536 x i8], [65536 x i8]* %8, i64 0, i64 0
%17 = call i64 @_getpwnam_r(i8* %15, %struct.passwd* %6, i8* %16, i32 65536, %struct.passwd** %7)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = load %struct.passwd*, %struct.passwd** %7
%21 = icmp ne %struct.passwd* %20, null
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load %struct.passwd*, %struct.passwd** %7
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64*, i64** %5
store i64 %25, i64* %26
store i32 0, i32* %3
br label %67
27: ; preds = %19, %14
store i64 0, i64* @errno
%28 = load i8*, i8** %4
%29 = call i64 @strtol(i8* %28, i8** %9, i32 10)
store i64 %29, i64* %10
%30 = load i64, i64* @errno
%31 = load i64, i64* @ERANGE
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %41
33: ; preds = %27
%34 = load i64, i64* %10
%35 = load i64, i64* @LONG_MIN
%36 = icmp eq i64 %34, %35
br i1 %36, label %57, label %37
37: ; preds = %33
%38 = load i64, i64* %10
%39 = load i64, i64* @LONG_MAX
%40 = icmp eq i64 %38, %39
br i1 %40, label %57, label %41
41: ; preds = %37, %27
%42 = load i8*, i8** %4
%43 = load i8*, i8** %9
%44 = icmp eq i8* %42, %43
br i1 %44, label %57, label %45
45: ; preds = %41
%46 = load i8*, i8** %9
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 0
br i1 %49, label %57, label %50
50: ; preds = %45
%51 = load i64, i64* %10
%52 = icmp slt i64 %51, 0
br i1 %52, label %57, label %53
53: ; preds = %50
%54 = load i64, i64* %10
%55 = load i64, i64* @INT_MAX
%56 = icmp sgt i64 %54, %55
br i1 %56, label %57, label %58
57: ; preds = %53, %50, %45, %41, %37, %33
store i32 -1, i32* %3
br label %67
58: ; preds = %53
%59 = load i64, i64* %10
%60 = getelementptr inbounds [65536 x i8], [65536 x i8]* %8, i64 0, i64 0
%61 = call i64 @slurm_getpwuid_r(i64 %59, %struct.passwd* %6, i8* %60, i32 65536, %struct.passwd** %7)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %58
store i32 -1, i32* %3
br label %67
64: ; preds = %58
%65 = load i64, i64* %10
%66 = load i64*, i64** %5
store i64 %65, i64* %66
store i32 0, i32* %3
br label %67
67: ; preds = %64, %63, %57, %22, %13
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
%struct.passwd = type { i64 }
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
@INT_MAX = global i64 0
define i32 @uid_from_string(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.passwd
%4 = alloca %struct.passwd*
%5 = alloca [65536 x i8]
%6 = alloca i8*
%7 = bitcast %struct.passwd* %3 to i8*
%8 = bitcast %struct.passwd** %4 to i8*
%9 = getelementptr inbounds [65536 x i8], [65536 x i8]* %5, i64 0, i64 0
%10 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%11 = icmp eq i8* %0, null
br i1 %11, label %48, label %12
12: ; preds = %2
%13 = call i64 @_getpwnam_r(i8* %0, %struct.passwd* %3, i8* %9, i32 65536, %struct.passwd** %4)
%14 = icmp eq i64 %13, 0
%15 = load %struct.passwd*, %struct.passwd** %4
%16 = icmp ne %struct.passwd* %15, null
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i64 0, i32 0
%20 = load i64, i64* %19
br label %46
21: ; preds = %12
store i64 0, i64* @errno
%22 = call i64 @strtol(i8* %0, i8** %6, i32 10)
%23 = load i64, i64* @errno
%24 = load i64, i64* @ERANGE
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i64, i64* @LONG_MIN
%28 = icmp eq i64 %22, %27
%29 = load i64, i64* @LONG_MAX
%30 = icmp eq i64 %22, %29
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %48, label %32
32: ; preds = %26, %21
%33 = load i8*, i8** %6
%34 = icmp eq i8* %33, %0
br i1 %34, label %48, label %35
35: ; preds = %32
%36 = load i8, i8* %33
%37 = icmp ne i8 %36, 0
%38 = icmp slt i64 %22, 0
%39 = select i1 %37, i1 true, i1 %38
%40 = load i64, i64* @INT_MAX
%41 = icmp sgt i64 %22, %40
%42 = select i1 %39, i1 true, i1 %41
br i1 %42, label %48, label %43
43: ; preds = %35
%44 = call i64 @slurm_getpwuid_r(i64 %22, %struct.passwd* %3, i8* %9, i32 65536, %struct.passwd** %4)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43, %18
%47 = phi i64 [ %20, %18 ], [ %22, %43 ]
store i64 %47, i64* %1
br label %48
48: ; preds = %46, %43, %26, %32, %35, %2
%49 = phi i32 [ -1, %2 ], [ -1, %35 ], [ -1, %32 ], [ -1, %26 ], [ -1, %43 ], [ 0, %46 ]
ret i32 %49
}