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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void hex_to_image(char outputf[], int hexSize, char hexdump[]) {
  int fd;
  int numSize = 17;
  char num[numSize];
  int iter = 0;
  fd = open(outputf, O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
  int height = 0, width = 0;
  width = (int)(hexSize / 3.0);
  if (hexSize % 2 == 0)
    height = hexSize / 2;
  else
    height = (hexSize / 2) - 1;
  height = 1;
  snprintf(num, numSize, "P6\n%i %i\n125\n", width, height);
  write(fd, num, strlen(num));
  int i;
  for (i = 0; i < width * height; i++) {
    char toWrite[1] = {hexdump[i]};
    write(fd, toWrite, 1);
    if (iter == 2) {
      iter = 0;
    } else {
      iter++;
    }
  }
  while (iter != 0) {
    iter++;
    if (iter > 2)
      break;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<hex_to_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x11,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
or     (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    0x2e13(%rip),%rax
or     (%rax),%edx
mov    0x2e02(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xe15(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x3c(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    121d <hex_to_image+0xcd>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x38(%rbp)
jmp    122e <hex_to_image+0xde>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x50(%rbp),%rdi
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x3c(%rbp),%ecx
mov    -0x38(%rbp),%r8d
lea    0xdbe(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x1c(%rbp),%eax
mov    %eax,-0x54(%rbp)
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    -0x3c(%rbp),%ecx
imul   -0x38(%rbp),%ecx
cmp    %ecx,%eax
jge    12d6 <hex_to_image+0x186>
mov    -0x18(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x41(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmpl   $0x2,-0x34(%rbp)
jne    12ba <hex_to_image+0x16a>
movl   $0x0,-0x34(%rbp)
jmp    12c3 <hex_to_image+0x173>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12c8 <hex_to_image+0x178>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1273 <hex_to_image+0x123>
jmp    12db <hex_to_image+0x18b>
cmpl   $0x0,-0x34(%rbp)
je     1302 <hex_to_image+0x1b2>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x2,-0x34(%rbp)
jle    12fd <hex_to_image+0x1ad>
jmp    1302 <hex_to_image+0x1b2>
jmp    12db <hex_to_image+0x18b>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_image>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e7d(%rip),%rcx
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e4b(%rip),%rbp
mov    0x2e54(%rip),%rcx
mov    (%rcx),%ecx
or     0x0(%rbp),%ecx
mov    %rdx,%r15
mov    %esi,%ebx
mov    0x2e3b(%rip),%rdx
or     (%rdx),%ecx
mov    %eax,%esi
mov    %ecx,%edx
call   1070 <open@plt>
mov    %eax,%r12d
cvtsi2sd %ebx,%xmm0
divsd  0xe59(%rip),%xmm0
cvttsd2si %xmm0,%r14d
lea    0xe55(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x11,%esi
mov    %r14d,%ecx
mov    $0x1,%r8d
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %r14d,%r14d
jle    1222 <hex_to_image+0xd2>
mov    %r14d,%ebx
xor    %ebp,%ebp
lea    0xf(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%r15,%rbp,1),%eax
mov    %al,0xf(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1200 <hex_to_image+0xb0>
mov    %r12d,%edi
call   1060 <close@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [14 x i8] c"P6\0A%i %i\0A125\0A\00"
define void @hex_to_image(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [1 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 17, i32* %8
%16 = load i32, i32* %8
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %9
%19 = alloca i8, i64 %17
store i64 %17, i64* %10
store i32 0, i32* %11
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IRGRP
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IROTH
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %20, i32 %23, i32 %28)
store i32 %29, i32* %7
store i32 0, i32* %12
store i32 0, i32* %13
%30 = load i32, i32* %5
%31 = sitofp i32 %30 to double
%32 = fdiv double %31, 3.000000e+00
%33 = fptosi double %32 to i32
store i32 %33, i32* %13
%34 = load i32, i32* %5
%35 = srem i32 %34, 2
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %3
%38 = load i32, i32* %5
%39 = sdiv i32 %38, 2
store i32 %39, i32* %12
br label %44
40: ; preds = %3
%41 = load i32, i32* %5
%42 = sdiv i32 %41, 2
%43 = sub nsw i32 %42, 1
store i32 %43, i32* %12
br label %44
44: ; preds = %40, %37
store i32 1, i32* %12
%45 = load i32, i32* %8
%46 = load i32, i32* %13
%47 = load i32, i32* %12
%48 = call i32 @snprintf(i8* %19, i32 %45, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %46, i32 %47)
%49 = load i32, i32* %7
%50 = call i32 @strlen(i8* %19)
%51 = call i32 @write(i32 %49, i8* %19, i32 %50)
store i32 0, i32* %14
br label %52
52: ; preds = %75, %44
%53 = load i32, i32* %14
%54 = load i32, i32* %13
%55 = load i32, i32* %12
%56 = mul nsw i32 %54, %55
%57 = icmp slt i32 %53, %56
br i1 %57, label %58, label %78
58: ; preds = %52
%59 = getelementptr inbounds [1 x i8], [1 x i8]* %15, i64 0, i64 0
%60 = load i8*, i8** %6
%61 = load i32, i32* %14
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = load i8, i8* %63
store i8 %64, i8* %59
%65 = load i32, i32* %7
%66 = getelementptr inbounds [1 x i8], [1 x i8]* %15, i64 0, i64 0
%67 = call i32 @write(i32 %65, i8* %66, i32 1)
%68 = load i32, i32* %11
%69 = icmp eq i32 %68, 2
br i1 %69, label %70, label %71
70: ; preds = %58
store i32 0, i32* %11
br label %74
71: ; preds = %58
%72 = load i32, i32* %11
%73 = add nsw i32 %72, 1
store i32 %73, i32* %11
br label %74
74: ; preds = %71, %70
br label %75
75: ; preds = %74
%76 = load i32, i32* %14
%77 = add nsw i32 %76, 1
store i32 %77, i32* %14
br label %52
78: ; preds = %52
br label %79
79: ; preds = %88, %78
%80 = load i32, i32* %11
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %89
82: ; preds = %79
%83 = load i32, i32* %11
%84 = add nsw i32 %83, 1
store i32 %84, i32* %11
%85 = load i32, i32* %11
%86 = icmp sgt i32 %85, 2
br i1 %86, label %87, label %88
87: ; preds = %82
br label %89
88: ; preds = %82
br label %79
89: ; preds = %87, %79
%90 = load i32, i32* %7
%91 = call i32 @close(i32 %90)
%92 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [14 x i8] c"P6\0A%i %i\0A125\0A\00"
define void @hex_to_image(i8* nocapture %0, i32 %1, i8* nocapture %2) {
%4 = alloca [1 x i8]
%5 = alloca [17 x i8]
%6 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %0, i32 %9, i32 %14)
%16 = sitofp i32 %1 to double
%17 = fdiv double %16, 3.000000e+00
%18 = fptosi double %17 to i32
%19 = call i32 @snprintf(i8* %6, i32 17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %18, i32 1)
%20 = call i32 @strlen(i8* %6)
%21 = call i32 @write(i32 %15, i8* %6, i32 %20)
%22 = icmp sgt i32 %18, 0
br i1 %22, label %23, label %33
23: ; preds = %3
%24 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%25 = zext i32 %18 to i64
br label %26
26: ; preds = %23, %26
%27 = phi i64 [ 0, %23 ], [ %31, %26 ]
%28 = getelementptr inbounds i8, i8* %2, i64 %27
%29 = load i8, i8* %28
store i8 %29, i8* %24
%30 = call i32 @write(i32 %15, i8* %24, i32 1)
%31 = add nuw nsw i64 %27, 1
%32 = icmp eq i64 %31, %25
br i1 %32, label %33, label %26
33: ; preds = %26, %3
%34 = call i32 @close(i32 %15)
ret void
}