#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;
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EPERM;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getline(char **, size_t *, int /*<<< orphan*/ *);
int getuid();
int /*<<< orphan*/ stderr;
char *strsep(char **, char *);
int strtoull(char *, char **, int);
int user_to_uid(char *);
uid_t find_one_subxid(char x) {
  char filename[] = "/etc/subxid";
  filename[8] = x;
  FILE *f = fopen(filename, "r");
  if (!f)
    return -1;
  uid_t result = -1;
  ssize_t size;
  size_t bufsize = 0;
  char *str = NULL;
  while ((size = getline(&str, &bufsize, f)) > 0) {
    char *run = str;
    char *user = strsep(&run, ":");
    char *id_start = strsep(&run, ":");
    char *id_length = strsep(&run, "\n");
    if (!str || !id_start || !id_length) {
      fprintf(stderr, "Invalid %s entry ignored", filename);
      continue;
    }
    uid_t uid = user_to_uid(user);
    if (uid == (uid_t)-1) {
      fprintf(stderr, "Unknown user %s in %s ignored", user, filename);
      errno = 0;
      continue;
    }
    if (uid != getuid())
      continue;
    errno = 0;
    char *endptr;
    uid_t val = strtoull(id_start, &endptr, 10);
    if (errno || *endptr || val == (uid_t)-1) {
      fprintf(stderr, "Invalid subuid %s ignored", id_start);
      errno = 0;
      continue;
    }
    result = val;
    break;
  }
  if (result == (uid_t)-1)
    errno = EPERM;
  int err = errno;
  free(str);
  fclose(f);
  errno = err;
  return result;
}


