#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*/ uint8_t;
typedef int uint64_t;
struct t_safeu_struct {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ RAND_bytes(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ abort();
char *calloc(int, int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ printf(char *, int, int, int);
int /*<<< orphan*/ read_file(char const *, char **, unsigned int *);
int /*<<< orphan*/ safeu_decrypt_a_file(struct t_safeu_struct *, char const *,
                                        char const *);
int /*<<< orphan*/ safeu_encrypt_a_file(struct t_safeu_struct *, char const *,
                                        char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char const *);
int /*<<< orphan*/ write_file(char const *, char *, int);
void test_file_encryption(struct t_safeu_struct *ac, uint64_t size) {
  const char *test1 = "test1.tmp";
  const char *test2 = "test2.tmp";
  const char *test3 = "test3.tmp";
  char *plaintext = calloc(1, size + 1);
  char *check = NULL;
  unsigned check_size = 0;
  printf("test file with size %d: %d %d\n", (int)size, (int)size % ((128) / 8),
         (int)size - (1 << 16));
  if (!plaintext) {
    abort();
  }
  RAND_bytes((uint8_t *)plaintext, size);
  if (!write_file(test1, plaintext, size)) {
    abort();
  }
  if (!safeu_encrypt_a_file(ac, test1, test2)) {
    abort();
  }
  if (!safeu_decrypt_a_file(ac, test2, test3)) {
    abort();
  }
  if (!read_file(test3, &check, &check_size)) {
    abort();
  }
  if (check_size != size) {
    fputs("safeu: "
          "readback size error\n",
          stderr);
    abort();
  }
  if (memcmp(check, plaintext, size) != 0) {
    fputs("safeu: "
          "readback data error\n",
          stderr);
    abort();
  }
  free(plaintext);
  free(check);
  unlink(test1);
  unlink(test2);
  unlink(test3);
}


x86 assembly (clang, -O0):
<test_file_encryption>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe2a(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe29(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe28(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   10b0 <calloc@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    -0xc(%rbp),%ecx
sub    $0x10000,%ecx
lea    0xded(%rip),%rdi
call   1070 <printf@plt>
cmpq   $0x0,-0x30(%rbp)
jne    1248 <test_file_encryption+0x88>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <RAND_bytes@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1080 <write_file@plt>
cmp    $0x0,%eax
jne    1274 <test_file_encryption+0xb4>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   10e0 <safeu_encrypt_a_file@plt>
cmp    $0x0,%eax
jne    1295 <test_file_encryption+0xd5>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   10d0 <safeu_decrypt_a_file@plt>
cmp    $0x0,%eax
jne    12b6 <test_file_encryption+0xf6>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10c0 <read_file@plt>
cmp    $0x0,%eax
jne    12d7 <test_file_encryption+0x117>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x3c(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     12ff <test_file_encryption+0x13f>
mov    0x2cf6(%rip),%rax
mov    (%rax),%esi
lea    0xd4a(%rip),%rdi
call   1090 <fputs@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10a0 <memcmp@plt>
cmp    $0x0,%rax
je     1335 <test_file_encryption+0x175>
mov    0x2cc0(%rip),%rax
mov    (%rax),%esi
lea    0xd30(%rip),%rdi
call   1090 <fputs@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <unlink@plt>
mov    -0x20(%rbp),%rdi
call   1050 <unlink@plt>
mov    -0x28(%rbp),%rdi
call   1050 <unlink@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_file_encryption>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x1(%rbx),%esi
mov    $0x1,%edi
call   10b0 <calloc@plt>
mov    %rax,%r14
movq   $0x0,0x8(%rsp)
lea    0xf(%rbx),%eax
test   %ebx,%ebx
cmovns %ebx,%eax
movl   $0x0,0x4(%rsp)
and    $0xfffffff0,%eax
mov    %ebx,%edx
sub    %eax,%edx
lea    -0x10000(%rbx),%ecx
lea    0xe13(%rip),%rdi
mov    %ebx,%esi
call   1070 <printf@plt>
test   %r14,%r14
je     12ea <test_file_encryption+0x12a>
mov    %r14,%rdi
mov    %ebx,%esi
call   1060 <RAND_bytes@plt>
lea    0xdd4(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1080 <write_file@plt>
test   %eax,%eax
je     12ea <test_file_encryption+0x12a>
lea    0xdbb(%rip),%rsi
lea    0xdbe(%rip),%rdx
mov    %r15,%rdi
call   10e0 <safeu_encrypt_a_file@plt>
test   %eax,%eax
je     12ea <test_file_encryption+0x12a>
lea    0xda7(%rip),%rsi
lea    0xdaa(%rip),%rdx
mov    %r15,%rdi
call   10d0 <safeu_decrypt_a_file@plt>
test   %eax,%eax
je     12ea <test_file_encryption+0x12a>
lea    0xd97(%rip),%rdi
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   10c0 <read_file@plt>
test   %eax,%eax
je     12ea <test_file_encryption+0x12a>
cmp    %ebx,0x4(%rsp)
jne    12f1 <test_file_encryption+0x131>
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   10a0 <memcmp@plt>
test   %rax,%rax
jne    1303 <test_file_encryption+0x143>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
lea    0xd3d(%rip),%rdi
call   1050 <unlink@plt>
lea    0xd3b(%rip),%rdi
call   1050 <unlink@plt>
lea    0xd39(%rip),%rdi
call   1050 <unlink@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1040 <abort@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%esi
lea    0xd3c(%rip),%rdi
jmp    1313 <test_file_encryption+0x153>
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
lea    0xd46(%rip),%rdi
call   1090 <fputs@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
%struct.t_safeu_struct = type { i32 }
@.str = [10 x i8] c"test1.tmp\00"
@.str.1 = [10 x i8] c"test2.tmp\00"
@.str.2 = [10 x i8] c"test3.tmp\00"
@.str.3 = [31 x i8] c"test file with size %d: %d %d\0A\00"
@.str.4 = [28 x i8] c"safeu: readback size error\0A\00"
@stderr = global i32 0
@.str.5 = [28 x i8] c"safeu: readback data error\0A\00"
define void @test_file_encryption(%struct.t_safeu_struct* %0, i32 %1) {
%3 = alloca %struct.t_safeu_struct*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store %struct.t_safeu_struct* %0, %struct.t_safeu_struct** %3
store i32 %1, i32* %4
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %7
%11 = load i32, i32* %4
%12 = add nsw i32 %11, 1
%13 = call i8* @calloc(i32 1, i32 %12)
store i8* %13, i8** %8
store i8* null, i8** %9
store i32 0, i32* %10
%14 = load i32, i32* %4
%15 = load i32, i32* %4
%16 = srem i32 %15, 16
%17 = load i32, i32* %4
%18 = sub nsw i32 %17, 65536
%19 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %14, i32 %16, i32 %18)
%20 = load i8*, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %2
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %2
%25 = load i8*, i8** %8
%26 = bitcast i8* %25 to i32*
%27 = load i32, i32* %4
%28 = call i32 @RAND_bytes(i32* %26, i32 %27)
%29 = load i8*, i8** %5
%30 = load i8*, i8** %8
%31 = load i32, i32* %4
%32 = call i32 @write_file(i8* %29, i8* %30, i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %24
%35 = call i32 (...) @abort()
unreachable
36: ; preds = %24
%37 = load %struct.t_safeu_struct*, %struct.t_safeu_struct** %3
%38 = load i8*, i8** %5
%39 = load i8*, i8** %6
%40 = call i32 @safeu_encrypt_a_file(%struct.t_safeu_struct* %37, i8* %38, i8* %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %36
%43 = call i32 (...) @abort()
unreachable
44: ; preds = %36
%45 = load %struct.t_safeu_struct*, %struct.t_safeu_struct** %3
%46 = load i8*, i8** %6
%47 = load i8*, i8** %7
%48 = call i32 @safeu_decrypt_a_file(%struct.t_safeu_struct* %45, i8* %46, i8* %47)
%49 = icmp ne i32 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %44
%51 = call i32 (...) @abort()
unreachable
52: ; preds = %44
%53 = load i8*, i8** %7
%54 = call i32 @read_file(i8* %53, i8** %9, i32* %10)
%55 = icmp ne i32 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %52
%57 = call i32 (...) @abort()
unreachable
58: ; preds = %52
%59 = load i32, i32* %10
%60 = load i32, i32* %4
%61 = icmp ne i32 %59, %60
br i1 %61, label %62, label %66
62: ; preds = %58
%63 = load i32, i32* @stderr
%64 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %63)
%65 = call i32 (...) @abort()
unreachable
66: ; preds = %58
%67 = load i8*, i8** %9
%68 = load i8*, i8** %8
%69 = load i32, i32* %4
%70 = call i64 @memcmp(i8* %67, i8* %68, i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %66
%73 = load i32, i32* @stderr
%74 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %73)
%75 = call i32 (...) @abort()
unreachable
76: ; preds = %66
%77 = load i8*, i8** %8
%78 = call i32 @free(i8* %77)
%79 = load i8*, i8** %9
%80 = call i32 @free(i8* %79)
%81 = load i8*, i8** %5
%82 = call i32 @unlink(i8* %81)
%83 = load i8*, i8** %6
%84 = call i32 @unlink(i8* %83)
%85 = load i8*, i8** %7
%86 = call i32 @unlink(i8* %85)
ret void
}

LLVM IR (-O2):
%struct.t_safeu_struct = type { i32 }
@.str = [10 x i8] c"test1.tmp\00"
@.str.1 = [10 x i8] c"test2.tmp\00"
@.str.2 = [10 x i8] c"test3.tmp\00"
@.str.3 = [31 x i8] c"test file with size %d: %d %d\0A\00"
@.str.4 = [28 x i8] c"safeu: readback size error\0A\00"
@stderr = global i32 0
@.str.5 = [28 x i8] c"safeu: readback data error\0A\00"
define void @test_file_encryption(%struct.t_safeu_struct* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = add nsw i32 %1, 1
%6 = tail call i8* @calloc(i32 1, i32 %5)
%7 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%9 = srem i32 %1, 16
%10 = add nsw i32 %1, -65536
%11 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %1, i32 %9, i32 %10)
%12 = icmp eq i8* %6, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 (...) @abort()
unreachable
15: ; preds = %2
%16 = bitcast i8* %6 to i32*
%17 = tail call i32 @RAND_bytes(i32* %16, i32 %1)
%18 = tail call i32 @write_file(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %6, i32 %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 (...) @abort()
unreachable
22: ; preds = %15
%23 = tail call i32 @safeu_encrypt_a_file(%struct.t_safeu_struct* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 (...) @abort()
unreachable
27: ; preds = %22
%28 = tail call i32 @safeu_decrypt_a_file(%struct.t_safeu_struct* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 (...) @abort()
unreachable
32: ; preds = %27
%33 = call i32 @read_file(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %3, i32* %4)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (...) @abort()
unreachable
37: ; preds = %32
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, %1
br i1 %39, label %44, label %40
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = call i32 (...) @abort()
unreachable
44: ; preds = %37
%45 = load i8*, i8** %3
%46 = call i64 @memcmp(i8* %45, i8* %6, i32 %1)
%47 = icmp eq i64 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %49)
%51 = call i32 (...) @abort()
unreachable
52: ; preds = %44
%53 = call i32 @free(i8* %6)
%54 = load i8*, i8** %3
%55 = call i32 @free(i8* %54)
%56 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%57 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%58 = call i32 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
ret void
}