#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 atsha_handle {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ create_and_lock_config(struct atsha_handle *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read_config(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write_and_lock_data(struct atsha_handle *, unsigned char *,
                               unsigned char *);
bool init(struct atsha_handle *handle, const char *filename) {
  FILE *conf = fopen(filename, "r");
  if (conf == NULL) {
    fprintf(stderr, "Couldn't open config file %s\n", filename);
    return false;
  }
  unsigned char data[16 * 32];
  unsigned char otp[16 * 4];
  if (!read_config(conf, data, 32)) {
    fprintf(stderr, "Couldn't read config data (keys).\n");
    return false;
  }
  if (!read_config(conf, otp, 4)) {
    fprintf(stderr, "Couldn't read config data (OTP).\n");
    return false;
  }
  if (create_and_lock_config(handle)) {
    printf("Configuration is locked\n");
  } else {
    printf("Configuration is NOT locked\n");
    return false;
  }
  if (write_and_lock_data(handle, data, otp)) {
    printf("Data and OTP zones are locked\n");
  } else {
    printf("Data and OTP zones are NOT locked\n");
    return false;
  }
  fclose(conf);
  return true;
}


x86 assembly (clang, -O0):
<init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c9 <init+0x59>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <init+0x171>
mov    -0x20(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x20,%edx
call   1070 <read_config@plt>
cmp    $0x0,%eax
jne    120a <init+0x9a>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <init+0x171>
mov    -0x20(%rbp),%rdi
lea    -0x260(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read_config@plt>
cmp    $0x0,%eax
jne    124b <init+0xdb>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <init+0x171>
mov    -0x10(%rbp),%rdi
call   1030 <create_and_lock_config@plt>
cmp    $0x0,%rax
je     126f <init+0xff>
lea    0xe00(%rip),%rdi
call   1050 <printf@plt>
jmp    1287 <init+0x117>
lea    0xe08(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <init+0x171>
mov    -0x10(%rbp),%rdi
lea    -0x220(%rbp),%rsi
lea    -0x260(%rbp),%rdx
call   1080 <write_and_lock_data@plt>
cmp    $0x0,%rax
je     12b9 <init+0x149>
lea    0xdec(%rip),%rdi
call   1050 <printf@plt>
jmp    12d1 <init+0x161>
lea    0xdfa(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <init+0x171>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x248,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe76(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1211 <init+0xa1>
mov    %rax,%r15
lea    0x40(%rsp),%rsi
mov    %rax,%rdi
mov    $0x20,%edx
call   1070 <read_config@plt>
test   %eax,%eax
je     122f <init+0xbf>
mov    %rsp,%rsi
mov    %r15,%rdi
mov    $0x4,%edx
call   1070 <read_config@plt>
test   %eax,%eax
je     1241 <init+0xd1>
mov    %r14,%rdi
call   1030 <create_and_lock_config@plt>
test   %rax,%rax
je     125c <init+0xec>
lea    0xea5(%rip),%rdi
call   1040 <puts@plt>
lea    0x40(%rsp),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1080 <write_and_lock_data@plt>
test   %rax,%rax
je     1265 <init+0xf5>
lea    0xebe(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    $0x1,%ebp
jmp    1273 <init+0x103>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1273 <init+0x103>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
jmp    1251 <init+0xe1>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1273 <init+0x103>
lea    0xe02(%rip),%rdi
jmp    126c <init+0xfc>
lea    0xe2d(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x248,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.atsha_handle = type { i32 }
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Couldn't open config file %s\0A\00"
@.str.2 = [35 x i8] c"Couldn't read config data (keys).\0A\00"
@.str.3 = [34 x i8] c"Couldn't read config data (OTP).\0A\00"
@.str.4 = [25 x i8] c"Configuration is locked\0A\00"
@.str.5 = [29 x i8] c"Configuration is NOT locked\0A\00"
@.str.6 = [31 x i8] c"Data and OTP zones are locked\0A\00"
@.str.7 = [35 x i8] c"Data and OTP zones are NOT locked\0A\00"
define i32 @init(%struct.atsha_handle* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.atsha_handle*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [512 x i8]
%8 = alloca [64 x i8]
store %struct.atsha_handle* %0, %struct.atsha_handle** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %5
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 0, i32* %3
br label %54
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%20 = call i32 @read_config(i32* %18, i8* %19, i32 32)
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %54
25: ; preds = %17
%26 = load i32*, i32** %6
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%28 = call i32 @read_config(i32* %26, i8* %27, i32 4)
%29 = icmp ne i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %54
33: ; preds = %25
%34 = load %struct.atsha_handle*, %struct.atsha_handle** %4
%35 = call i64 @create_and_lock_config(%struct.atsha_handle* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %41
39: ; preds = %33
%40 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %54
41: ; preds = %37
%42 = load %struct.atsha_handle*, %struct.atsha_handle** %4
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%45 = call i64 @write_and_lock_data(%struct.atsha_handle* %42, i8* %43, i8* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
br label %51
49: ; preds = %41
%50 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %54
51: ; preds = %47
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
store i32 1, i32* %3
br label %54
54: ; preds = %51, %49, %39, %30, %22, %13
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.atsha_handle = type { i32 }
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Couldn't open config file %s\0A\00"
@.str.2 = [35 x i8] c"Couldn't read config data (keys).\0A\00"
@.str.3 = [34 x i8] c"Couldn't read config data (OTP).\0A\00"
@str = [28 x i8] c"Configuration is NOT locked\00"
@str.8 = [24 x i8] c"Configuration is locked\00"
@str.9 = [34 x i8] c"Data and OTP zones are NOT locked\00"
@str.10 = [30 x i8] c"Data and OTP zones are locked\00"
define i32 @init(%struct.atsha_handle* %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = alloca [64 x i8]
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %40
10: ; preds = %2
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%13 = call i32 @read_config(i32* %5, i8* %11, i32 32)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %38
18: ; preds = %10
%19 = call i32 @read_config(i32* %5, i8* %12, i32 4)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %38
24: ; preds = %18
%25 = call i64 @create_and_lock_config(%struct.atsha_handle* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.8, i64 0, i64 0))
%29 = call i64 @write_and_lock_data(%struct.atsha_handle* %0, i8* %11, i8* %12)
%30 = icmp eq i64 %29, 0
br i1 %30, label %36, label %33
31: ; preds = %24
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %38
33: ; preds = %27
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.10, i64 0, i64 0))
%35 = call i32 @fclose(i32* %5)
br label %38
36: ; preds = %27
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.9, i64 0, i64 0))
br label %38
38: ; preds = %33, %36, %31, %21, %15
%39 = phi i32 [ 1, %33 ], [ 0, %36 ], [ 0, %31 ], [ 0, %21 ], [ 0, %15 ]
br label %40
40: ; preds = %38, %7
%41 = phi i32 [ 0, %7 ], [ %39, %38 ]
ret i32 %41
}