x86 assembly (clang, -O0):
<find_one_subxid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    0xe58(%rip),%rax
mov    %rax,-0x11(%rbp)
mov    0xe56(%rip),%eax
mov    %eax,-0x9(%rbp)
mov    -0x5(%rbp),%al
mov    %al,-0x9(%rbp)
lea    -0x11(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e6 <find_one_subxid+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d0 <find_one_subxid+0x240>
movl   $0xffffffff,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdx
lea    -0x38(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <getline@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jle    1384 <find_one_subxid+0x1f4>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xde1(%rip),%rsi
call   1090 <strsep@plt>
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xdcd(%rip),%rsi
call   1090 <strsep@plt>
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xdbb(%rip),%rsi
call   1090 <strsep@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     127f <find_one_subxid+0xef>
cmpq   $0x0,-0x50(%rbp)
je     127f <find_one_subxid+0xef>
cmpq   $0x0,-0x58(%rbp)
jne    129f <find_one_subxid+0x10f>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    -0x11(%rbp),%rdx
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    11fd <find_one_subxid+0x6d>
mov    -0x48(%rbp),%rdi
call   1060 <user_to_uid@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    12e6 <find_one_subxid+0x156>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    -0x11(%rbp),%rcx
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2cfd(%rip),%rax
movl   $0x0,(%rax)
jmp    11fd <find_one_subxid+0x6d>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,%ecx
mov    -0x74(%rbp),%eax
cmp    %ecx,%eax
je     1305 <find_one_subxid+0x175>
jmp    11fd <find_one_subxid+0x6d>
mov    0x2ccc(%rip),%rax
movl   $0x0,(%rax)
mov    -0x50(%rbp),%rdi
lea    -0x68(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtoull@plt>
mov    %eax,-0x6c(%rbp)
mov    0x2caa(%rip),%rax
cmpl   $0x0,(%rax)
jne    1351 <find_one_subxid+0x1c1>
mov    -0x68(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1351 <find_one_subxid+0x1c1>
cmpl   $0xffffffff,-0x6c(%rbp)
jne    137e <find_one_subxid+0x1ee>
mov    0x2c88(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rdx
lea    0xce4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c65(%rip),%rax
movl   $0x0,(%rax)
jmp    11fd <find_one_subxid+0x6d>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    13a0 <find_one_subxid+0x210>
mov    0x2c33(%rip),%rax
mov    (%rax),%ecx
mov    0x2c3a(%rip),%rax
mov    %ecx,(%rax)
mov    0x2c31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x70(%rbp),%ecx
mov    0x2c10(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_one_subxid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movabs $0x6275732f6374652f,%rax
73 75 62
mov    %rax,0x18(%rsp)
movl   $0x646978,0x20(%rsp)
mov    %dil,0x20(%rsp)
lea    0xe4b(%rip),%rsi
lea    0x18(%rsp),%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1388 <find_one_subxid+0x1f8>
mov    %rax,%r15
movq   $0x0,(%rsp)
movq   $0x0,0x10(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
lea    0x10(%rsp),%r12
mov    %rsp,%rsi
jmp    1236 <find_one_subxid+0xa6>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
mov    %r13,%rdx
lea    0x18(%rsp),%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbx,%rax
movl   $0x0,(%rbx)
mov    $0x2,%ecx
mov    %rbp,%r12
mov    %rsp,%rsi
cmp    $0x2,%ecx
jne    1373 <find_one_subxid+0x1e3>
mov    %r12,%rdi
mov    %r15,%rdx
call   10b0 <getline@plt>
test   %eax,%eax
jle    1373 <find_one_subxid+0x1e3>
mov    0x10(%rsp),%rax
mov    %rax,0x30(%rsp)
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
lea    0xdac(%rip),%rbp
mov    %rbp,%rsi
call   1090 <strsep@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <strsep@plt>
mov    %rax,%r14
mov    %rbx,%rdi
lea    0xd8b(%rip),%rsi
call   1090 <strsep@plt>
cmpq   $0x0,0x10(%rsp)
je     1330 <find_one_subxid+0x1a0>
test   %r14,%r14
je     1330 <find_one_subxid+0x1a0>
test   %rax,%rax
je     1330 <find_one_subxid+0x1a0>
mov    %r12,%rbp
mov    0x2d26(%rip),%rbx
mov    %r13,%rdi
call   1060 <user_to_uid@plt>
cmp    $0xffffffff,%eax
je     11fa <find_one_subxid+0x6a>
mov    %eax,%r12d
xor    %eax,%eax
call   1050 <getuid@plt>
mov    $0x2,%ecx
cmp    %eax,%r12d
jne    1227 <find_one_subxid+0x97>
movl   $0x0,(%rbx)
mov    %r14,%rdi
lea    0x28(%rsp),%rsi
mov    $0xa,%edx
call   1070 <strtoull@plt>
cmpl   $0x0,(%rbx)
mov    %rbp,%r12
je     1356 <find_one_subxid+0x1c6>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2cbc(%rip),%rax
movl   $0x0,(%rax)
jmp    134c <find_one_subxid+0x1bc>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
lea    0xcd2(%rip),%rsi
lea    0x18(%rsp),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%ecx
jmp    122a <find_one_subxid+0x9a>
mov    0x28(%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    12fb <find_one_subxid+0x16b>
cmp    $0xffffffff,%eax
je     12fb <find_one_subxid+0x16b>
mov    $0x3,%ecx
mov    %eax,0xc(%rsp)
jmp    122a <find_one_subxid+0x9a>
mov    0xc(%rsp),%ebp
cmp    $0xffffffff,%ebp
je     138f <find_one_subxid+0x1ff>
mov    0x2c55(%rip),%rbx
mov    (%rbx),%r14d
jmp    13a3 <find_one_subxid+0x213>
mov    $0xffffffff,%ebp
jmp    13b8 <find_one_subxid+0x228>
mov    0x2c32(%rip),%rax
mov    (%rax),%r14d
mov    0x2c38(%rip),%rbx
mov    %r14d,(%rbx)
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14d,(%rbx)
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.find_one_subxid.filename = [12 x i8] c"/etc/subxid\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"\0A\00"
@stderr = global i32 0
@.str.3 = [25 x i8] c"Invalid %s entry ignored\00"
@.str.4 = [30 x i8] c"Unknown user %s in %s ignored\00"
@errno = global i32 0
@.str.5 = [26 x i8] c"Invalid subuid %s ignored\00"
@EPERM = global i32 0
define i32 @find_one_subxid(i8 signext %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca [12 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
store i8 %0, i8* %3
%18 = bitcast [12 x i8]* %4 to i8*
%19 = load i8, i8* %3
%20 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 8
store i8 %19, i8* %20
%21 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %5
%23 = load i32*, i32** %5
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %1
store i32 -1, i32* %2
br label %95
26: ; preds = %1
store i32 -1, i32* %6
store i64 0, i64* %8
store i8* null, i8** %9
br label %27
27: ; preds = %76, %62, %53, %44, %26
%28 = load i32*, i32** %5
%29 = call i32 @getline(i8** %9, i64* %8, i32* %28)
store i32 %29, i32* %7
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %82
31: ; preds = %27
%32 = load i8*, i8** %9
store i8* %32, i8** %10
%33 = call i8* @strsep(i8** %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %33, i8** %11
%34 = call i8* @strsep(i8** %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %34, i8** %12
%35 = call i8* @strsep(i8** %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %35, i8** %13
%36 = load i8*, i8** %9
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %44
38: ; preds = %31
%39 = load i8*, i8** %12
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i8*, i8** %13
%43 = icmp ne i8* %42, null
br i1 %43, label %48, label %44
44: ; preds = %41, %38, %31
%45 = load i32, i32* @stderr
%46 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%47 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %46)
br label %27
48: ; preds = %41
%49 = load i8*, i8** %11
%50 = call i32 @user_to_uid(i8* %49)
store i32 %50, i32* %14
%51 = load i32, i32* %14
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %58
53: ; preds = %48
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %11
%56 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%57 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %55, i8* %56)
store i32 0, i32* @errno
br label %27
58: ; preds = %48
%59 = load i32, i32* %14
%60 = call i32 (...) @getuid()
%61 = icmp ne i32 %59, %60
br i1 %61, label %62, label %63
62: ; preds = %58
br label %27
63: ; preds = %58
store i32 0, i32* @errno
%64 = load i8*, i8** %12
%65 = call i32 @strtoull(i8* %64, i8** %15, i32 10)
store i32 %65, i32* %16
%66 = load i32, i32* @errno
%67 = icmp ne i32 %66, 0
br i1 %67, label %76, label %68
68: ; preds = %63
%69 = load i8*, i8** %15
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = icmp ne i32 %71, 0
br i1 %72, label %76, label %73
73: ; preds = %68
%74 = load i32, i32* %16
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %80
76: ; preds = %73, %68, %63
%77 = load i32, i32* @stderr
%78 = load i8*, i8** %12
%79 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %78)
store i32 0, i32* @errno
br label %27
80: ; preds = %73
%81 = load i32, i32* %16
store i32 %81, i32* %6
br label %82
82: ; preds = %80, %27
%83 = load i32, i32* %6
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %87
85: ; preds = %82
%86 = load i32, i32* @EPERM
store i32 %86, i32* @errno
br label %87
87: ; preds = %85, %82
%88 = load i32, i32* @errno
store i32 %88, i32* %17
%89 = load i8*, i8** %9
%90 = call i32 @free(i8* %89)
%91 = load i32*, i32** %5
%92 = call i32 @fclose(i32* %91)
%93 = load i32, i32* %17
store i32 %93, i32* @errno
%94 = load i32, i32* %6
store i32 %94, i32* %2
br label %95
95: ; preds = %87, %25
%96 = load i32, i32* %2
ret i32 %96
}

LLVM IR (-O2):
@__const.find_one_subxid.filename = [12 x i8] c"/etc/subxid\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"\0A\00"
@stderr = global i32 0
@.str.3 = [25 x i8] c"Invalid %s entry ignored\00"
@.str.4 = [30 x i8] c"Unknown user %s in %s ignored\00"
@errno = global i32 0
@.str.5 = [26 x i8] c"Invalid subuid %s ignored\00"
@EPERM = global i32 0
define i32 @find_one_subxid(i8 signext %0) {
%2 = alloca [12 x i8]
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 8
store i8 %0, i8* %8
%9 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %75, label %11
11: ; preds = %1
%12 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%13 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%14 = bitcast i8** %5 to i8*
%15 = bitcast i8** %6 to i8*
br label %16
16: ; preds = %59, %11
%17 = phi i32 [ -1, %11 ], [ %60, %59 ]
%18 = call i32 @getline(i8** %4, i64* %3, i32* %9)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %63
20: ; preds = %16
%21 = load i8*, i8** %4
store i8* %21, i8** %5
%22 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = load i8*, i8** %4
%26 = icmp ne i8* %25, null
%27 = icmp ne i8* %23, null
%28 = select i1 %26, i1 %27, i1 false
%29 = icmp ne i8* %24, null
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %34, label %31
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br label %59
34: ; preds = %20
%35 = call i32 @user_to_uid(i8* %22)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %22, i8* %7)
store i32 0, i32* @errno
br label %59
40: ; preds = %34
%41 = call i32 (...) @getuid()
%42 = icmp eq i32 %35, %41
br i1 %42, label %43, label %59
43: ; preds = %40
store i32 0, i32* @errno
%44 = call i32 @strtoull(i8* %23, i8** %6, i32 10)
%45 = load i32, i32* @errno
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %43
%48 = load i8*, i8** %6
%49 = load i8, i8* %48
%50 = icmp ne i8 %49, 0
%51 = icmp eq i32 %44, -1
%52 = select i1 %50, i1 true, i1 %51
br i1 %52, label %53, label %56
53: ; preds = %47, %43
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %23)
store i32 0, i32* @errno
br label %56
56: ; preds = %47, %53
%57 = phi i32 [ %17, %53 ], [ %44, %47 ]
%58 = phi i32 [ 2, %53 ], [ 3, %47 ]
br label %59
59: ; preds = %37, %56, %40, %31
%60 = phi i32 [ %17, %31 ], [ %17, %37 ], [ %57, %56 ], [ %17, %40 ]
%61 = phi i32 [ 2, %31 ], [ 2, %37 ], [ %58, %56 ], [ 2, %40 ]
%62 = icmp eq i32 %61, 2
br i1 %62, label %16, label %63
63: ; preds = %59, %16
%64 = phi i32 [ %60, %59 ], [ %17, %16 ]
%65 = icmp eq i32 %64, -1
br i1 %65, label %68, label %66
66: ; preds = %63
%67 = load i32, i32* @errno
br label %70
68: ; preds = %63
%69 = load i32, i32* @EPERM
store i32 %69, i32* @errno
br label %70
70: ; preds = %66, %68
%71 = phi i32 [ %67, %66 ], [ %69, %68 ]
%72 = load i8*, i8** %4
%73 = call i32 @free(i8* %72)
%74 = call i32 @fclose(i32* %9)
store i32 %71, i32* @errno
br label %75
75: ; preds = %1, %70
%76 = phi i32 [ %64, %70 ], [ -1, %1 ]
ret i32 %76
}