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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int atoi(char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ help();
int /*<<< orphan*/ stderr;
int main(int argc, const char *argv[]) {
  if (argc != 4) {
    help();
    return 1;
  }
  int width = atoi(argv[2]);
  int height = atoi(argv[3]);
  if (width <= 0 || width > 0xFFFF) {
    fprintf(stderr, "build-resize: invalid width (%d)\n", width);
    return 1;
  }
  if (height <= 0 || height > 0xFFFF) {
    fprintf(stderr, "build-resize: invalid height (%d)\n", height);
    return 1;
  }
  FILE *fimage = fopen(argv[1], "rb+");
  if (fimage == NULL) {
    fprintf(stderr, "build-resize: invalid image (%s)\n", argv[1]);
    return 1;
  }
  int size = (width << 16) | (height & 0xFFFF);
  fseek(fimage, 24, SEEK_SET);
  fwrite(&size, sizeof(size), 1, fimage);
  fclose(fimage);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11a3 <main+0x33>
mov    $0x0,%al
call   1070 <help@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x16d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11da <main+0x6a>
cmpl   $0xffff,-0x14(%rbp)
jle    1200 <main+0x90>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x16d>
cmpl   $0x0,-0x18(%rbp)
jle    1217 <main+0xa7>
cmpl   $0xffff,-0x18(%rbp)
jle    123d <main+0xcd>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x16d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    128b <main+0x11b>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x16d>
mov    -0x14(%rbp),%eax
shl    $0x10,%eax
mov    -0x18(%rbp),%ecx
and    $0xffff,%ecx
or     %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2d2e(%rip),%rax
mov    (%rax),%edx
mov    $0x18,%esi
call   1050 <fseek@plt>
mov    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x4,%edi
jne    11bb <main+0x4b>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x18(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%r15d
lea    -0x10000(%r14),%eax
cmp    $0xffff0000,%eax
ja     11c4 <main+0x54>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
mov    %r14d,%edx
jmp    11e5 <main+0x75>
xor    %eax,%eax
call   1070 <help@plt>
jmp    11ec <main+0x7c>
lea    -0x10000(%r15),%eax
cmp    $0xffff0000,%eax
ja     11fd <main+0x8d>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x8(%rbx),%rdi
lea    0xe3d(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     125a <main+0xea>
mov    %rax,%r12
shl    $0x10,%r14d
or     %r14d,%r15d
mov    %r15d,0x4(%rsp)
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x18,%esi
call   1050 <fseek@plt>
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1090 <fwrite@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11f1 <main+0x81>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xddb(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11ec <main+0x7c>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [34 x i8] c"build-resize: invalid width (%d)\0A\00"
@.str.1 = [35 x i8] c"build-resize: invalid height (%d)\0A\00"
@.str.2 = [4 x i8] c"rb+\00"
@.str.3 = [34 x i8] c"build-resize: invalid image (%s)\0A\00"
@SEEK_SET = global i32 0
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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 4
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (...) @help()
store i32 1, i32* %3
br label %68
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %6
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 3
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %7
%23 = load i32, i32* %6
%24 = icmp sle i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %14
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 65535
br i1 %27, label %28, label %32
28: ; preds = %25, %14
%29 = load i32, i32* @stderr
%30 = load i32, i32* %6
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %30)
store i32 1, i32* %3
br label %68
32: ; preds = %25
%33 = load i32, i32* %7
%34 = icmp sle i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = load i32, i32* %7
%37 = icmp sgt i32 %36, 65535
br i1 %37, label %38, label %42
38: ; preds = %35, %32
%39 = load i32, i32* @stderr
%40 = load i32, i32* %7
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %40)
store i32 1, i32* %3
br label %68
42: ; preds = %35
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32* %46, i32** %8
%47 = load i32*, i32** %8
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %55
49: ; preds = %42
%50 = load i32, i32* @stderr
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %53)
store i32 1, i32* %3
br label %68
55: ; preds = %42
%56 = load i32, i32* %6
%57 = shl i32 %56, 16
%58 = load i32, i32* %7
%59 = and i32 %58, 65535
%60 = or i32 %57, %59
store i32 %60, i32* %9
%61 = load i32*, i32** %8
%62 = load i32, i32* @SEEK_SET
%63 = call i32 @fseek(i32* %61, i32 24, i32 %62)
%64 = load i32*, i32** %8
%65 = call i32 @fwrite(i32* %9, i32 4, i32 1, i32* %64)
%66 = load i32*, i32** %8
%67 = call i32 @fclose(i32* %66)
store i32 0, i32* %3
br label %68
68: ; preds = %55, %49, %38, %28, %12
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [34 x i8] c"build-resize: invalid width (%d)\0A\00"
@.str.1 = [35 x i8] c"build-resize: invalid height (%d)\0A\00"
@.str.2 = [4 x i8] c"rb+\00"
@.str.3 = [34 x i8] c"build-resize: invalid image (%s)\0A\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 4
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 (...) @help()
br label %42
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 3
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = add i32 %10, -65536
%15 = icmp ult i32 %14, -65535
br i1 %15, label %16, label %19
16: ; preds = %7
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %42
19: ; preds = %7
%20 = add i32 %13, -65536
%21 = icmp ult i32 %20, -65535
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %42
25: ; preds = %19
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = tail call i32* @fopen(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %26
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %32)
br label %42
34: ; preds = %25
%35 = bitcast i32* %3 to i8*
%36 = shl nuw i32 %10, 16
%37 = or i32 %13, %36
store i32 %37, i32* %3
%38 = load i32, i32* @SEEK_SET
%39 = tail call i32 @fseek(i32* %28, i32 24, i32 %38)
%40 = call i32 @fwrite(i32* %3, i32 4, i32 1, i32* %28)
%41 = tail call i32 @fclose(i32* %28)
br label %42
42: ; preds = %16, %22, %34, %30, %5
%43 = phi i32 [ 1, %5 ], [ 1, %16 ], [ 1, %22 ], [ 1, %30 ], [ 0, %34 ]
ret i32 %43
}