#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 *b16encode(char *, int);
int encryptSymmetricKey(char **, int, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
bool makeTicketFile(const char *keyiv, const char *directory) {
  int keyid = 1;
  char *encrypted_key;
  int encrypted_key_size = encryptSymmetricKey(&encrypted_key, keyid, keyiv);
  if (encrypted_key_size == -1) {
    fprintf(stderr, "Error: makeTicketFile: encryptSymmetricKey failed\n");
    return 0;
  }
  char *raw_ticket = (char *)malloc(2 + encrypted_key_size);
  raw_ticket[0] = keyid / 256;
  raw_ticket[1] = keyid % 256;
  for (int i = 0; i < encrypted_key_size; i++)
    raw_ticket[i + 2] = encrypted_key[i];
  char *ticket = b16encode(raw_ticket, 2 + encrypted_key_size);
  int ticket_size = (2 + encrypted_key_size) * 2;
  char *ticket_path =
      (char *)malloc(strlen(".osir_ticket") + 1 + strlen(directory));
  strcpy(ticket_path, directory);
  strcat(ticket_path, "/");
  strcat(ticket_path, ".osir_ticket");
  FILE *ticket_fp = fopen(ticket_path, "wb");
  fwrite(ticket, 1, ticket_size, ticket_fp);
  fclose(ticket_fp);
  free(ticket_path);
  free(encrypted_key);
  free(raw_ticket);
  free(ticket);
  return 1;
}


x86 assembly (clang, -O0):
<makeTicketFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rdi
call   10a0 <encryptSymmetricKey@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1205 <makeTicketFile+0x55>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1349 <makeTicketFile+0x199>
mov    -0x2c(%rbp),%edi
add    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %dl,%cl
mov    -0x38(%rbp),%rax
mov    %cl,0x1(%rax)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1277 <makeTicketFile+0xc7>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x38(%rbp),%rax
mov    -0x3c(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1242 <makeTicketFile+0x92>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    $0x2,%esi
call   1080 <b16encode@plt>
mov    %rax,-0x48(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x2,%eax
shl    $0x1,%eax
mov    %eax,-0x4c(%rbp)
lea    0xd96(%rip),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x64(%rbp),%edi
add    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x58(%rbp),%rdi
lea    0xd69(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x58(%rbp),%rdi
lea    0xd4c(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x58(%rbp),%rdi
lea    0xd4b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x60(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x4c(%rbp),%edx
mov    -0x60(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
mov    -0x60(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeTicketFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rdx
mov    %rsp,%rdi
mov    $0x1,%esi
call   10a0 <encryptSymmetricKey@plt>
cmp    $0xffffffff,%eax
je     1206 <makeTicketFile+0x56>
mov    %eax,%ebp
lea    0x2(%rbp),%r12d
mov    %r12d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
movw   $0x100,(%rax)
test   %ebp,%ebp
jle    1296 <makeTicketFile+0xe6>
mov    %ebp,%edi
lea    -0x1(%rdi),%rcx
mov    %edi,%eax
and    $0x3,%eax
cmp    $0x3,%rcx
jae    1223 <makeTicketFile+0x73>
xor    %ecx,%ecx
jmp    126c <makeTicketFile+0xbc>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1070 <fprintf@plt>
xor    %r14d,%r14d
jmp    1347 <makeTicketFile+0x197>
and    $0xfffffffc,%edi
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rsi
movzbl (%rsi,%rcx,1),%edx
mov    %dl,0x2(%rbx,%rcx,1)
mov    (%rsp),%rdx
movzbl 0x1(%rdx,%rcx,1),%edx
mov    %dl,0x3(%rbx,%rcx,1)
mov    (%rsp),%rdx
movzbl 0x2(%rdx,%rcx,1),%edx
mov    %dl,0x4(%rbx,%rcx,1)
mov    (%rsp),%rdx
movzbl 0x3(%rdx,%rcx,1),%edx
mov    %dl,0x5(%rbx,%rcx,1)
add    $0x4,%rcx
cmp    %rcx,%rdi
jne    1230 <makeTicketFile+0x80>
test   %rax,%rax
je     1296 <makeTicketFile+0xe6>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rdx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,0x2(%rbx,%rcx,1)
add    $0x1,%rcx
add    $0xffffffffffffffff,%rax
jne    1280 <makeTicketFile+0xd0>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1080 <b16encode@plt>
mov    %rax,%r15
add    %r12d,%r12d
lea    0xd85(%rip),%r13
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rax,%rbp,1),%edi
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
lea    0xd60(%rip),%rsi
mov    %rbp,%rdi
call   10c0 <strcat@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   10c0 <strcat@plt>
lea    0xd48(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
mov    %rax,%r13
mov    $0x1,%r14d
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [51 x i8] c"Error: makeTicketFile: encryptSymmetricKey failed\0A\00"
@.str.1 = [13 x i8] c".osir_ticket\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [3 x i8] c"wb\00"
define i32 @makeTicketFile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 1, i32* %6
%15 = load i32, i32* %6
%16 = load i8*, i8** %4
%17 = call i32 @encryptSymmetricKey(i8** %7, i32 %15, i8* %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %94
23: ; preds = %2
%24 = load i32, i32* %8
%25 = add nsw i32 2, %24
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %9
%28 = load i32, i32* %6
%29 = sdiv i32 %28, 256
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %9
%32 = getelementptr inbounds i8, i8* %31, i64 0
store i8 %30, i8* %32
%33 = load i32, i32* %6
%34 = srem i32 %33, 256
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %9
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8 %35, i8* %37
store i32 0, i32* %10
br label %38
38: ; preds = %53, %23
%39 = load i32, i32* %10
%40 = load i32, i32* %8
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %56
42: ; preds = %38
%43 = load i8*, i8** %7
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i8, i8* %46
%48 = load i8*, i8** %9
%49 = load i32, i32* %10
%50 = add nsw i32 %49, 2
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8 %47, i8* %52
br label %53
53: ; preds = %42
%54 = load i32, i32* %10
%55 = add nsw i32 %54, 1
store i32 %55, i32* %10
br label %38
56: ; preds = %38
%57 = load i8*, i8** %9
%58 = load i32, i32* %8
%59 = add nsw i32 2, %58
%60 = call i8* @b16encode(i8* %57, i32 %59)
store i8* %60, i8** %11
%61 = load i32, i32* %8
%62 = add nsw i32 2, %61
%63 = mul nsw i32 %62, 2
store i32 %63, i32* %12
%64 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%65 = add nsw i32 %64, 1
%66 = load i8*, i8** %5
%67 = call i32 @strlen(i8* %66)
%68 = add nsw i32 %65, %67
%69 = call i64 @malloc(i32 %68)
%70 = inttoptr i64 %69 to i8*
store i8* %70, i8** %13
%71 = load i8*, i8** %13
%72 = load i8*, i8** %5
%73 = call i32 @strcpy(i8* %71, i8* %72)
%74 = load i8*, i8** %13
%75 = call i32 @strcat(i8* %74, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%76 = load i8*, i8** %13
%77 = call i32 @strcat(i8* %76, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%78 = load i8*, i8** %13
%79 = call i32* @fopen(i8* %78, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %79, i32** %14
%80 = load i8*, i8** %11
%81 = load i32, i32* %12
%82 = load i32*, i32** %14
%83 = call i32 @fwrite(i8* %80, i32 1, i32 %81, i32* %82)
%84 = load i32*, i32** %14
%85 = call i32 @fclose(i32* %84)
%86 = load i8*, i8** %13
%87 = call i32 @free(i8* %86)
%88 = load i8*, i8** %7
%89 = call i32 @free(i8* %88)
%90 = load i8*, i8** %9
%91 = call i32 @free(i8* %90)
%92 = load i8*, i8** %11
%93 = call i32 @free(i8* %92)
store i32 1, i32* %3
br label %94
94: ; preds = %56, %20
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [51 x i8] c"Error: makeTicketFile: encryptSymmetricKey failed\0A\00"
@.str.1 = [13 x i8] c".osir_ticket\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [3 x i8] c"wb\00"
define i32 @makeTicketFile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i32 @encryptSymmetricKey(i8** %3, i32 1, i8* %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %86
10: ; preds = %2
%11 = add nsw i32 %5, 2
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8 0, i8* %13
%14 = getelementptr inbounds i8, i8* %13, i64 1
store i8 1, i8* %14
%15 = icmp sgt i32 %5, 0
br i1 %15, label %16, label %37
16: ; preds = %10
%17 = zext i32 %5 to i64
%18 = add nsw i64 %17, -1
%19 = and i64 %17, 3
%20 = icmp ult i64 %18, 3
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = and i64 %17, 4294967292
br label %57
23: ; preds = %57, %16
%24 = phi i64 [ 0, %16 ], [ %83, %57 ]
%25 = icmp eq i64 %19, 0
br i1 %25, label %37, label %26
26: ; preds = %23, %26
%27 = phi i64 [ %34, %26 ], [ %24, %23 ]
%28 = phi i64 [ %35, %26 ], [ 0, %23 ]
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i64 %27
%31 = load i8, i8* %30
%32 = add nuw nsw i64 %27, 2
%33 = getelementptr inbounds i8, i8* %13, i64 %32
store i8 %31, i8* %33
%34 = add nuw nsw i64 %27, 1
%35 = add i64 %28, 1
%36 = icmp eq i64 %35, %19
br i1 %36, label %37, label %26
37: ; preds = %23, %26, %10
%38 = call i8* @b16encode(i8* %13, i32 %11)
%39 = shl nsw i32 %11, 1
%40 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%41 = add nsw i32 %40, 1
%42 = call i32 @strlen(i8* %1)
%43 = add nsw i32 %41, %42
%44 = call i64 @malloc(i32 %43)
%45 = inttoptr i64 %44 to i8*
%46 = call i32 @strcpy(i8* %45, i8* %1)
%47 = call i32 @strcat(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @strcat(i8* %45, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%49 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @fwrite(i8* %38, i32 1, i32 %39, i32* %49)
%51 = call i32 @fclose(i32* %49)
%52 = call i32 @free(i8* %45)
%53 = load i8*, i8** %3
%54 = call i32 @free(i8* %53)
%55 = call i32 @free(i8* %13)
%56 = call i32 @free(i8* %38)
br label %86
57: ; preds = %57, %21
%58 = phi i64 [ 0, %21 ], [ %83, %57 ]
%59 = phi i64 [ 0, %21 ], [ %84, %57 ]
%60 = load i8*, i8** %3
%61 = getelementptr inbounds i8, i8* %60, i64 %58
%62 = load i8, i8* %61
%63 = or i64 %58, 2
%64 = getelementptr inbounds i8, i8* %13, i64 %63
store i8 %62, i8* %64
%65 = or i64 %58, 1
%66 = load i8*, i8** %3
%67 = getelementptr inbounds i8, i8* %66, i64 %65
%68 = load i8, i8* %67
%69 = or i64 %58, 3
%70 = getelementptr inbounds i8, i8* %13, i64 %69
store i8 %68, i8* %70
%71 = or i64 %58, 2
%72 = load i8*, i8** %3
%73 = getelementptr inbounds i8, i8* %72, i64 %71
%74 = load i8, i8* %73
%75 = add nuw nsw i64 %58, 4
%76 = getelementptr inbounds i8, i8* %13, i64 %75
store i8 %74, i8* %76
%77 = or i64 %58, 3
%78 = load i8*, i8** %3
%79 = getelementptr inbounds i8, i8* %78, i64 %77
%80 = load i8, i8* %79
%81 = add nuw nsw i64 %58, 5
%82 = getelementptr inbounds i8, i8* %13, i64 %81
store i8 %80, i8* %82
%83 = add nuw nsw i64 %58, 4
%84 = add i64 %59, 4
%85 = icmp eq i64 %84, %22
br i1 %85, label %23, label %57
86: ; preds = %37, %7
%87 = phi i32 [ 0, %7 ], [ 1, %37 ]
ret i32 %87
}