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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
void chmem_fzx2(FILE *fp, unsigned char *buf, int argc, char *argv[]) {
  uint32_t v;
  unsigned short top;
  if (argc == 2) {
    top = buf[13] << 8;
    if (top)
      printf("Fuzix binary set at %d bytes.\n", top);
    else
      printf("Fuzix binary, set to allocate all available.\n");
    return;
  }
  if (sscanf(argv[2], "%u", &v) != 1 || v > 65536) {
    fprintf(stderr, "%s: invalid chmem value '%s'.\n", argv[0], argv[2]);
    exit(1);
  }
  v += 255;
  buf[13] = v >> 8;
  rewind(fp);
  if (fwrite(buf, 16, 1, fp) != 1) {
    fprintf(stderr, "%s: write error.\n", argv[0]);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<chmem_fzx2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0x2,-0x14(%rbp)
jne    11c5 <chmem_fzx2+0x65>
mov    -0x10(%rbp),%rax
movzbl 0xd(%rax),%eax
shl    $0x8,%eax
mov    %ax,-0x26(%rbp)
cmpw   $0x0,-0x26(%rbp)
je     11b2 <chmem_fzx2+0x52>
movzwl -0x26(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11c0 <chmem_fzx2+0x60>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    128e <chmem_fzx2+0x12e>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe79(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    11f3 <chmem_fzx2+0x93>
cmpl   $0x10000,-0x24(%rbp)
jle    1223 <chmem_fzx2+0xc3>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x24(%rbp),%eax
add    $0xff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0xd(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x10,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
je     128e <chmem_fzx2+0x12e>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chmem_fzx2>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edx
jne    11a1 <chmem_fzx2+0x31>
movzbl 0xd(%rbp),%esi
test   %esi,%esi
je     1203 <chmem_fzx2+0x93>
shl    $0x8,%esi
lea    0xe6e(%rip),%rdi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <printf@plt>
mov    %rcx,%r14
mov    %rdi,%rbx
mov    0x10(%rcx),%rdi
lea    0xe6d(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    1217 <chmem_fzx2+0xa7>
mov    0xc(%rsp),%eax
cmp    $0x10001,%eax
jge    1217 <chmem_fzx2+0xa7>
add    $0xff,%eax
mov    %eax,0xc(%rsp)
mov    %ah,0xd(%rbp)
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %rbp,%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
jne    123f <chmem_fzx2+0xcf>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe49(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <puts@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
mov    0x10(%r14),%rcx
lea    0xdf4(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdef(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"Fuzix binary set at %d bytes.\0A\00"
@.str.1 = [46 x i8] c"Fuzix binary, set to allocate all available.\0A\00"
@.str.2 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.3 = [31 x i8] c"%s: invalid chmem value '%s'.\0A\00"
@.str.4 = [18 x i8] c"%s: write error.\0A\00"
define void @chmem_fzx2(i32* %0, i8* %1, i32 %2, i8** %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i16
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8** %3, i8*** %8
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %29
13: ; preds = %4
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 13
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = shl i32 %17, 8
%19 = trunc i32 %18 to i16
store i16 %19, i16* %10
%20 = load i16, i16* %10
%21 = icmp ne i16 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = load i16, i16* %10
%24 = zext i16 %23 to i32
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %28
26: ; preds = %13
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
br label %69
29: ; preds = %4
%30 = load i8**, i8*** %8
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %9)
%34 = icmp ne i32 %33, 1
br i1 %34, label %38, label %35
35: ; preds = %29
%36 = load i32, i32* %9
%37 = icmp sgt i32 %36, 65536
br i1 %37, label %38, label %48
38: ; preds = %35, %29
%39 = load i32, i32* @stderr
%40 = load i8**, i8*** %8
%41 = getelementptr inbounds i8*, i8** %40, i64 0
%42 = load i8*, i8** %41
%43 = load i8**, i8*** %8
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = load i8*, i8** %44
%46 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %42, i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %35
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 255
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = ashr i32 %51, 8
%53 = trunc i32 %52 to i8
%54 = load i8*, i8** %6
%55 = getelementptr inbounds i8, i8* %54, i64 13
store i8 %53, i8* %55
%56 = load i32*, i32** %5
%57 = call i32 @rewind(i32* %56)
%58 = load i8*, i8** %6
%59 = load i32*, i32** %5
%60 = call i32 @fwrite(i8* %58, i32 16, i32 1, i32* %59)
%61 = icmp ne i32 %60, 1
br i1 %61, label %62, label %69
62: ; preds = %48
%63 = load i32, i32* @stderr
%64 = load i8**, i8*** %8
%65 = getelementptr inbounds i8*, i8** %64, i64 0
%66 = load i8*, i8** %65
%67 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %66)
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %28, %48
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"Fuzix binary set at %d bytes.\0A\00"
@.str.2 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.3 = [31 x i8] c"%s: invalid chmem value '%s'.\0A\00"
@.str.4 = [18 x i8] c"%s: write error.\0A\00"
@str = [45 x i8] c"Fuzix binary, set to allocate all available.\00"
define void @chmem_fzx2(i32* nocapture %0, i8* nocapture %1, i32 %2, i8** nocapture %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = icmp eq i32 %2, 2
br i1 %7, label %8, label %18
8: ; preds = %4
%9 = getelementptr inbounds i8, i8* %1, i64 13
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = zext i8 %10 to i32
%14 = shl nuw nsw i32 %13, 8
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %45
16: ; preds = %8
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
br label %45
18: ; preds = %4
%19 = getelementptr inbounds i8*, i8** %3, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%22 = icmp ne i32 %21, 1
%23 = load i32, i32* %5
%24 = icmp sgt i32 %23, 65536
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %3
%29 = load i8*, i8** %19
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %28, i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = add nsw i32 %23, 255
store i32 %33, i32* %5
%34 = lshr i32 %33, 8
%35 = trunc i32 %34 to i8
%36 = getelementptr inbounds i8, i8* %1, i64 13
store i8 %35, i8* %36
%37 = call i32 @rewind(i32* %0)
%38 = call i32 @fwrite(i8* %1, i32 16, i32 1, i32* %0)
%39 = icmp eq i32 %38, 1
br i1 %39, label %45, label %40
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %3
%43 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %32, %12, %16
ret void
}