#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 uint16_t;
typedef int int16_t;

/* Variables and functions */
int get_s(char *, int);
int /*<<< orphan*/ puts(char *);
int strtol(char *, int /*<<< orphan*/ *, int);
int getcode(uint16_t *memarray, int max) {
  char opcode[5];
  if (get_s(opcode, 4) == -1) {
    puts("Fatal Error: Unexpected expression length ");
    return 1;
  }
  int16_t ir, ip = 0;
  ir = strtol(opcode, NULL, 16);
  while (ir != -1) {
    if (ip >= max) {
      puts("Fatal Error: memory overflow");
      return 1;
    }
    memarray[ip] = ir;
    ip++;
    if (get_s(opcode, 4) == -1) {
      puts("Fatal Error: Unexpected expression length ");
      return 1;
    }
    ir = strtol(opcode, NULL, 16);
  }
  return 0;
}


x86 assembly (clang, -O0):
<getcode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x19(%rbp),%rdi
mov    $0x4,%esi
call   1030 <get_s@plt>
cmp    $0xffffffff,%eax
jne    116e <getcode+0x3e>
lea    0xea3(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    121f <getcode+0xef>
movl   $0x0,-0x24(%rbp)
lea    -0x19(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     1218 <getcode+0xe8>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11b8 <getcode+0x88>
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    121f <getcode+0xef>
mov    -0x20(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
lea    -0x19(%rbp),%rdi
mov    $0x4,%esi
call   1030 <get_s@plt>
cmp    $0xffffffff,%eax
jne    11fe <getcode+0xce>
lea    0xe13(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    121f <getcode+0xef>
lea    -0x19(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
mov    %eax,-0x20(%rbp)
jmp    118a <getcode+0x5a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %rdi,%r15
lea    0x3(%rsp),%rdi
mov    $0x4,%esi
call   1030 <get_s@plt>
lea    0xea9(%rip),%r14
cmp    $0xffffffff,%eax
je     11ce <getcode+0x9e>
xor    %ebx,%ebx
lea    0x3(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
cmp    $0xffffffff,%eax
je     11db <getcode+0xab>
xor    %ebp,%ebp
test   %r12d,%r12d
cmovle %ebp,%r12d
lea    0x3(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    %rbp,%r12
je     11c7 <getcode+0x97>
mov    %eax,(%r15,%rbp,4)
mov    %r13,%rdi
mov    $0x4,%esi
call   1030 <get_s@plt>
cmp    $0xffffffff,%eax
je     11ce <getcode+0x9e>
add    $0x1,%rbp
xor    %ebx,%ebx
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtol@plt>
cmp    $0xffffffff,%eax
jne    1190 <getcode+0x60>
jmp    11db <getcode+0xab>
lea    0xe5d(%rip),%r14
mov    %r14,%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Fatal Error: Unexpected expression length \00"
@.str.1 = [29 x i8] c"Fatal Error: memory overflow\00"
define i32 @getcode(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [5 x i8]
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%10 = call i32 @get_s(i8* %9, i32 4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %43
14: ; preds = %2
store i32 0, i32* %8
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%16 = call i32 @strtol(i8* %15, i32* null, i32 16)
store i32 %16, i32* %7
br label %17
17: ; preds = %39, %14
%18 = load i32, i32* %7
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %42
20: ; preds = %17
%21 = load i32, i32* %8
%22 = load i32, i32* %5
%23 = icmp sge i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %43
26: ; preds = %20
%27 = load i32, i32* %7
%28 = load i32*, i32** %4
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
store i32 %27, i32* %31
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
%34 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%35 = call i32 @get_s(i8* %34, i32 4)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @puts(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %43
39: ; preds = %26
%40 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%41 = call i32 @strtol(i8* %40, i32* null, i32 16)
store i32 %41, i32* %7
br label %17
42: ; preds = %17
store i32 0, i32* %3
br label %43
43: ; preds = %42, %37, %24, %12
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [43 x i8] c"Fatal Error: Unexpected expression length \00"
@.str.1 = [29 x i8] c"Fatal Error: memory overflow\00"
define i32 @getcode(i32* nocapture writeonly %0, i32 %1) {
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%5 = call i32 @get_s(i8* %4, i32 4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %25, label %7
7: ; preds = %2
%8 = call i32 @strtol(i8* nocapture %4, i32* null, i32 16)
%9 = icmp eq i32 %8, -1
br i1 %9, label %28, label %10
10: ; preds = %7
%11 = call i32 @llvm.smax.i32(i32 %1, i32 0)
%12 = zext i32 %11 to i64
br label %13
13: ; preds = %10, %21
%14 = phi i64 [ 0, %10 ], [ %22, %21 ]
%15 = phi i32 [ %8, %10 ], [ %23, %21 ]
%16 = icmp eq i64 %14, %12
br i1 %16, label %25, label %17
17: ; preds = %13
%18 = getelementptr inbounds i32, i32* %0, i64 %14
store i32 %15, i32* %18
%19 = call i32 @get_s(i8* %4, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = add nuw nsw i64 %14, 1
%23 = call i32 @strtol(i8* nocapture %4, i32* null, i32 16)
%24 = icmp eq i32 %23, -1
br i1 %24, label %28, label %13
25: ; preds = %17, %13, %2
%26 = phi i8* [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), %17 ]
%27 = call i32 @puts(i8* dereferenceable(1) %26)
br label %28
28: ; preds = %21, %25, %7
%29 = phi i32 [ 0, %7 ], [ 1, %25 ], [ 0, %21 ]
ret i32 %29
}