#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*/ FILE;

/* Variables and functions */
char *crypt(char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
scalar_t__ strtok(char *, char *);
int valid_user_p(char *username, char *passwd) {
  FILE *fd;
  char line[256];
  if ((fd = fopen("/etc/nh-login", "r")) == NULL)
    exit(100);
  while (fgets(line, 256, fd) != NULL) {
    char *n;
    char *p;
    n = (char *)strtok(line, ",");
    p = (char *)strtok(NULL, "\n");
    if (!strcmp(n, username)) {
      fclose(fd);
      if (!strcmp(crypt(passwd, passwd), p))
        return 2;
      else
        return 1;
    }
  }
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<valid_user_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe76(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ae <valid_user_p+0x3e>
mov    $0x64,%edi
call   1090 <exit@plt>
jmp    11b3 <valid_user_p+0x43>
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1267 <valid_user_p+0xf7>
lea    -0x120(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x128(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe1b(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x130(%rbp)
mov    -0x128(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1262 <valid_user_p+0xf2>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <crypt@plt>
mov    %rax,%rdi
mov    -0x130(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1256 <valid_user_p+0xe6>
movl   $0x2,-0x4(%rbp)
jmp    1277 <valid_user_p+0x107>
movl   $0x1,-0x4(%rbp)
jmp    1277 <valid_user_p+0x107>
jmp    11b3 <valid_user_p+0x43>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_user_p>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r15
lea    0xe70(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     124c <valid_user_p+0xdc>
mov    %rax,%rbx
lea    0x10(%rsp),%r12
lea    0xe5e(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1230 <valid_user_p+0xc0>
mov    %r12,%rdi
lea    0xe31(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,%r13
xor    %edi,%edi
mov    %rbp,%rsi
call   1080 <strtok@plt>
mov    %rax,%r14
mov    %r13,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    11c0 <valid_user_p+0x50>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdi
mov    %rdi,%rsi
call   1030 <crypt@plt>
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
cmp    $0x1,%eax
mov    $0x1,%eax
adc    $0x0,%eax
jmp    123a <valid_user_p+0xca>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x64,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/etc/nh-login\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c",\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @valid_user_p(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @exit(i32 100)
unreachable
14: ; preds = %2
br label %15
15: ; preds = %41, %14
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%17 = load i32*, i32** %6
%18 = call i32* @fgets(i8* %16, i32 256, i32* %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %42
20: ; preds = %15
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%22 = call i64 @strtok(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %8
%24 = call i64 @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %9
%26 = load i8*, i8** %8
%27 = load i8*, i8** %4
%28 = call i32 @strcmp(i8* %26, i8* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %20
%31 = load i32*, i32** %6
%32 = call i32 @fclose(i32* %31)
%33 = load i8*, i8** %5
%34 = load i8*, i8** %5
%35 = call i8* @crypt(i8* %33, i8* %34)
%36 = load i8*, i8** %9
%37 = call i32 @strcmp(i8* %35, i8* %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %30
store i32 2, i32* %3
br label %45
40: ; preds = %30
store i32 1, i32* %3
br label %45
41: ; preds = %20
br label %15
42: ; preds = %15
%43 = load i32*, i32** %6
%44 = call i32 @fclose(i32* %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %40, %39
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [14 x i8] c"/etc/nh-login\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c",\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @valid_user_p(i8* nocapture %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @exit(i32 100)
unreachable
9: ; preds = %2, %12
%10 = call i32* @fgets(i8* %4, i32 256, i32* %5)
%11 = icmp eq i32* %10, null
br i1 %11, label %25, label %12
12: ; preds = %9
%13 = call i64 @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = inttoptr i64 %13 to i8*
%15 = call i64 @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @strcmp(i8* dereferenceable(1) %14, i8* dereferenceable(1) %0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %9
18: ; preds = %12
%19 = inttoptr i64 %15 to i8*
%20 = call i32 @fclose(i32* %5)
%21 = call i8* @crypt(i8* %1, i8* %1)
%22 = call i32 @strcmp(i8* dereferenceable(1) %21, i8* dereferenceable(1) %19)
%23 = icmp eq i32 %22, 0
%24 = select i1 %23, i32 2, i32 1
br label %27
25: ; preds = %9
%26 = call i32 @fclose(i32* %5)
br label %27
27: ; preds = %18, %25
%28 = phi i32 [ 0, %25 ], [ %24, %18 ]
ret i32 %28
}