#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 */
struct crypt_device {
  int dummy;
};

/* Variables and functions */
scalar_t__ hash(int /*<<< orphan*/ *, char const *, char *, size_t,
                char const *, size_t);
int /*<<< orphan*/ logger(struct crypt_device *, int, char *, int, char *, ...);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
char *safe_alloc(size_t);
int /*<<< orphan*/ safe_free(char *);
int /*<<< orphan*/ set_error(char *, ...);
scalar_t__ strcmp(char const *, char *);
char *process_key(struct crypt_device *cd, const char *hash_name,
                  const char *key_file, size_t key_size, const char *pass,
                  size_t passLen) {
  char *key = safe_alloc(key_size);
  memset(key, 0, key_size);
  if (key_file && strcmp(key_file, "-")) {
    if (passLen < key_size) {
      do {
        logger(
            cd, 1,
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/SteamMOD/"
            "android_bootable_steam_cryptsetup/refs/heads/master/lib/setup.c",
            130, ("Cannot not read %d bytes from key file %s.\n"), key_size,
            key_file);
        set_error(("Cannot not read %d bytes from key file %s.\n"), key_size,
                  key_file);
      } while (0);
      safe_free(key);
      return NULL;
    }
    memcpy(key, pass, key_size);
    return key;
  }
  if (hash_name) {
    if (hash(NULL, hash_name, key, key_size, pass, passLen) < 0) {
      do {
        logger(
            cd, 1,
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/SteamMOD/"
            "android_bootable_steam_cryptsetup/refs/heads/master/lib/setup.c",
            142, ("Key processing error (using hash algorithm %s).\n"),
            hash_name);
        set_error(("Key processing error (using hash algorithm %s).\n"),
                  hash_name);
      } while (0);
      safe_free(key);
      return NULL;
    }
  } else if (passLen > key_size) {
    memcpy(key, pass, key_size);
  } else {
    memcpy(key, pass, passLen);
  }
  return key;
}


