#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 {
  int /*<<< orphan*/ pw_uid;
};

/* Variables and functions */
scalar_t__ errno;
struct passwd *getpwnam(char const *);
struct passwd *getpwuid(long);
long strtol(char const *, char **, int);
int get_user(uid_t *p_uid, const char *username) {
  struct passwd *passwd;
  char *endptr;
  long maybe_uid;
  int retval = 0;
  errno = 0;
  maybe_uid = strtol(username, &endptr, 10);
  if (endptr == username || *endptr != '\0') {
    errno = 0;
    passwd = getpwnam(username);
  } else if (errno != 0) {
    return 2;
  } else {
    errno = 0;
    passwd = getpwuid(maybe_uid);
  }
  if (passwd != NULL) {
    *p_uid = passwd->pw_uid;
  } else {
    if (errno == 0)
      retval = 1;
    else
      retval = 2;
  }
  return retval;
}


x86 assembly (clang, -O0):
<get_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
mov    0x2e92(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     1189 <get_user+0x59>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a9 <get_user+0x79>
mov    0x2e50(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
jmp    11e6 <get_user+0xb6>
mov    0x2e30(%rip),%rax
cmpq   $0x0,(%rax)
je     11c6 <get_user+0x96>
movl   $0x2,-0x4(%rbp)
jmp    1231 <get_user+0x101>
mov    0x2e13(%rip),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
call   1030 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
jmp    11e6 <get_user+0xb6>
cmpq   $0x0,-0x20(%rbp)
je     1202 <get_user+0xd2>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    122b <get_user+0xfb>
mov    0x2dd7(%rip),%rax
cmpq   $0x0,(%rax)
jne    121f <get_user+0xef>
movl   $0x1,-0x34(%rbp)
jmp    1226 <get_user+0xf6>
movl   $0x2,-0x34(%rbp)
jmp    122b <get_user+0xfb>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e9a(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    0x8(%rsp),%rcx
cmp    %rbx,%rcx
je     116e <get_user+0x3e>
cmpb   $0x0,(%rcx)
je     118b <get_user+0x5b>
movq   $0x0,(%r15)
mov    %rbx,%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     11aa <get_user+0x7a>
mov    (%rax),%eax
mov    %eax,(%r14)
xor    %ecx,%ecx
jmp    11b6 <get_user+0x86>
mov    $0x2,%ecx
cmpq   $0x0,(%r15)
jne    11b6 <get_user+0x86>
movq   $0x0,(%r15)
mov    %rax,%rdi
call   1030 <getpwuid@plt>
test   %rax,%rax
jne    1182 <get_user+0x52>
cmpq   $0x1,(%r15)
mov    $0x2,%ecx
sbb    $0x0,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@errno = global i64 0
define i32 @get_user(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca %struct.passwd*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 0, i32* %9
store i64 0, i64* @errno
%10 = load i8*, i8** %5
%11 = call i64 @strtol(i8* %10, i8** %7, i32 10)
store i64 %11, i64* %8
%12 = load i8*, i8** %7
%13 = load i8*, i8** %5
%14 = icmp eq i8* %12, %13
br i1 %14, label %20, label %15
15: ; preds = %2
%16 = load i8*, i8** %7
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15, %2
store i64 0, i64* @errno
%21 = load i8*, i8** %5
%22 = call %struct.passwd* @getpwnam(i8* %21)
store %struct.passwd* %22, %struct.passwd** %6
br label %31
23: ; preds = %15
%24 = load i64, i64* @errno
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 2, i32* %3
br label %47
27: ; preds = %23
store i64 0, i64* @errno
%28 = load i64, i64* %8
%29 = call %struct.passwd* @getpwuid(i64 %28)
store %struct.passwd* %29, %struct.passwd** %6
br label %30
30: ; preds = %27
br label %31
31: ; preds = %30, %20
%32 = load %struct.passwd*, %struct.passwd** %6
%33 = icmp ne %struct.passwd* %32, null
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load %struct.passwd*, %struct.passwd** %6
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32*, i32** %4
store i32 %37, i32* %38
br label %45
39: ; preds = %31
%40 = load i64, i64* @errno
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
store i32 1, i32* %9
br label %44
43: ; preds = %39
store i32 2, i32* %9
br label %44
44: ; preds = %43, %42
br label %45
45: ; preds = %44, %34
%46 = load i32, i32* %9
store i32 %46, i32* %3
br label %47
47: ; preds = %45, %26
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@errno = global i64 0
define i32 @get_user(i32* nocapture writeonly %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%5 = call i64 @strtol(i8* %1, i8** %3, i32 10)
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, %1
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8, %2
store i64 0, i64* @errno
%12 = tail call %struct.passwd* @getpwnam(i8* %1)
br label %18
13: ; preds = %8
%14 = load i64, i64* @errno
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %13
store i64 0, i64* @errno
%17 = tail call %struct.passwd* @getpwuid(i64 %5)
br label %18
18: ; preds = %16, %11
%19 = phi %struct.passwd* [ %12, %11 ], [ %17, %16 ]
%20 = icmp eq %struct.passwd* %19, null
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i64 0, i32 0
%23 = load i32, i32* %22
store i32 %23, i32* %0
br label %28
24: ; preds = %18
%25 = load i64, i64* @errno
%26 = icmp eq i64 %25, 0
%27 = select i1 %26, i32 1, i32 2
br label %28
28: ; preds = %21, %24, %13
%29 = phi i32 [ 2, %13 ], [ 0, %21 ], [ %27, %24 ]
ret i32 %29
}