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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *, char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
char *password_of(char *user) {
  int len;
  int id;
  char name[32];
  char username[32];
  char password[32];
  char line[4 * 32];
  char *res;
  char *db_filename;
  FILE *file;
  db_filename = "db";
  file = fopen(db_filename, "r");
  if (!file)
    return NULL;
  res = NULL;
  fgets(line, sizeof(line), file);
  while (fgets(line, sizeof(line), file) != NULL) {
    sscanf(line, "%d\t%s\t%s\t%s\n", &id, name, username, password);
    if (!strncmp(username, user, 32)) {
      len = strlen(password);
      res = malloc(len);
      memcpy(res, password, len);
      break;
    }
  }
  fclose(file);
  return res != NULL ? res : "";
}


x86 assembly (clang, -O0):
<password_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6a(%rip),%rax
mov    %rax,-0x110(%rbp)
mov    -0x110(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    11d2 <password_of+0x52>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <password_of+0x158>
movq   $0x0,-0x108(%rbp)
lea    -0x100(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
lea    -0x100(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     128d <password_of+0x10d>
lea    -0x100(%rbp),%rdi
lea    -0x40(%rbp),%rcx
lea    -0x60(%rbp),%r8
lea    -0x80(%rbp),%r9
lea    0xdd4(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1090 <sscanf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x20,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1288 <password_of+0x108>
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x108(%rbp)
mov    -0x108(%rbp),%rdi
lea    -0x80(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <memcpy@plt>
jmp    128d <password_of+0x10d>
jmp    11f5 <password_of+0x75>
mov    -0x118(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x108(%rbp)
je     12ba <password_of+0x13a>
mov    -0x108(%rbp),%rax
mov    %rax,-0x120(%rbp)
jmp    12cd <password_of+0x14d>
lea    0xd50(%rip),%rax
mov    %rax,-0x120(%rbp)
jmp    12cd <password_of+0x14d>
mov    -0x120(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<password_of>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xf8,%rsp
mov    %rdi,%r15
lea    0xe65(%rip),%rdi
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     124e <password_of+0xce>
mov    %rax,%r12
lea    0x70(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
lea    0x50(%rsp),%r14
lea    0x30(%rsp),%r13
lea    0x10(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1252 <password_of+0xd2>
mov    %rbx,%rdi
lea    0xe06(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %r14,%rcx
mov    %r13,%r8
mov    %rbp,%r9
call   1090 <sscanf@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    $0x20,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11e0 <password_of+0x60>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
jmp    1254 <password_of+0xd4>
xor    %eax,%eax
jmp    126a <password_of+0xea>
xor    %ebx,%ebx
mov    %r12,%rdi
call   1040 <fclose@plt>
test   %rbx,%rbx
lea    0xdab(%rip),%rax
cmovne %rbx,%rax
add    $0xf8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"db\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"%d\09%s\09%s\09%s\0A\00"
@.str.3 = [1 x i8] zeroinitializer
define i8* @password_of(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = alloca [32 x i8]
%8 = alloca [32 x i8]
%9 = alloca [128 x i8]
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32*
store i8* %0, i8** %3
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %11
%13 = load i8*, i8** %11
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %12
%15 = load i32*, i32** %12
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %1
store i8* null, i8** %2
br label %57
18: ; preds = %1
store i8* null, i8** %10
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%20 = load i32*, i32** %12
%21 = call i32* @fgets(i8* %19, i32 128, i32* %20)
br label %22
22: ; preds = %46, %18
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%24 = load i32*, i32** %12
%25 = call i32* @fgets(i8* %23, i32 128, i32* %24)
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %47
27: ; preds = %22
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%32 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32* %5, i8* %29, i8* %30, i8* %31)
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%34 = load i8*, i8** %3
%35 = call i32 @strncmp(i8* %33, i8* %34, i32 32)
%36 = icmp ne i32 %35, 0
br i1 %36, label %46, label %37
37: ; preds = %27
%38 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
store i32 %39, i32* %4
%40 = load i32, i32* %4
%41 = call i8* @malloc(i32 %40)
store i8* %41, i8** %10
%42 = load i8*, i8** %10
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%44 = load i32, i32* %4
%45 = call i32 @memcpy(i8* %42, i8* %43, i32 %44)
br label %47
46: ; preds = %27
br label %22
47: ; preds = %37, %22
%48 = load i32*, i32** %12
%49 = call i32 @fclose(i32* %48)
%50 = load i8*, i8** %10
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = load i8*, i8** %10
br label %55
54: ; preds = %47
br label %55
55: ; preds = %54, %52
%56 = phi i8* [ %53, %52 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %54 ]
store i8* %56, i8** %2
br label %57
57: ; preds = %55, %17
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@.str = [3 x i8] c"db\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [13 x i8] c"%d\09%s\09%s\09%s\0A\00"
@.str.3 = [1 x i8] zeroinitializer
define i8* @password_of(i8* %0) {
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = alloca [32 x i8]
%5 = alloca [32 x i8]
%6 = alloca [128 x i8]
%7 = bitcast i32* %2 to i8*
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %32, label %14
14: ; preds = %1
%15 = call i32* @fgets(i8* %11, i32 128, i32* %12)
br label %16
16: ; preds = %19, %14
%17 = call i32* @fgets(i8* %11, i32 128, i32* %12)
%18 = icmp eq i32* %17, null
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32* %2, i8* %8, i8* %9, i8* %10)
%21 = call i32 @strncmp(i8* %9, i8* %0, i32 32)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %16
23: ; preds = %19
%24 = call i32 @strlen(i8* %10)
%25 = call i8* @malloc(i32 %24)
%26 = call i32 @memcpy(i8* %25, i8* %10, i32 %24)
br label %27
27: ; preds = %16, %23
%28 = phi i8* [ %25, %23 ], [ null, %16 ]
%29 = call i32 @fclose(i32* %12)
%30 = icmp eq i8* %28, null
%31 = select i1 %30, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* %28
br label %32
32: ; preds = %1, %27
%33 = phi i8* [ %31, %27 ], [ null, %1 ]
ret i8* %33
}