#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 htonl(unsigned int);
int ntohl(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int sscanf(char *, char *, unsigned int *);
int /*<<< orphan*/ stderr;
void chmem_flat(FILE *fp, unsigned char *buf, int argc, char *argv[]) {
  uint32_t *hdr = (uint32_t *)buf;
  uint32_t top;
  unsigned int v;
  if (ntohl(hdr[1]) != 4) {
    fprintf(stderr, "%s: unsupported revision %d.\n", argv[0], ntohl(hdr[1]));
    exit(1);
  }
  if (argc == 2) {
    top = ntohl(hdr[6]);
    if (top)
      printf("Flat binary set at %d bytes.\n", top);
    else
      printf("Flat binary, set to allocate all available.\n");
    return;
  }
  if (sscanf(argv[2], "%u", &v) != 1) {
    fprintf(stderr, "%s: invalid chmem value '%s'.\n", argv[0], argv[2]);
    exit(1);
  }
  hdr[6] = htonl(v);
  rewind(fp);
  if (fwrite(buf, 32, 1, fp) != 1) {
    fprintf(stderr, "%s: write error.\n", argv[0]);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<chmem_flat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   10a0 <ntohl@plt>
cmp    $0x4,%eax
je     11f8 <chmem_flat+0x78>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   10a0 <ntohl@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x2,-0x14(%rbp)
jne    1244 <chmem_flat+0xc4>
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%edi
call   10a0 <ntohl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1231 <chmem_flat+0xb1>
mov    -0x2c(%rbp),%esi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123f <chmem_flat+0xbf>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12f7 <chmem_flat+0x177>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe16(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     1295 <chmem_flat+0x115>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x20,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     12f7 <chmem_flat+0x177>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chmem_flat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    0x4(%rsi),%edi
call   10b0 <ntohl@plt>
cmp    $0x4,%eax
jne    1247 <chmem_flat+0xb7>
cmp    $0x2,%ebp
jne    11de <chmem_flat+0x4e>
mov    0x18(%rbx),%edi
call   10b0 <ntohl@plt>
test   %eax,%eax
je     1231 <chmem_flat+0xa1>
lea    0xe53(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
mov    0x10(%r14),%rdi
lea    0xe53(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    126d <chmem_flat+0xdd>
mov    0x4(%rsp),%edi
call   1060 <htonl@plt>
mov    %eax,0x18(%rbx)
mov    %r15,%rdi
call   1050 <rewind@plt>
mov    %rbx,%rdi
mov    $0x20,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
jne    1295 <chmem_flat+0x105>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe38(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
mov    0x2d92(%rip),%rcx
mov    (%rcx),%edi
mov    (%r14),%rdx
lea    0xda6(%rip),%rsi
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
mov    0x10(%r14),%rcx
lea    0xdbb(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdb6(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"%s: unsupported revision %d.\0A\00"
@.str.1 = [30 x i8] c"Flat binary set at %d bytes.\0A\00"
@.str.2 = [45 x i8] c"Flat binary, set to allocate all available.\0A\00"
@.str.3 = [3 x i8] c"%u\00"
@.str.4 = [31 x i8] c"%s: invalid chmem value '%s'.\0A\00"
@.str.5 = [18 x i8] c"%s: write error.\0A\00"
define void @chmem_flat(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 i32
%11 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8** %3, i8*** %8
%12 = load i8*, i8** %6
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %9
%14 = load i32*, i32** %9
%15 = getelementptr inbounds i32, i32* %14, i64 1
%16 = load i32, i32* %15
%17 = call i32 @ntohl(i32 %16)
%18 = icmp ne i32 %17, 4
br i1 %18, label %19, label %30
19: ; preds = %4
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %8
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = load i32*, i32** %9
%25 = getelementptr inbounds i32, i32* %24, i64 1
%26 = load i32, i32* %25
%27 = call i32 @ntohl(i32 %26)
%28 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %23, i32 %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %4
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 2
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = load i32*, i32** %9
%35 = getelementptr inbounds i32, i32* %34, i64 6
%36 = load i32, i32* %35
%37 = call i32 @ntohl(i32 %36)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = load i32, i32* %10
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %41)
br label %45
43: ; preds = %33
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
br label %80
46: ; preds = %30
%47 = load i8**, i8*** %8
%48 = getelementptr inbounds i8*, i8** %47, i64 2
%49 = load i8*, i8** %48
%50 = call i32 @sscanf(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %11)
%51 = icmp ne i32 %50, 1
br i1 %51, label %52, label %62
52: ; preds = %46
%53 = load i32, i32* @stderr
%54 = load i8**, i8*** %8
%55 = getelementptr inbounds i8*, i8** %54, i64 0
%56 = load i8*, i8** %55
%57 = load i8**, i8*** %8
%58 = getelementptr inbounds i8*, i8** %57, i64 2
%59 = load i8*, i8** %58
%60 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %56, i8* %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %46
%63 = load i32, i32* %11
%64 = call i32 @htonl(i32 %63)
%65 = load i32*, i32** %9
%66 = getelementptr inbounds i32, i32* %65, i64 6
store i32 %64, i32* %66
%67 = load i32*, i32** %5
%68 = call i32 @rewind(i32* %67)
%69 = load i8*, i8** %6
%70 = load i32*, i32** %5
%71 = call i32 @fwrite(i8* %69, i32 32, i32 1, i32* %70)
%72 = icmp ne i32 %71, 1
br i1 %72, label %73, label %80
73: ; preds = %62
%74 = load i32, i32* @stderr
%75 = load i8**, i8*** %8
%76 = getelementptr inbounds i8*, i8** %75, i64 0
%77 = load i8*, i8** %76
%78 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %77)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %45, %62
ret void
}

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