#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  char *pw_name;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ geteuid();
TYPE_1__ *getpwuid(int /*<<< orphan*/);
char **realloc(char **, unsigned int);
int sscanf(char *, char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int read_shells(char *shells_path, char ***shell_name, char ***shell_cmds) {
  FILE *shells_file = fopen(shells_path, "r");
  if (!shells_file) {
    fprintf(stderr, "error: unable to open shells file\n");
    exit(1);
  }
  char *login_name = getpwuid(geteuid())->pw_name;
  if (!login_name) {
    fprintf(stderr, "error: unable to determine user name\n");
    exit(1);
  }
  unsigned int num_shells = 0;
  shell_name[0] = 0;
  shell_cmds[0] = 0;
  while (!feof(shells_file)) {
    char *line = calloc(4097, sizeof(char));
    char *user = calloc(1024, sizeof(char));
    char *name = calloc(1024, sizeof(char));
    char *cmd = calloc(2048, sizeof(char));
    fgets(line, 4096, shells_file);
    int elems = sscanf(line, "%s %s %[^\r\n]", user, name, cmd);
    if (elems == 3 && strcmp(user, login_name) == 0 && line[0] != '#') {
      num_shells++;
      shell_name[0] = realloc(shell_name[0], num_shells * sizeof(char *));
      shell_cmds[0] = realloc(shell_cmds[0], num_shells * sizeof(char *));
      shell_name[0][num_shells - 1] = name;
      shell_cmds[0][num_shells - 1] = cmd;
    } else {
      free(name);
      free(cmd);
    }
    free(user);
    free(line);
  }
  fclose(shells_file);
  return num_shells;
}


x86 assembly (clang, -O0):
<read_shells>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1222 <read_shells+0x52>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1261 <read_shells+0x91>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   10b0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    129c <read_shells+0xcc>
jmp    13ed <read_shells+0x21d>
mov    $0x1001,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    $0x400,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x40(%rbp)
mov    $0x400,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x48(%rbp)
mov    $0x800,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x1000,%esi
call   1070 <fgets@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x50(%rbp),%r8
lea    0xd3a(%rip),%rsi
call   10c0 <sscanf@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x3,-0x54(%rbp)
jne    13c4 <read_shells+0x1f4>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    13c4 <read_shells+0x1f4>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x23,%eax
je     13c4 <read_shells+0x1f4>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%esi
call   10d0 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%eax
shl    $0x3,%rax
mov    %eax,%esi
call   10d0 <realloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0x50(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    %rdx,(%rax,%rcx,8)
jmp    13d6 <read_shells+0x206>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    127e <read_shells+0xae>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_shells>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x10(%rsp)
mov    %rsi,(%rsp)
lea    0xe12(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     139a <read_shells+0x1ca>
mov    %rax,%rbx
xor    %r13d,%r13d
xor    %eax,%eax
call   1060 <geteuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    (%rax),%rax
mov    %rax,0x20(%rsp)
test   %rax,%rax
je     13ac <read_shells+0x1dc>
mov    (%rsp),%rax
movq   $0x0,(%rax)
mov    0x10(%rsp),%rax
movq   $0x0,(%rax)
mov    %rbx,%rdi
call   10b0 <feof@plt>
test   %eax,%eax
je     125e <read_shells+0x8e>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r13d,%r13d
jmp    129c <read_shells+0xcc>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   10b0 <feof@plt>
test   %eax,%eax
jne    1244 <read_shells+0x74>
mov    $0x1001,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r14
mov    $0x400,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r15
mov    $0x400,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r12
mov    $0x800,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbp
mov    %r14,%rdi
mov    $0x1000,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    %r14,%rdi
lea    0xd4d(%rip),%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %rbp,%r8
call   10c0 <sscanf@plt>
cmp    $0x3,%eax
jne    1270 <read_shells+0xa0>
mov    %r15,%rdi
mov    0x20(%rsp),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1270 <read_shells+0xa0>
cmpb   $0x23,(%r14)
je     1270 <read_shells+0xa0>
mov    %r13,0x18(%rsp)
lea    0x1(%r13),%eax
mov    %eax,0xc(%rsp)
mov    (%rsp),%rax
mov    (%rax),%rdi
lea    0x8(,%r13,8),%esi
mov    %esi,0x8(%rsp)
call   10d0 <realloc@plt>
mov    (%rsp),%rcx
mov    %rax,(%rcx)
mov    0x10(%rsp),%r13
mov    0x0(%r13),%rdi
mov    0x8(%rsp),%esi
call   10d0 <realloc@plt>
mov    %rax,0x0(%r13)
mov    (%rsp),%rax
mov    (%rax),%rax
mov    0x18(%rsp),%ecx
mov    %r12,(%rax,%rcx,8)
mov    0x0(%r13),%rax
mov    %rbp,(%rax,%rcx,8)
mov    0xc(%rsp),%eax
mov    %eax,%r13d
jmp    1280 <read_shells+0xb0>
mov    0x2c3f(%rip),%rax
mov    (%rax),%edi
lea    0xc58(%rip),%rsi
jmp    13bc <read_shells+0x1ec>
mov    0x2c2d(%rip),%rax
mov    (%rax),%edi
lea    0xc69(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_2__ = type { i8* }
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"error: unable to open shells file\0A\00"
@.str.2 = [38 x i8] c"error: unable to determine user name\0A\00"
@.str.3 = [13 x i8] c"%s %s %[^\0D\0A]\00"
define i32 @read_shells(i8* %0, i8*** %1, i8*** %2) {
%4 = alloca i8*
%5 = alloca i8***
%6 = alloca i8***
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
store i8* %0, i8** %4
store i8*** %1, i8**** %5
store i8*** %2, i8**** %6
%15 = load i8*, i8** %4
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = icmp ne i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %3
%24 = call i32 (...) @geteuid()
%25 = call %struct.TYPE_2__* @getpwuid(i32 %24)
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i8*, i8** %26
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %34, label %30
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %23
store i32 0, i32* %9
%35 = load i8***, i8**** %5
%36 = getelementptr inbounds i8**, i8*** %35, i64 0
store i8** null, i8*** %36
%37 = load i8***, i8**** %6
%38 = getelementptr inbounds i8**, i8*** %37, i64 0
store i8** null, i8*** %38
br label %39
39: ; preds = %114, %34
%40 = load i32*, i32** %7
%41 = call i32 @feof(i32* %40)
%42 = icmp ne i32 %41, 0
%43 = xor i1 %42, true
br i1 %43, label %44, label %119
44: ; preds = %39
%45 = call i8* @calloc(i32 4097, i32 1)
store i8* %45, i8** %10
%46 = call i8* @calloc(i32 1024, i32 1)
store i8* %46, i8** %11
%47 = call i8* @calloc(i32 1024, i32 1)
store i8* %47, i8** %12
%48 = call i8* @calloc(i32 2048, i32 1)
store i8* %48, i8** %13
%49 = load i8*, i8** %10
%50 = load i32*, i32** %7
%51 = call i32 @fgets(i8* %49, i32 4096, i32* %50)
%52 = load i8*, i8** %10
%53 = load i8*, i8** %11
%54 = load i8*, i8** %12
%55 = load i8*, i8** %13
%56 = call i32 @sscanf(i8* %52, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %53, i8* %54, i8* %55)
store i32 %56, i32* %14
%57 = load i32, i32* %14
%58 = icmp eq i32 %57, 3
br i1 %58, label %59, label %109
59: ; preds = %44
%60 = load i8*, i8** %11
%61 = load i8*, i8** %8
%62 = call i64 @strcmp(i8* %60, i8* %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %109
64: ; preds = %59
%65 = load i8*, i8** %10
%66 = getelementptr inbounds i8, i8* %65, i64 0
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = icmp ne i32 %68, 35
br i1 %69, label %70, label %109
70: ; preds = %64
%71 = load i32, i32* %9
%72 = add i32 %71, 1
store i32 %72, i32* %9
%73 = load i8***, i8**** %5
%74 = getelementptr inbounds i8**, i8*** %73, i64 0
%75 = load i8**, i8*** %74
%76 = load i32, i32* %9
%77 = zext i32 %76 to i64
%78 = mul i64 %77, 8
%79 = trunc i64 %78 to i32
%80 = call i8** @realloc(i8** %75, i32 %79)
%81 = load i8***, i8**** %5
%82 = getelementptr inbounds i8**, i8*** %81, i64 0
store i8** %80, i8*** %82
%83 = load i8***, i8**** %6
%84 = getelementptr inbounds i8**, i8*** %83, i64 0
%85 = load i8**, i8*** %84
%86 = load i32, i32* %9
%87 = zext i32 %86 to i64
%88 = mul i64 %87, 8
%89 = trunc i64 %88 to i32
%90 = call i8** @realloc(i8** %85, i32 %89)
%91 = load i8***, i8**** %6
%92 = getelementptr inbounds i8**, i8*** %91, i64 0
store i8** %90, i8*** %92
%93 = load i8*, i8** %12
%94 = load i8***, i8**** %5
%95 = getelementptr inbounds i8**, i8*** %94, i64 0
%96 = load i8**, i8*** %95
%97 = load i32, i32* %9
%98 = sub i32 %97, 1
%99 = zext i32 %98 to i64
%100 = getelementptr inbounds i8*, i8** %96, i64 %99
store i8* %93, i8** %100
%101 = load i8*, i8** %13
%102 = load i8***, i8**** %6
%103 = getelementptr inbounds i8**, i8*** %102, i64 0
%104 = load i8**, i8*** %103
%105 = load i32, i32* %9
%106 = sub i32 %105, 1
%107 = zext i32 %106 to i64
%108 = getelementptr inbounds i8*, i8** %104, i64 %107
store i8* %101, i8** %108
br label %114
109: ; preds = %64, %59, %44
%110 = load i8*, i8** %12
%111 = call i32 @free(i8* %110)
%112 = load i8*, i8** %13
%113 = call i32 @free(i8* %112)
br label %114
114: ; preds = %109, %70
%115 = load i8*, i8** %11
%116 = call i32 @free(i8* %115)
%117 = load i8*, i8** %10
%118 = call i32 @free(i8* %117)
br label %39
119: ; preds = %39
%120 = load i32*, i32** %7
%121 = call i32 @fclose(i32* %120)
%122 = load i32, i32* %9
ret i32 %122
}

LLVM IR (-O2):
%struct.TYPE_2__ = type { i8* }
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"error: unable to open shells file\0A\00"
@.str.2 = [38 x i8] c"error: unable to determine user name\0A\00"
@.str.3 = [13 x i8] c"%s %s %[^\0D\0A]\00"
define i32 @read_shells(i8* nocapture %0, i8*** nocapture %1, i8*** nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = tail call i32 (...) @geteuid()
%12 = tail call %struct.TYPE_2__* @getpwuid(i32 %11)
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
store i8** null, i8*** %1
store i8** null, i8*** %2
%21 = tail call i32 @feof(i32* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %59
23: ; preds = %20, %53
%24 = phi i32 [ %54, %53 ], [ 0, %20 ]
%25 = tail call dereferenceable_or_null(4097) i8* @calloc(i32 4097, i32 1)
%26 = tail call dereferenceable_or_null(1024) i8* @calloc(i32 1024, i32 1)
%27 = tail call dereferenceable_or_null(1024) i8* @calloc(i32 1024, i32 1)
%28 = tail call dereferenceable_or_null(2048) i8* @calloc(i32 2048, i32 1)
%29 = tail call i32 @fgets(i8* %25, i32 4096, i32* %4)
%30 = tail call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %26, i8* %27, i8* %28)
%31 = icmp eq i32 %30, 3
br i1 %31, label %32, label %50
32: ; preds = %23
%33 = tail call i64 @strcmp(i8* %26, i8* %14)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %50
35: ; preds = %32
%36 = load i8, i8* %25
%37 = icmp eq i8 %36, 35
br i1 %37, label %50, label %38
38: ; preds = %35
%39 = add i32 %24, 1
%40 = load i8**, i8*** %1
%41 = shl i32 %39, 3
%42 = tail call i8** @realloc(i8** %40, i32 %41)
store i8** %42, i8*** %1
%43 = load i8**, i8*** %2
%44 = tail call i8** @realloc(i8** %43, i32 %41)
store i8** %44, i8*** %2
%45 = load i8**, i8*** %1
%46 = zext i32 %24 to i64
%47 = getelementptr inbounds i8*, i8** %45, i64 %46
store i8* %27, i8** %47
%48 = load i8**, i8*** %2
%49 = getelementptr inbounds i8*, i8** %48, i64 %46
store i8* %28, i8** %49
br label %53
50: ; preds = %35, %32, %23
%51 = tail call i32 @free(i8* %27)
%52 = tail call i32 @free(i8* %28)
br label %53
53: ; preds = %50, %38
%54 = phi i32 [ %39, %38 ], [ %24, %50 ]
%55 = tail call i32 @free(i8* %26)
%56 = tail call i32 @free(i8* %25)
%57 = tail call i32 @feof(i32* %4)
%58 = icmp eq i32 %57, 0
br i1 %58, label %23, label %59
59: ; preds = %53, %20
%60 = phi i32 [ 0, %20 ], [ %54, %53 ]
%61 = tail call i32 @fclose(i32* %4)
ret i32 %60
}