x86 assembly (clang, -O0):
<process_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <safe_alloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
cmpq   $0x0,-0x20(%rbp)
je     126d <process_key+0xed>
mov    -0x20(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     126d <process_key+0xed>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    124f <process_key+0xcf>
jmp    11f4 <process_key+0x74>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%r9
mov    -0x20(%rbp),%rax
mov    $0x1,%esi
lea    0xdf6(%rip),%rdx
mov    $0x82,%ecx
lea    0xe8d(%rip),%r8
mov    %rax,(%rsp)
mov    $0x0,%al
call   1080 <logger@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1050 <set_error@plt>
mov    -0x40(%rbp),%rdi
call   10a0 <safe_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    133a <process_key+0x1ba>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    133a <process_key+0x1ba>
cmpq   $0x0,-0x18(%rbp)
je     12f8 <process_key+0x178>
mov    -0x18(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r9
xor    %eax,%eax
mov    %eax,%edi
call   1030 <hash@plt>
cmp    $0x0,%rax
jge    12f3 <process_key+0x173>
jmp    12a4 <process_key+0x124>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%r9
mov    $0x1,%esi
lea    0xd4a(%rip),%rdx
mov    $0x8e,%ecx
lea    0xe0d(%rip),%r8
mov    $0x0,%al
call   1080 <logger@plt>
mov    -0x18(%rbp),%rsi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1050 <set_error@plt>
mov    -0x40(%rbp),%rdi
call   10a0 <safe_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    133a <process_key+0x1ba>
jmp    1332 <process_key+0x1b2>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    131c <process_key+0x19c>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1090 <memcpy@plt>
jmp    132d <process_key+0x1ad>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1090 <memcpy@plt>
jmp    1332 <process_key+0x1b2>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r15
mov    %r8,%r13
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,0x10(%rsp)
mov    %rcx,%rdi
call   1060 <safe_alloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
test   %rbp,%rbp
je     121c <process_key+0x9c>
lea    0xe3a(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     121c <process_key+0x9c>
cmp    %rbx,%r15
jae    1285 <process_key+0x105>
mov    %rbp,(%rsp)
lea    0xe1b(%rip),%rdx
lea    0xeb7(%rip),%r15
mov    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x82,%ecx
mov    %r15,%r8
mov    %rbx,%r9
xor    %eax,%eax
call   1080 <logger@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <set_error@plt>
jmp    1273 <process_key+0xf3>
test   %r12,%r12
je     1280 <process_key+0x100>
xor    %edi,%edi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %r13,%r8
mov    %r15,%r9
call   1030 <hash@plt>
test   %rax,%rax
jns    129e <process_key+0x11e>
lea    0xdbf(%rip),%rdx
lea    0xe87(%rip),%rbx
mov    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x8e,%ecx
mov    %rbx,%r8
mov    %r12,%r9
xor    %eax,%eax
call   1080 <logger@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <set_error@plt>
mov    %r14,%rdi
call   10a0 <safe_free@plt>
xor    %r14d,%r14d
jmp    129e <process_key+0x11e>
cmp    %rbx,%r15
jbe    1290 <process_key+0x110>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
jmp    1299 <process_key+0x119>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1090 <memcpy@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.crypt_device = type { i32 }
@.str = [2 x i8] c"-\00"
@.str.1 = [163 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/SteamMOD/android_bootable_steam_cryptsetup/refs/heads/master/lib/setup.c\00"
@.str.2 = [44 x i8] c"Cannot not read %d bytes from key file %s.\0A\00"
@.str.3 = [49 x i8] c"Key processing error (using hash algorithm %s).\0A\00"
define i8* @process_key(%struct.crypt_device* %0, i8* %1, i8* %2, i64 %3, i8* %4, i64 %5) {
%7 = alloca i8*
%8 = alloca %struct.crypt_device*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
store %struct.crypt_device* %0, %struct.crypt_device** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i64 %3, i64* %11
store i8* %4, i8** %12
store i64 %5, i64* %13
%15 = load i64, i64* %11
%16 = call i8* @safe_alloc(i64 %15)
store i8* %16, i8** %14
%17 = load i8*, i8** %14
%18 = load i64, i64* %11
%19 = call i32 @memset(i8* %17, i32 0, i64 %18)
%20 = load i8*, i8** %10
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %48
22: ; preds = %6
%23 = load i8*, i8** %10
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %48
26: ; preds = %22
%27 = load i64, i64* %13
%28 = load i64, i64* %11
%29 = icmp ult i64 %27, %28
br i1 %29, label %30, label %42
30: ; preds = %26
br label %31
31: ; preds = %30
%32 = load %struct.crypt_device*, %struct.crypt_device** %8
%33 = load i64, i64* %11
%34 = load i8*, i8** %10
%35 = call i32 (%struct.crypt_device*, i32, i8*, i32, i8*, ...) @logger(%struct.crypt_device* %32, i32 1, i8* getelementptr inbounds ([163 x i8], [163 x i8]* @.str.1, i64 0, i64 0), i32 130, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i64 %33, i8* %34)
%36 = load i64, i64* %11
%37 = load i8*, i8** %10
%38 = call i32 (i8*, ...) @set_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i64 %36, i8* %37)
br label %39
39: ; preds = %31
%40 = load i8*, i8** %14
%41 = call i32 @safe_free(i8* %40)
store i8* null, i8** %7
br label %87
42: ; preds = %26
%43 = load i8*, i8** %14
%44 = load i8*, i8** %12
%45 = load i64, i64* %11
%46 = call i32 @memcpy(i8* %43, i8* %44, i64 %45)
%47 = load i8*, i8** %14
store i8* %47, i8** %7
br label %87
48: ; preds = %22, %6
%49 = load i8*, i8** %9
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %70
51: ; preds = %48
%52 = load i8*, i8** %9
%53 = load i8*, i8** %14
%54 = load i64, i64* %11
%55 = load i8*, i8** %12
%56 = load i64, i64* %13
%57 = call i64 @hash(i32* null, i8* %52, i8* %53, i64 %54, i8* %55, i64 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %69
59: ; preds = %51
br label %60
60: ; preds = %59
%61 = load %struct.crypt_device*, %struct.crypt_device** %8
%62 = load i8*, i8** %9
%63 = call i32 (%struct.crypt_device*, i32, i8*, i32, i8*, ...) @logger(%struct.crypt_device* %61, i32 1, i8* getelementptr inbounds ([163 x i8], [163 x i8]* @.str.1, i64 0, i64 0), i32 142, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %62)
%64 = load i8*, i8** %9
%65 = call i32 (i8*, ...) @set_error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %60
%67 = load i8*, i8** %14
%68 = call i32 @safe_free(i8* %67)
store i8* null, i8** %7
br label %87
69: ; preds = %51
br label %85
70: ; preds = %48
%71 = load i64, i64* %13
%72 = load i64, i64* %11
%73 = icmp ugt i64 %71, %72
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i8*, i8** %14
%76 = load i8*, i8** %12
%77 = load i64, i64* %11
%78 = call i32 @memcpy(i8* %75, i8* %76, i64 %77)
br label %84
79: ; preds = %70
%80 = load i8*, i8** %14
%81 = load i8*, i8** %12
%82 = load i64, i64* %13
%83 = call i32 @memcpy(i8* %80, i8* %81, i64 %82)
br label %84
84: ; preds = %79, %74
br label %85
85: ; preds = %84, %69
%86 = load i8*, i8** %14
store i8* %86, i8** %7
br label %87
87: ; preds = %85, %66, %42, %39
%88 = load i8*, i8** %7
ret i8* %88
}

LLVM IR (-O2):
%struct.crypt_device = type { i32 }
@.str = [2 x i8] c"-\00"
@.str.1 = [163 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/SteamMOD/android_bootable_steam_cryptsetup/refs/heads/master/lib/setup.c\00"
@.str.2 = [44 x i8] c"Cannot not read %d bytes from key file %s.\0A\00"
@.str.3 = [49 x i8] c"Key processing error (using hash algorithm %s).\0A\00"
define i8* @process_key(%struct.crypt_device* %0, i8* %1, i8* %2, i64 %3, i8* %4, i64 %5) {
%7 = tail call i8* @safe_alloc(i64 %3)
%8 = tail call i32 @memset(i8* %7, i32 0, i64 %3)
%9 = icmp eq i8* %2, null
br i1 %9, label %21, label %10
10: ; preds = %6
%11 = tail call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = icmp ult i64 %5, %3
br i1 %14, label %15, label %19
15: ; preds = %13
%16 = tail call i32 (%struct.crypt_device*, i32, i8*, i32, i8*, ...) @logger(%struct.crypt_device* %0, i32 1, i8* getelementptr inbounds ([163 x i8], [163 x i8]* @.str.1, i64 0, i64 0), i32 130, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i64 %3, i8* %2)
%17 = tail call i32 (i8*, ...) @set_error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i64 %3, i8* %2)
%18 = tail call i32 @safe_free(i8* %7)
br label %36
19: ; preds = %13
%20 = tail call i32 @memcpy(i8* %7, i8* %4, i64 %3)
br label %36
21: ; preds = %10, %6
%22 = icmp eq i8* %1, null
br i1 %22, label %30, label %23
23: ; preds = %21
%24 = tail call i64 @hash(i32* null, i8* %1, i8* %7, i64 %3, i8* %4, i64 %5)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %23
%27 = tail call i32 (%struct.crypt_device*, i32, i8*, i32, i8*, ...) @logger(%struct.crypt_device* %0, i32 1, i8* getelementptr inbounds ([163 x i8], [163 x i8]* @.str.1, i64 0, i64 0), i32 142, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%28 = tail call i32 (i8*, ...) @set_error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%29 = tail call i32 @safe_free(i8* %7)
br label %36
30: ; preds = %21
%31 = icmp ugt i64 %5, %3
br i1 %31, label %32, label %34
32: ; preds = %30
%33 = tail call i32 @memcpy(i8* %7, i8* %4, i64 %3)
br label %36
34: ; preds = %30
%35 = tail call i32 @memcpy(i8* %7, i8* %4, i64 %5)
br label %36
36: ; preds = %23, %34, %32, %26, %19, %15
%37 = phi i8* [ null, %15 ], [ %7, %19 ], [ null, %26 ], [ %7, %32 ], [ %7, %34 ], [ %7, %23 ]
ret i8* %37
}