#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 passwd {
  char const *pw_name;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
struct passwd *getpwnam(char const *);
struct passwd *getpwuid(int /*<<< orphan*/);
scalar_t__ isdigit(char const);
int sscanf(char const *, char *, int *);
char *strdup(char const *);
char *security_parse_user(const char *spec) {
  uid_t uid;
  struct passwd *pwdata;
  assert(spec != NULL);
  assert(sizeof(uid_t) == sizeof(int));
  if (isdigit(spec[0])) {
    if (sscanf(spec, "%d", (int *)&uid) != 1) {
      return NULL;
    }
    pwdata = getpwuid(uid);
    if (!pwdata) {
      return NULL;
    }
    return strdup(pwdata->pw_name);
  } else {
    pwdata = getpwnam(spec);
    if (!pwdata)
      return NULL;
    else
      return strdup(spec);
  }
}


x86 assembly (clang, -O0):
<security_parse_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x1,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1201 <security_parse_user+0xa1>
mov    -0x10(%rbp),%rdi
lea    0xe57(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     11c8 <security_parse_user+0x68>
movq   $0x0,-0x8(%rbp)
jmp    1233 <security_parse_user+0xd3>
mov    -0x14(%rbp),%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ec <security_parse_user+0x8c>
movq   $0x0,-0x8(%rbp)
jmp    1233 <security_parse_user+0xd3>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1233 <security_parse_user+0xd3>
mov    -0x10(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1226 <security_parse_user+0xc6>
movq   $0x0,-0x8(%rbp)
jmp    1233 <security_parse_user+0xd3>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<security_parse_user>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
mov    $0x1,%edi
call   1060 <assert@plt>
movsbl (%rbx),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     11be <security_parse_user+0x5e>
lea    0xe6c(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    11d8 <security_parse_user+0x78>
mov    0xc(%rsp),%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     11d8 <security_parse_user+0x78>
mov    (%rax),%rdi
call   1080 <strdup@plt>
jmp    11da <security_parse_user+0x7a>
mov    %rbx,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     11d8 <security_parse_user+0x78>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1080 <strdup@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [3 x i8] c"%d\00"
define i8* @security_parse_user(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.passwd*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = call i32 @assert(i32 1)
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
%14 = call i64 @isdigit(i8 signext %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %16
store i8* null, i8** %2
br label %41
21: ; preds = %16
%22 = load i32, i32* %4
%23 = call %struct.passwd* @getpwuid(i32 %22)
store %struct.passwd* %23, %struct.passwd** %5
%24 = load %struct.passwd*, %struct.passwd** %5
%25 = icmp ne %struct.passwd* %24, null
br i1 %25, label %27, label %26
26: ; preds = %21
store i8* null, i8** %2
br label %41
27: ; preds = %21
%28 = load %struct.passwd*, %struct.passwd** %5
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i8* @strdup(i8* %30)
store i8* %31, i8** %2
br label %41
32: ; preds = %1
%33 = load i8*, i8** %3
%34 = call %struct.passwd* @getpwnam(i8* %33)
store %struct.passwd* %34, %struct.passwd** %5
%35 = load %struct.passwd*, %struct.passwd** %5
%36 = icmp ne %struct.passwd* %35, null
br i1 %36, label %38, label %37
37: ; preds = %32
store i8* null, i8** %2
br label %41
38: ; preds = %32
%39 = load i8*, i8** %3
%40 = call i8* @strdup(i8* %39)
store i8* %40, i8** %2
br label %41
41: ; preds = %38, %37, %27, %26, %20
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [3 x i8] c"%d\00"
define noalias i8* @security_parse_user(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i32 @assert(i32 1)
%8 = load i8, i8* %0
%9 = tail call i64 @isdigit(i8 signext %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %1
%12 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = load i32, i32* %2
%16 = call %struct.passwd* @getpwuid(i32 %15)
%17 = icmp eq %struct.passwd* %16, null
br i1 %17, label %27, label %18
18: ; preds = %14
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i8* @strdup(i8* %20)
br label %27
22: ; preds = %1
%23 = tail call %struct.passwd* @getpwnam(i8* %0)
%24 = icmp eq %struct.passwd* %23, null
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i8* @strdup(i8* %0)
br label %27
27: ; preds = %22, %14, %11, %25, %18
%28 = phi i8* [ %21, %18 ], [ %26, %25 ], [ null, %11 ], [ null, %14 ], [ null, %22 ]
ret i8* %28
}