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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ calloc(size_t, int);
size_t cmd_write_memory(int /*<<< orphan*/ **, int /*<<< orphan*/,
                        int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ sscanf(char *, char *, int /*<<< orphan*/ *);
size_t cmd_write_memory_file(uint32_t **bufio, char *file_name) {
  size_t idx, i;
  uint32_t *buf;
  size_t n;
  buf = *bufio;
  if (buf == NULL) {
    buf = (uint32_t *)calloc(5000, sizeof(uint32_t));
    if (buf == NULL)
      return -1;
    *bufio = buf;
  }
  FILE *fp;
  char buffer[15];
  fp = fopen(file_name, "r");
  fseek(fp, SEEK_SET, 0);
  size_t word_counter = 0;
  uint32_t *jtag_buf;
  while (1 == fread(buffer, 11, 1, fp)) {
    word_counter++;
  }
  jtag_buf = (uint32_t *)calloc(word_counter, sizeof(uint32_t));
  fseek(fp, SEEK_SET, 0);
  uint32_t wd;
  int i_con = 0;
  while (1 == fread(buffer, 11, 1, fp)) {
    sscanf(buffer, "%x", &wd);
    jtag_buf[i_con] = wd;
    i_con++;
  }
  n = cmd_write_memory(&buf, 0, jtag_buf, word_counter);
  free(jtag_buf);
  fclose(fp);
  return n;
}


x86 assembly (clang, -O0):
<cmd_write_memory_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11dc <cmd_write_memory_file+0x5c>
mov    $0x1388,%edi
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d1 <cmd_write_memory_file+0x51>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12f7 <cmd_write_memory_file+0x177>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <fseek@plt>
movq   $0x0,-0x58(%rbp)
lea    -0x4f(%rbp),%rdi
mov    -0x40(%rbp),%rcx
mov    $0xb,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1243 <cmd_write_memory_file+0xc3>
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    120c <cmd_write_memory_file+0x8c>
mov    -0x58(%rbp),%rdi
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,-0x60(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <fseek@plt>
movl   $0x0,-0x68(%rbp)
lea    -0x4f(%rbp),%rdi
mov    -0x40(%rbp),%rcx
mov    $0xb,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    12c6 <cmd_write_memory_file+0x146>
lea    -0x4f(%rbp),%rdi
lea    0xd61(%rip),%rsi
lea    -0x64(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x64(%rbp),%edx
mov    -0x60(%rbp),%rax
movslq -0x68(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    1270 <cmd_write_memory_file+0xf0>
mov    -0x60(%rbp),%rdx
mov    -0x58(%rbp),%rcx
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1040 <cmd_write_memory@plt>
mov    %rax,-0x38(%rbp)
mov    -0x60(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_write_memory_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    (%rdi),%rax
mov    %rax,0x10(%rsp)
test   %rax,%rax
jne    11c2 <cmd_write_memory_file+0x42>
mov    %rdi,%rbp
mov    $0x1388,%edi
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     12df <cmd_write_memory_file+0x15f>
mov    %rax,0x0(%rbp)
lea    0xe37(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r14
mov    0x2e05(%rip),%r12
mov    (%r12),%esi
mov    %rax,%rdi
xor    %edx,%edx
call   1090 <fseek@plt>
mov    $0xffffffffffffffff,%rbx
lea    0x19(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
add    $0x1,%rbx
cmp    $0x1,%eax
je     1200 <cmd_write_memory_file+0x80>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,0x8(%rsp)
mov    (%r12),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   1090 <fseek@plt>
lea    0x19(%rsp),%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    12a3 <cmd_write_memory_file+0x123>
lea    0xda1(%rip),%r12
lea    0x19(%rsp),%r13
lea    0x4(%rsp),%rbp
mov    0x8(%rsp),%r15
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1080 <sscanf@plt>
mov    0x4(%rsp),%eax
mov    %eax,(%r15)
mov    %r13,%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
add    $0x4,%r15
cmp    $0x1,%eax
je     1270 <cmd_write_memory_file+0xf0>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    0x8(%rsp),%rbp
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1040 <cmd_write_memory@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffffffffffff,%rbx
jmp    12cd <cmd_write_memory_file+0x14d>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@.str.1 = [3 x i8] c"%x\00"
define i64 @cmd_write_memory_file(i32** %0, i8* %1) {
%3 = alloca i64
%4 = alloca i32**
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca [15 x i8]
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
store i32** %0, i32*** %4
store i8* %1, i8** %5
%16 = load i32**, i32*** %4
%17 = load i32*, i32** %16
store i32* %17, i32** %8
%18 = load i32*, i32** %8
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %29
20: ; preds = %2
%21 = call i64 @calloc(i64 5000, i32 4)
%22 = inttoptr i64 %21 to i32*
store i32* %22, i32** %8
%23 = load i32*, i32** %8
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20
store i64 -1, i64* %3
br label %74
26: ; preds = %20
%27 = load i32*, i32** %8
%28 = load i32**, i32*** %4
store i32* %27, i32** %28
br label %29
29: ; preds = %26, %2
%30 = load i8*, i8** %5
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %31, i32** %10
%32 = load i32*, i32** %10
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %32, i32 %33, i32 0)
store i64 0, i64* %12
br label %35
35: ; preds = %40, %29
%36 = getelementptr inbounds [15 x i8], [15 x i8]* %11, i64 0, i64 0
%37 = load i32*, i32** %10
%38 = call i32 @fread(i8* %36, i32 11, i32 1, i32* %37)
%39 = icmp eq i32 1, %38
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i64, i64* %12
%42 = add i64 %41, 1
store i64 %42, i64* %12
br label %35
43: ; preds = %35
%44 = load i64, i64* %12
%45 = call i64 @calloc(i64 %44, i32 4)
%46 = inttoptr i64 %45 to i32*
store i32* %46, i32** %13
%47 = load i32*, i32** %10
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @fseek(i32* %47, i32 %48, i32 0)
store i32 0, i32* %15
br label %50
50: ; preds = %55, %43
%51 = getelementptr inbounds [15 x i8], [15 x i8]* %11, i64 0, i64 0
%52 = load i32*, i32** %10
%53 = call i32 @fread(i8* %51, i32 11, i32 1, i32* %52)
%54 = icmp eq i32 1, %53
br i1 %54, label %55, label %65
55: ; preds = %50
%56 = getelementptr inbounds [15 x i8], [15 x i8]* %11, i64 0, i64 0
%57 = call i32 @sscanf(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %14)
%58 = load i32, i32* %14
%59 = load i32*, i32** %13
%60 = load i32, i32* %15
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i32, i32* %59, i64 %61
store i32 %58, i32* %62
%63 = load i32, i32* %15
%64 = add nsw i32 %63, 1
store i32 %64, i32* %15
br label %50
65: ; preds = %50
%66 = load i32*, i32** %13
%67 = load i64, i64* %12
%68 = call i64 @cmd_write_memory(i32** %8, i32 0, i32* %66, i64 %67)
store i64 %68, i64* %9
%69 = load i32*, i32** %13
%70 = call i32 @free(i32* %69)
%71 = load i32*, i32** %10
%72 = call i32 @fclose(i32* %71)
%73 = load i64, i64* %9
store i64 %73, i64* %3
br label %74
74: ; preds = %65, %25
%75 = load i64, i64* %3
ret i64 %75
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
@.str.1 = [3 x i8] c"%x\00"
define i64 @cmd_write_memory_file(i32** nocapture %0, i8* nocapture %1) {
%3 = alloca i32*
%4 = alloca [15 x i8]
%5 = alloca i32
%6 = bitcast i32** %3 to i8*
%7 = load i32*, i32** %0
store i32* %7, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = tail call i64 @calloc(i64 5000, i32 4)
%11 = inttoptr i64 %10 to i32*
store i32* %11, i32** %3
%12 = icmp eq i64 %10, 0
br i1 %12, label %44, label %13
13: ; preds = %9
store i32* %11, i32** %0
br label %14
14: ; preds = %13, %2
%15 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%16 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %16, i32 %17, i32 0)
br label %19
19: ; preds = %19, %14
%20 = phi i64 [ 0, %14 ], [ %23, %19 ]
%21 = call i32 @fread(i8* %15, i32 11, i32 1, i32* %16)
%22 = icmp eq i32 %21, 1
%23 = add i64 %20, 1
br i1 %22, label %19, label %24
24: ; preds = %19
%25 = tail call i64 @calloc(i64 %20, i32 4)
%26 = inttoptr i64 %25 to i32*
%27 = load i32, i32* @SEEK_SET
%28 = tail call i32 @fseek(i32* %16, i32 %27, i32 0)
%29 = bitcast i32* %5 to i8*
%30 = call i32 @fread(i8* %15, i32 11, i32 1, i32* %16)
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %40
32: ; preds = %24, %32
%33 = phi i64 [ %37, %32 ], [ 0, %24 ]
%34 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%35 = load i32, i32* %5
%36 = getelementptr inbounds i32, i32* %26, i64 %33
store i32 %35, i32* %36
%37 = add nuw i64 %33, 1
%38 = call i32 @fread(i8* %15, i32 11, i32 1, i32* %16)
%39 = icmp eq i32 %38, 1
br i1 %39, label %32, label %40
40: ; preds = %32, %24
%41 = call i64 @cmd_write_memory(i32** %3, i32 0, i32* %26, i64 %20)
%42 = call i32 @free(i32* %26)
%43 = call i32 @fclose(i32* %16)
br label %44
44: ; preds = %9, %40
%45 = phi i64 [ %41, %40 ], [ -1, %9 ]
ret i64 %45
}