#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 stat {
  int st_uid;
  int st_gid;
};
struct passwd {
  char *pw_name;
};
typedef enum nss_status { ____Placeholder_nss_status } nss_status;

/* Variables and functions */
int NSS_STATUS_UNAVAIL;
int atoi(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
scalar_t__ stat(char *, struct stat *);
size_t strlen(char *);
int unbit_res(char *, size_t, char *, int, int, struct passwd *, char **,
              size_t *);
enum nss_status _nss_unbit_getpwnam_r(char *name, struct passwd *res,
                                      char *buffer, size_t buflen,
                                      int *errnop) {
  struct stat st;
  char filename[1024];
  size_t name_len = strlen(name);
  uid_t uid = atoi(name);
  if (uid < 30000)
    return NSS_STATUS_UNAVAIL;
  if (name_len + sizeof("/containers/") >= 1024)
    return NSS_STATUS_UNAVAIL;
  memcpy(filename, "/containers/", sizeof("/containers/"));
  memcpy(filename + (sizeof("/containers/") - 1), name, name_len);
  filename[(sizeof("/containers/") - 1) + name_len] = 0;
  if (stat(filename, &st))
    return NSS_STATUS_UNAVAIL;
  if (st.st_uid != uid)
    return NSS_STATUS_UNAVAIL;
  if (st.st_gid != uid)
    return NSS_STATUS_UNAVAIL;
  res->pw_name = name;
  return unbit_res(name, name_len, filename,
                   (sizeof("/containers/") - 1) + name_len, uid, res, &buffer,
                   &buflen);
}


x86 assembly (clang, -O0):
<_nss_unbit_getpwnam_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x448(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x44c(%rbp)
cmpl   $0x7530,-0x44c(%rbp)
jge    11af <_nss_unbit_getpwnam_r+0x5f>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d6 <_nss_unbit_getpwnam_r+0x186>
mov    -0x448(%rbp),%rax
add    $0xd,%rax
cmp    $0x400,%rax
jb     11d7 <_nss_unbit_getpwnam_r+0x87>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d6 <_nss_unbit_getpwnam_r+0x186>
lea    -0x440(%rbp),%rdi
lea    0xe1b(%rip),%rsi
mov    $0xd,%edx
call   1050 <memcpy@plt>
lea    -0x440(%rbp),%rdi
add    $0xc,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x448(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x448(%rbp),%rax
add    $0xc,%rax
movb   $0x0,-0x440(%rbp,%rax,1)
lea    -0x440(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1248 <_nss_unbit_getpwnam_r+0xf8>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d6 <_nss_unbit_getpwnam_r+0x186>
mov    -0x38(%rbp),%eax
cmp    -0x44c(%rbp),%eax
je     1268 <_nss_unbit_getpwnam_r+0x118>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d6 <_nss_unbit_getpwnam_r+0x186>
mov    -0x34(%rbp),%eax
cmp    -0x44c(%rbp),%eax
je     1288 <_nss_unbit_getpwnam_r+0x138>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d6 <_nss_unbit_getpwnam_r+0x186>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x448(%rbp),%rsi
lea    -0x440(%rbp),%rdx
mov    -0x448(%rbp),%rax
add    $0xc,%rax
mov    %eax,%ecx
mov    -0x44c(%rbp),%r8d
mov    -0x18(%rbp),%r9
lea    -0x20(%rbp),%r10
lea    -0x28(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1060 <unbit_res@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nss_unbit_getpwnam_r>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rdx,0x18(%rsp)
mov    %rcx,0x10(%rsp)
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <atoi@plt>
cmp    $0x752f,%eax
jle    121f <_nss_unbit_getpwnam_r+0xcf>
mov    %eax,%ebp
lea    -0x3f3(%r15),%rax
cmp    $0xfffffffffffffbff,%rax
jbe    121f <_nss_unbit_getpwnam_r+0xcf>
lea    0xe5a(%rip),%rsi
lea    0x20(%rsp),%r12
mov    $0xd,%edx
mov    %r12,%rdi
call   1050 <memcpy@plt>
lea    0x2c(%rsp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
movb   $0x0,0x2c(%rsp,%r15,1)
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jne    121f <_nss_unbit_getpwnam_r+0xcf>
cmp    %ebp,0x8(%rsp)
jne    121f <_nss_unbit_getpwnam_r+0xcf>
cmp    %ebp,0xc(%rsp)
jne    121f <_nss_unbit_getpwnam_r+0xcf>
mov    %r15,%rcx
add    $0xc,%rcx
mov    %rbx,(%r14)
lea    0x10(%rsp),%rax
lea    0x18(%rsp),%r10
lea    0x20(%rsp),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%r8d
mov    %r14,%r9
push   %rax
push   %r10
call   1060 <unbit_res@plt>
add    $0x10,%rsp
jmp    1228 <_nss_unbit_getpwnam_r+0xd8>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
add    $0x420,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
%struct.stat = type { i32, i32 }
@NSS_STATUS_UNAVAIL = global i32 0
@.str = [13 x i8] c"/containers/\00"
define i32 @_nss_unbit_getpwnam_r(i8* %0, %struct.passwd* %1, i8* %2, i64 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.passwd*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca %struct.stat
%13 = alloca [1024 x i8]
%14 = alloca i64
%15 = alloca i32
store i8* %0, i8** %7
store %struct.passwd* %1, %struct.passwd** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i32* %4, i32** %11
%16 = load i8*, i8** %7
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %14
%18 = load i8*, i8** %7
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %15
%20 = load i32, i32* %15
%21 = icmp slt i32 %20, 30000
br i1 %21, label %22, label %24
22: ; preds = %5
%23 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %23, i32* %6
br label %73
24: ; preds = %5
%25 = load i64, i64* %14
%26 = add i64 %25, 13
%27 = icmp uge i64 %26, 1024
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %29, i32* %6
br label %73
30: ; preds = %24
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%32 = call i32 @memcpy(i8* %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 13)
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%34 = getelementptr inbounds i8, i8* %33, i64 12
%35 = load i8*, i8** %7
%36 = load i64, i64* %14
%37 = call i32 @memcpy(i8* %34, i8* %35, i64 %36)
%38 = load i64, i64* %14
%39 = add i64 12, %38
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%42 = call i64 @stat(i8* %41, %struct.stat* %12)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %30
%45 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %45, i32* %6
br label %73
46: ; preds = %30
%47 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* %15
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %52, i32* %6
br label %73
53: ; preds = %46
%54 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load i32, i32* %15
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = load i32, i32* @NSS_STATUS_UNAVAIL
store i32 %59, i32* %6
br label %73
60: ; preds = %53
%61 = load i8*, i8** %7
%62 = load %struct.passwd*, %struct.passwd** %8
%63 = getelementptr inbounds %struct.passwd, %struct.passwd* %62, i32 0, i32 0
store i8* %61, i8** %63
%64 = load i8*, i8** %7
%65 = load i64, i64* %14
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%67 = load i64, i64* %14
%68 = add i64 12, %67
%69 = trunc i64 %68 to i32
%70 = load i32, i32* %15
%71 = load %struct.passwd*, %struct.passwd** %8
%72 = call i32 @unbit_res(i8* %64, i64 %65, i8* %66, i32 %69, i32 %70, %struct.passwd* %71, i8** %9, i64* %10)
store i32 %72, i32* %6
br label %73
73: ; preds = %60, %58, %51, %44, %28, %22
%74 = load i32, i32* %6
ret i32 %74
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
%struct.stat = type { i32, i32 }
@NSS_STATUS_UNAVAIL = global i32 0
@.str = [13 x i8] c"/containers/\00"
define i32 @_nss_unbit_getpwnam_r(i8* %0, %struct.passwd* %1, i8* %2, i64 %3, i32* nocapture readnone %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca [1024 x i8]
store i8* %2, i8** %6
store i64 %3, i64* %7
%10 = bitcast %struct.stat* %8 to i8*
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%12 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%13 = tail call i32 @atoi(i8* %0)
%14 = icmp slt i32 %13, 30000
br i1 %14, label %15, label %17
15: ; preds = %5
%16 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %48
17: ; preds = %5
%18 = add i64 %12, -1011
%19 = icmp ult i64 %18, -1024
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %48
22: ; preds = %17
%23 = call i32 @memcpy(i8* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 13)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 12
%25 = call i32 @memcpy(i8* %24, i8* %0, i64 %12)
%26 = add nsw i64 %12, 12
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %26
store i8 0, i8* %27
%28 = call i64 @stat(i8* %11, %struct.stat* %8)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %22
%31 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %48
32: ; preds = %22
%33 = getelementptr inbounds %struct.stat, %struct.stat* %8, i64 0, i32 0
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, %13
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %48
38: ; preds = %32
%39 = getelementptr inbounds %struct.stat, %struct.stat* %8, i64 0, i32 1
%40 = load i32, i32* %39
%41 = icmp eq i32 %40, %13
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = load i32, i32* @NSS_STATUS_UNAVAIL
br label %48
44: ; preds = %38
%45 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 0
store i8* %0, i8** %45
%46 = trunc i64 %26 to i32
%47 = call i32 @unbit_res(i8* %0, i64 %12, i8* %11, i32 %46, i32 %13, %struct.passwd* %1, i8** %6, i64* %7)
br label %48
48: ; preds = %44, %42, %36, %30, %20, %15
%49 = phi i32 [ %16, %15 ], [ %21, %20 ], [ %31, %30 ], [ %37, %36 ], [ %43, %42 ], [ %47, %44 ]
ret i32 %49
}