#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*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int O_RDWR;
int O_SYNC;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(char *);
int get_m4_trace_flag(int);
int /*<<< orphan*/ load_m4_fw(int, char *, unsigned long);
scalar_t__ malloc(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ reset_m4_trace_flag(int);
int /*<<< orphan*/ send_m4_stop_flag(int, int);
int /*<<< orphan*/ set_gate_m4_clk(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ srcscr_set_bit(int, int);
int /*<<< orphan*/ srcscr_unset_bit(int, int);
int /*<<< orphan*/ stderr;
unsigned long strtoul(char *, char **, int);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  int fd, n, size, size2;
  int update_fw = 1;
  void *map_base, *virt_addr;
  unsigned long stack, pc, loadaddr;
  unsigned long read_result, writeval;
  off_t target;
  char *p;
  char m4IsStopped = 0;
  char m4IsRunning = 0;
  int m4TraceFlags = 0;
  int m4Retry;
  printf("\n%s - %s\n", "UDOONeo", "mqx_upload_on_m4SoloX 1.1.0");
  char *filepath;
  filepath = (char *)malloc(150);
  if (argc < 2) {
    fprintf(stderr, "\n\t%s - Usage: %s <project_name> [0xLOADADDR]\n\n",
            "UDOONeo", argv[0]);
    return (1);
  }
  sprintf(filepath, "%s", argv[1]);
  if (access(filepath, F_OK) == -1) {
    fprintf(stderr, "\n\tFile %s not found.\n", argv[1]);
    return 1;
  }
  if (argc == 3) {
    loadaddr = strtoul(argv[2], &p, 16);
  } else {
    loadaddr = 0x0;
  }
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (get_m4_trace_flag(fd) != 0) {
    reset_m4_trace_flag(fd);
    send_m4_stop_flag(fd, 0xAA);
    m4Retry = 4;
    while ((m4IsStopped == 0) && (m4Retry > 0)) {
      usleep(300000);
      m4Retry--;
      m4TraceFlags = get_m4_trace_flag(fd);
      printf("%s - Waiting M4 Stop, m4TraceFlags: %08X \n", "UDOONeo",
             m4TraceFlags);
      if ((m4TraceFlags & 0x00000800) != 0) {
        m4IsStopped = 1;
        printf("%s - Stopped M4 sketch \n", "UDOONeo");
      }
    }
    send_m4_stop_flag(fd, 0x00);
    if (m4IsStopped == 0) {
      fprintf(stderr, "%s - Failed to Stop M4 sketch: reboot system ! \n",
              "UDOONeo");
      close(fd);
      exit(2);
    }
    usleep(300000);
  }
  if (update_fw == 1) {
    srcscr_set_bit(fd, ((1 << 4)));
    set_gate_m4_clk(fd);
    load_m4_fw(fd, filepath, loadaddr);
  }
  free(filepath);
  srcscr_unset_bit(fd, ~((1 << 4)));
  m4Retry = 4;
  while ((m4IsRunning == 0) && (m4Retry > 0)) {
    usleep(300000);
    m4Retry--;
    m4TraceFlags = get_m4_trace_flag(fd);
    printf("%s - Waiting M4 Run, m4TraceFlags: %08X \n", "UDOONeo",
           m4TraceFlags);
    if ((m4TraceFlags & (0x00000020 | 0x00000040 | 0x00000080 | 0x00000100)) ==
        (0x00000020 | 0x00000040 | 0x00000080 | 0x00000100)) {
      m4IsRunning = 1;
      printf("%s - M4 sketch is running\n", "UDOONeo");
    }
  }
  if (m4IsRunning == 0) {
    fprintf(stderr, "%s - Failed to Start M4 sketch: reboot system ! \n",
            "UDOONeo");
    close(fd);
    exit(3);
  }
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x24(%rbp)
movb   $0x0,-0x71(%rbp)
movb   $0x0,-0x72(%rbp)
movl   $0x0,-0x78(%rbp)
lea    0xdaa(%rip),%rdi
lea    0xdad(%rip),%rsi
lea    0xdae(%rip),%rdx
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x96,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x88(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    12b7 <main+0x97>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xd91(%rip),%rsi
lea    0xd66(%rip),%rdx
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15bf <main+0x39f>
mov    -0x88(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd90(%rip),%rsi
call   1110 <sprintf@plt>
mov    -0x88(%rbp),%rdi
mov    0x2ce0(%rip),%rax
mov    (%rax),%esi
call   10f0 <access@plt>
cmp    $0xffffffff,%eax
jne    131b <main+0xfb>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd58(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15bf <main+0x39f>
cmpl   $0x3,-0x8(%rbp)
jne    1344 <main+0x124>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x70(%rbp),%rsi
mov    $0x10,%edx
call   1100 <strtoul@plt>
mov    %rax,-0x50(%rbp)
jmp    134c <main+0x12c>
movq   $0x0,-0x50(%rbp)
mov    0x2c95(%rip),%rax
mov    (%rax),%esi
mov    0x2c84(%rip),%rax
or     (%rax),%esi
lea    0xd11(%rip),%rdi
call   10e0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   10c0 <get_m4_trace_flag@plt>
cmp    $0x0,%eax
je     1485 <main+0x265>
mov    -0x14(%rbp),%edi
call   1060 <reset_m4_trace_flag@plt>
mov    -0x14(%rbp),%edi
mov    $0xaa,%esi
call   1050 <send_m4_stop_flag@plt>
movl   $0x4,-0x7c(%rbp)
movsbl -0x71(%rbp),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x89(%rbp)
jne    13bc <main+0x19c>
cmpl   $0x0,-0x7c(%rbp)
setg   %al
mov    %al,-0x89(%rbp)
mov    -0x89(%rbp),%al
test   $0x1,%al
jne    13cf <main+0x1af>
jmp    1434 <main+0x214>
mov    $0x493e0,%edi
call   1140 <usleep@plt>
mov    -0x7c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x7c(%rbp)
mov    -0x14(%rbp),%edi
call   10c0 <get_m4_trace_flag@plt>
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%edx
lea    0xc88(%rip),%rdi
lea    0xc0c(%rip),%rsi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x78(%rbp),%eax
and    $0x800,%eax
cmp    $0x0,%eax
je     142f <main+0x20f>
movb   $0x1,-0x71(%rbp)
lea    0xc89(%rip),%rdi
lea    0xbe2(%rip),%rsi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    139a <main+0x17a>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1050 <send_m4_stop_flag@plt>
movsbl -0x71(%rbp),%eax
cmp    $0x0,%eax
jne    147b <main+0x25b>
mov    0x2b7e(%rip),%rax
mov    (%rax),%edi
lea    0xc68(%rip),%rsi
lea    0xba8(%rip),%rdx
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0x2,%edi
call   1120 <exit@plt>
mov    $0x493e0,%edi
call   1140 <usleep@plt>
cmpl   $0x1,-0x24(%rbp)
jne    14b7 <main+0x297>
mov    -0x14(%rbp),%edi
mov    $0x10,%esi
call   10b0 <srcscr_set_bit@plt>
mov    -0x14(%rbp),%edi
call   1040 <set_gate_m4_clk@plt>
mov    -0x14(%rbp),%edi
mov    -0x88(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   10d0 <load_m4_fw@plt>
mov    -0x88(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
mov    $0xffffffef,%esi
call   1130 <srcscr_unset_bit@plt>
movl   $0x4,-0x7c(%rbp)
movsbl -0x72(%rbp),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x8a(%rbp)
jne    14f9 <main+0x2d9>
cmpl   $0x0,-0x7c(%rbp)
setg   %al
mov    %al,-0x8a(%rbp)
mov    -0x8a(%rbp),%al
test   $0x1,%al
jne    150c <main+0x2ec>
jmp    1573 <main+0x353>
mov    $0x493e0,%edi
call   1140 <usleep@plt>
mov    -0x7c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x7c(%rbp)
mov    -0x14(%rbp),%edi
call   10c0 <get_m4_trace_flag@plt>
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%edx
lea    0xbc0(%rip),%rdi
lea    0xacf(%rip),%rsi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x78(%rbp),%eax
and    $0x1e0,%eax
cmp    $0x1e0,%eax
jne    156e <main+0x34e>
movb   $0x1,-0x72(%rbp)
lea    0xbbe(%rip),%rdi
lea    0xaa3(%rip),%rsi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    14d7 <main+0x2b7>
movsbl -0x72(%rbp),%eax
cmp    $0x0,%eax
jne    15b0 <main+0x390>
mov    0x2a49(%rip),%rax
mov    (%rax),%edi
lea    0xba9(%rip),%rsi
lea    0xa73(%rip),%rdx
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0x3,%edi
call   1120 <exit@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1120 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0xdc8(%rip),%rdi
lea    0xdcb(%rip),%rsi
lea    0xdcc(%rip),%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x96,%edi
call   10b0 <malloc@plt>
cmp    $0x1,%ebx
jg     1280 <main+0x60>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rcx
lea    0xdbe(%rip),%rsi
lea    0xd93(%rip),%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    12c0 <main+0xa0>
mov    %rax,%r15
mov    0x8(%rbp),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1100 <access@plt>
cmp    $0xffffffff,%eax
jne    12d2 <main+0xb2>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbp),%rdx
lea    0xda4(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x3,%ebx
jne    12ef <main+0xcf>
mov    0x10(%rbp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x10,%edx
call   1110 <strtoul@plt>
mov    %rax,%r14
jmp    12f2 <main+0xd2>
xor    %r14d,%r14d
mov    0x2cef(%rip),%rax
mov    0x2ce0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xd68(%rip),%rdi
call   10f0 <open@plt>
mov    %eax,%r12d
mov    %eax,%edi
call   10d0 <get_m4_trace_flag@plt>
test   %eax,%eax
je     146e <main+0x24e>
mov    %r12d,%edi
call   1070 <reset_m4_trace_flag@plt>
mov    %r12d,%edi
mov    $0xaa,%esi
call   1060 <send_m4_stop_flag@plt>
mov    $0x493e0,%edi
call   1140 <usleep@plt>
mov    %r12d,%edi
call   10d0 <get_m4_trace_flag@plt>
mov    %eax,%ebx
lea    0xd2a(%rip),%rdi
lea    0xcb1(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
test   $0x800,%ebx
jne    1445 <main+0x225>
mov    $0x493e0,%edi
call   1140 <usleep@plt>
mov    %r12d,%edi
call   10d0 <get_m4_trace_flag@plt>
mov    %eax,%ebx
lea    0xcf3(%rip),%rdi
lea    0xc7a(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
test   $0x800,%ebx
jne    1445 <main+0x225>
mov    $0x493e0,%edi
call   1140 <usleep@plt>
mov    %r12d,%edi
call   10d0 <get_m4_trace_flag@plt>
mov    %eax,%ebx
lea    0xcbc(%rip),%rdi
lea    0xc43(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
test   $0x800,%ebx
jne    1445 <main+0x225>
mov    $0x493e0,%edi
call   1140 <usleep@plt>
mov    %r12d,%edi
call   10d0 <get_m4_trace_flag@plt>
mov    %eax,%ebx
lea    0xc89(%rip),%rdi
lea    0xc10(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
test   $0x800,%ebx
jne    1445 <main+0x225>
mov    %r12d,%edi
xor    %esi,%esi
call   1060 <send_m4_stop_flag@plt>
mov    0x2bb4(%rip),%rax
mov    (%rax),%edi
lea    0xc9b(%rip),%rsi
lea    0xbde(%rip),%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r12d,%edi
call   1090 <close@plt>
mov    $0x2,%edi
call   1120 <exit@plt>
lea    0xc5b(%rip),%rdi
lea    0xbb7(%rip),%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12d,%edi
xor    %esi,%esi
call   1060 <send_m4_stop_flag@plt>
mov    $0x493e0,%edi
call   1140 <usleep@plt>
mov    %r12d,%edi
mov    $0x10,%esi
call   10c0 <srcscr_set_bit@plt>
mov    %r12d,%edi
call   1050 <set_gate_m4_clk@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   10e0 <load_m4_fw@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12d,%edi
mov    $0xffffffef,%esi
call   1130 <srcscr_unset_bit@plt>
mov    $0x4,%ebp
lea    0xc3f(%rip),%r14
lea    0xb51(%rip),%r15
nopl   0x0(%rax)
mov    $0x493e0,%edi
call   1140 <usleep@plt>
mov    %r12d,%edi
call   10d0 <get_m4_trace_flag@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
and    $0x1e0,%ebx
cmp    $0x1e0,%ebx
je     152d <main+0x30d>
je     14fd <main+0x2dd>
lea    -0x1(%rbp),%eax
cmp    $0x1,%ebp
mov    %eax,%ebp
ja     14c0 <main+0x2a0>
mov    0x2acc(%rip),%rax
mov    (%rax),%edi
lea    0xc29(%rip),%rsi
lea    0xaf6(%rip),%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r12d,%edi
call   1090 <close@plt>
mov    $0x3,%edi
call   1120 <exit@plt>
lea    0xbe7(%rip),%rdi
lea    0xacf(%rip),%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12d,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"\0A%s - %s\0A\00"
@.str.1 = [8 x i8] c"UDOONeo\00"
@.str.2 = [28 x i8] c"mqx_upload_on_m4SoloX 1.1.0\00"
@stderr = global i32 0
@.str.3 = [47 x i8] c"\0A\09%s - Usage: %s <project_name> [0xLOADADDR]\0A\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@F_OK = global i32 0
@.str.5 = [22 x i8] c"\0A\09File %s not found.\0A\00"
@.str.6 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.7 = [43 x i8] c"%s - Waiting M4 Stop, m4TraceFlags: %08X \0A\00"
@.str.8 = [25 x i8] c"%s - Stopped M4 sketch \0A\00"
@.str.9 = [49 x i8] c"%s - Failed to Stop M4 sketch: reboot system ! \0A\00"
@.str.10 = [42 x i8] c"%s - Waiting M4 Run, m4TraceFlags: %08X \0A\00"
@.str.11 = [27 x i8] c"%s - M4 sketch is running\0A\00"
@.str.12 = [50 x i8] c"%s - Failed to Start M4 sketch: reboot system ! \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
%18 = alloca i32
%19 = alloca i8*
%20 = alloca i8
%21 = alloca i8
%22 = alloca i32
%23 = alloca i32
%24 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %10
store i8 0, i8* %20
store i8 0, i8* %21
store i32 0, i32* %22
%25 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%26 = call i64 @malloc(i32 150)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %24
%28 = load i32, i32* %4
%29 = icmp slt i32 %28, 2
br i1 %29, label %30, label %36
30: ; preds = %2
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 1, i32* %3
br label %165
36: ; preds = %2
%37 = load i8*, i8** %24
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = load i8*, i8** %24
%43 = load i32, i32* @F_OK
%44 = call i32 @access(i8* %42, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %52
46: ; preds = %36
%47 = load i32, i32* @stderr
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %50)
store i32 1, i32* %3
br label %165
52: ; preds = %36
%53 = load i32, i32* %4
%54 = icmp eq i32 %53, 3
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 2
%58 = load i8*, i8** %57
%59 = call i64 @strtoul(i8* %58, i8** %19, i32 16)
store i64 %59, i64* %15
br label %61
60: ; preds = %52
store i64 0, i64* %15
br label %61
61: ; preds = %60, %55
%62 = load i32, i32* @O_RDWR
%63 = load i32, i32* @O_SYNC
%64 = or i32 %62, %63
%65 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %64)
store i32 %65, i32* %6
%66 = load i32, i32* %6
%67 = call i32 @get_m4_trace_flag(i32 %66)
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %111
69: ; preds = %61
%70 = load i32, i32* %6
%71 = call i32 @reset_m4_trace_flag(i32 %70)
%72 = load i32, i32* %6
%73 = call i32 @send_m4_stop_flag(i32 %72, i32 170)
store i32 4, i32* %23
br label %74
74: ; preds = %96, %69
%75 = load i8, i8* %20
%76 = sext i8 %75 to i32
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = load i32, i32* %23
%80 = icmp sgt i32 %79, 0
br label %81
81: ; preds = %78, %74
%82 = phi i1 [ false, %74 ], [ %80, %78 ]
br i1 %82, label %83, label %97
83: ; preds = %81
%84 = call i32 @usleep(i32 300000)
%85 = load i32, i32* %23
%86 = add nsw i32 %85, -1
store i32 %86, i32* %23
%87 = load i32, i32* %6
%88 = call i32 @get_m4_trace_flag(i32 %87)
store i32 %88, i32* %22
%89 = load i32, i32* %22
%90 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %89)
%91 = load i32, i32* %22
%92 = and i32 %91, 2048
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %96
94: ; preds = %83
store i8 1, i8* %20
%95 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %96
96: ; preds = %94, %83
br label %74
97: ; preds = %81
%98 = load i32, i32* %6
%99 = call i32 @send_m4_stop_flag(i32 %98, i32 0)
%100 = load i8, i8* %20
%101 = sext i8 %100 to i32
%102 = icmp eq i32 %101, 0
br i1 %102, label %103, label %109
103: ; preds = %97
%104 = load i32, i32* @stderr
%105 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %104, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%106 = load i32, i32* %6
%107 = call i32 @close(i32 %106)
%108 = call i32 @exit(i32 2)
unreachable
109: ; preds = %97
%110 = call i32 @usleep(i32 300000)
br label %111
111: ; preds = %109, %61
%112 = load i32, i32* %10
%113 = icmp eq i32 %112, 1
br i1 %113, label %114, label %123
114: ; preds = %111
%115 = load i32, i32* %6
%116 = call i32 @srcscr_set_bit(i32 %115, i32 16)
%117 = load i32, i32* %6
%118 = call i32 @set_gate_m4_clk(i32 %117)
%119 = load i32, i32* %6
%120 = load i8*, i8** %24
%121 = load i64, i64* %15
%122 = call i32 @load_m4_fw(i32 %119, i8* %120, i64 %121)
br label %123
123: ; preds = %114, %111
%124 = load i8*, i8** %24
%125 = call i32 @free(i8* %124)
%126 = load i32, i32* %6
%127 = call i32 @srcscr_unset_bit(i32 %126, i32 -17)
store i32 4, i32* %23
br label %128
128: ; preds = %150, %123
%129 = load i8, i8* %21
%130 = sext i8 %129 to i32
%131 = icmp eq i32 %130, 0
br i1 %131, label %132, label %135
132: ; preds = %128
%133 = load i32, i32* %23
%134 = icmp sgt i32 %133, 0
br label %135
135: ; preds = %132, %128
%136 = phi i1 [ false, %128 ], [ %134, %132 ]
br i1 %136, label %137, label %151
137: ; preds = %135
%138 = call i32 @usleep(i32 300000)
%139 = load i32, i32* %23
%140 = add nsw i32 %139, -1
store i32 %140, i32* %23
%141 = load i32, i32* %6
%142 = call i32 @get_m4_trace_flag(i32 %141)
store i32 %142, i32* %22
%143 = load i32, i32* %22
%144 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %143)
%145 = load i32, i32* %22
%146 = and i32 %145, 480
%147 = icmp eq i32 %146, 480
br i1 %147, label %148, label %150
148: ; preds = %137
store i8 1, i8* %21
%149 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %150
150: ; preds = %148, %137
br label %128
151: ; preds = %135
%152 = load i8, i8* %21
%153 = sext i8 %152 to i32
%154 = icmp eq i32 %153, 0
br i1 %154, label %155, label %161
155: ; preds = %151
%156 = load i32, i32* @stderr
%157 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %156, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%158 = load i32, i32* %6
%159 = call i32 @close(i32 %158)
%160 = call i32 @exit(i32 3)
unreachable
161: ; preds = %151
%162 = load i32, i32* %6
%163 = call i32 @close(i32 %162)
%164 = call i32 @exit(i32 0)
unreachable
165: ; preds = %46, %30
%166 = load i32, i32* %3
ret i32 %166
}

LLVM IR (-O2):
@.str = [10 x i8] c"\0A%s - %s\0A\00"
@.str.1 = [8 x i8] c"UDOONeo\00"
@.str.2 = [28 x i8] c"mqx_upload_on_m4SoloX 1.1.0\00"
@stderr = global i32 0
@.str.3 = [47 x i8] c"\0A\09%s - Usage: %s <project_name> [0xLOADADDR]\0A\0A\00"
@F_OK = global i32 0
@.str.5 = [22 x i8] c"\0A\09File %s not found.\0A\00"
@.str.6 = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.7 = [43 x i8] c"%s - Waiting M4 Stop, m4TraceFlags: %08X \0A\00"
@.str.8 = [25 x i8] c"%s - Stopped M4 sketch \0A\00"
@.str.9 = [49 x i8] c"%s - Failed to Stop M4 sketch: reboot system ! \0A\00"
@.str.10 = [42 x i8] c"%s - Waiting M4 Run, m4TraceFlags: %08X \0A\00"
@.str.11 = [27 x i8] c"%s - M4 sketch is running\0A\00"
@.str.12 = [50 x i8] c"%s - Failed to Start M4 sketch: reboot system ! \0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i64 @malloc(i32 150)
%7 = inttoptr i64 %6 to i8*
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %1
%12 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %101
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i8* @strcpy(i8* dereferenceable(1) %7, i8* dereferenceable(1) %15)
%17 = load i32, i32* @F_OK
%18 = tail call i32 @access(i8* %7, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %14
%23 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %22)
br label %101
24: ; preds = %13
%25 = icmp eq i32 %0, 3
br i1 %25, label %26, label %30
26: ; preds = %24
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = call i64 @strtoul(i8* %28, i8** %3, i32 16)
br label %30
30: ; preds = %24, %26
%31 = phi i64 [ %29, %26 ], [ 0, %24 ]
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_SYNC
%34 = or i32 %33, %32
%35 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %34)
%36 = tail call i32 @get_m4_trace_flag(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %74, label %38
38: ; preds = %30
%39 = tail call i32 @reset_m4_trace_flag(i32 %35)
%40 = tail call i32 @send_m4_stop_flag(i32 %35, i32 170)
%41 = tail call i32 @usleep(i32 300000)
%42 = tail call i32 @get_m4_trace_flag(i32 %35)
%43 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = and i32 %42, 2048
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %70
46: ; preds = %38
%47 = tail call i32 @usleep(i32 300000)
%48 = tail call i32 @get_m4_trace_flag(i32 %35)
%49 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = and i32 %48, 2048
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %70
52: ; preds = %46
%53 = tail call i32 @usleep(i32 300000)
%54 = tail call i32 @get_m4_trace_flag(i32 %35)
%55 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %54)
%56 = and i32 %54, 2048
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %70
58: ; preds = %52
%59 = tail call i32 @usleep(i32 300000)
%60 = tail call i32 @get_m4_trace_flag(i32 %35)
%61 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %60)
%62 = and i32 %60, 2048
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %70
64: ; preds = %58
%65 = tail call i32 @send_m4_stop_flag(i32 %35, i32 0)
%66 = load i32, i32* @stderr
%67 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%68 = tail call i32 @close(i32 %35)
%69 = tail call i32 @exit(i32 2)
unreachable
70: ; preds = %58, %52, %46, %38
%71 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%72 = tail call i32 @send_m4_stop_flag(i32 %35, i32 0)
%73 = tail call i32 @usleep(i32 300000)
br label %74
74: ; preds = %30, %70
%75 = tail call i32 @srcscr_set_bit(i32 %35, i32 16)
%76 = tail call i32 @set_gate_m4_clk(i32 %35)
%77 = tail call i32 @load_m4_fw(i32 %35, i8* %7, i64 %31)
%78 = tail call i32 @free(i8* %7)
%79 = tail call i32 @srcscr_unset_bit(i32 %35, i32 -17)
br label %80
80: ; preds = %74, %87
%81 = phi i32 [ 4, %74 ], [ %88, %87 ]
%82 = tail call i32 @usleep(i32 300000)
%83 = tail call i32 @get_m4_trace_flag(i32 %35)
%84 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %83)
%85 = and i32 %83, 480
%86 = icmp eq i32 %85, 480
br i1 %86, label %97, label %87
87: ; preds = %80
%88 = add nsw i32 %81, -1
%89 = xor i1 %86, true
%90 = icmp ugt i32 %81, 1
%91 = select i1 %89, i1 %90, i1 false
br i1 %91, label %80, label %92
92: ; preds = %87
%93 = load i32, i32* @stderr
%94 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%95 = tail call i32 @close(i32 %35)
%96 = tail call i32 @exit(i32 3)
unreachable
97: ; preds = %80
%98 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%99 = tail call i32 @close(i32 %35)
%100 = tail call i32 @exit(i32 0)
unreachable
101: ; preds = %20, %9
ret i32 1
}