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

/* Variables and functions */
scalar_t__ EOF;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ sscanf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int shadowpass(char *user, char **passwd) {
  char buf[2048], u[256], p[256];
  size_t bs = sizeof(buf);
  FILE *f;
  errno = 0;
  if ((f = fopen("/etc/shadow", "r")) == NULL) {
    return -errno;
  }
  while (fgets(buf, bs, f) != NULL) {
    if (sscanf(buf,
               "%[^:]:%[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]",
               u, p) != EOF) {
      if (strcmp(u, user) == 0) {
        strncpy(*passwd, p, sizeof(p));
        break;
      }
    }
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<shadowpass>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x800,-0xa28(%rbp)
mov    0x2e53(%rip),%rax
movl   $0x0,(%rax)
lea    0xe6e(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xa30(%rbp)
cmp    $0x0,%rax
jne    11c2 <shadowpass+0x62>
xor    %eax,%eax
mov    0x2e20(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1270 <shadowpass+0x110>
jmp    11c7 <shadowpass+0x67>
lea    -0x820(%rbp),%rdi
mov    -0xa28(%rbp),%rsi
mov    -0xa30(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     125d <shadowpass+0xfd>
lea    -0x820(%rbp),%rdi
lea    -0x920(%rbp),%rdx
lea    -0xa20(%rbp),%rcx
lea    0xe07(%rip),%rsi
call   1070 <sscanf@plt>
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
je     1258 <shadowpass+0xf8>
lea    -0x920(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1253 <shadowpass+0xf3>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    -0xa20(%rbp),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
jmp    125d <shadowpass+0xfd>
jmp    1258 <shadowpass+0xf8>
jmp    11c7 <shadowpass+0x67>
mov    -0xa30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shadowpass>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa18,%rsp
mov    %rsi,%rbp
mov    %rdi,0x8(%rsp)
mov    0x2e58(%rip),%rbx
movl   $0x0,(%rbx)
lea    0xe73(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     124f <shadowpass+0xef>
mov    %rax,%r13
mov    %rbp,(%rsp)
lea    0x210(%rsp),%rdi
mov    $0x800,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1243 <shadowpass+0xe3>
lea    0xe40(%rip),%r12
lea    0x210(%rsp),%rbx
lea    0x110(%rsp),%rbp
lea    0x10(%rsp),%r14
mov    0x2dfe(%rip),%r15
jmp    1205 <shadowpass+0xa5>
nopl   0x0(%rax)
mov    $0x800,%esi
mov    %rbx,%rdi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1243 <shadowpass+0xe3>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
call   1070 <sscanf@plt>
cmp    (%r15),%rax
je     11f0 <shadowpass+0x90>
mov    %rbp,%rdi
mov    0x8(%rsp),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11f0 <shadowpass+0x90>
mov    (%rsp),%rax
mov    (%rax),%rdi
lea    0x10(%rsp),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1253 <shadowpass+0xf3>
xor    %eax,%eax
sub    (%rbx),%eax
add    $0xa18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@.str = [12 x i8] c"/etc/shadow\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [61 x i8] c"%[^:]:%[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]:%*[^:]\00"
@EOF = global i64 0
define i32 @shadowpass(i8* %0, i8** nocapture %1) {
%3 = alloca [2048 x i8]
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
store i32 0, i32* @errno
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = call i32* @fgets(i8* %6, i64 2048, i32* %9)
%13 = icmp eq i32* %12, null
br i1 %13, label %30, label %17
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
br label %32
17: ; preds = %11, %27
%18 = call i64 @sscanf(i8* %6, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i8* %7, i8* %8)
%19 = load i64, i64* @EOF
%20 = icmp eq i64 %18, %19
br i1 %20, label %27, label %21
21: ; preds = %17
%22 = call i64 @strcmp(i8* %7, i8* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i8*, i8** %1
%26 = call i32 @strncpy(i8* %25, i8* %8, i32 256)
br label %30
27: ; preds = %21, %17
%28 = call i32* @fgets(i8* %6, i64 2048, i32* %9)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %17
30: ; preds = %27, %11, %24
%31 = call i32 @fclose(i32* %9)
br label %32
32: ; preds = %30, %14
%33 = phi i32 [ %16, %14 ], [ 0, %30 ]
ret i32 %33
}