#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 */
struct stat {
  unsigned int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
double sqrt(double);
size_t strlen(char *);
int /*<<< orphan*/ tga_save(char *, char *, unsigned int, short, short);
int main(int argc, char **argv) {
  char *bytes = 0;
  char buf = 0;
  char *outfile = 0;
  struct stat buffer;
  unsigned int readsize;
  double result;
  unsigned int width, height;
  FILE *file = 0;
  if (!argc) {
    printf("Usage: <src-file>\n");
    return 1;
  }
  outfile = (char *)malloc((strlen(argv[1]) + 1) + 4);
  sprintf(outfile, "%s.tga", argv[1]);
  file = fopen(argv[1], "r");
  if (!file)
    return 1;
  fstat(fileno(file), &buffer);
  readsize = buffer.st_size;
  printf("size: %d\n", readsize);
  result = sqrt((double)readsize);
  width = result;
  height = result;
  bytes = (char *)malloc((size_t)readsize);
  fread(bytes, 1, readsize, file);
  fclose(file);
  tga_save(outfile, bytes, width * height, (short)width, (short)height);
  free((void *)bytes);
  free((void *)outfile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x50(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1216 <main+0x56>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1336 <main+0x176>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
add    $0x4,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdc9(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdbc(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    127e <main+0xbe>
movl   $0x1,-0x4(%rbp)
jmp    1336 <main+0x176>
mov    -0x50(%rbp),%rdi
call   1090 <fileno@plt>
mov    %eax,%edi
lea    -0x30(%rbp),%rsi
call   10e0 <fstat@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd7a(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    -0x34(%rbp),%eax
cvtsi2sd %rax,%xmm0
call   10c0 <sqrt@plt>
movsd  %xmm0,-0x40(%rbp)
cvttsd2si -0x40(%rbp),%rax
mov    %eax,-0x44(%rbp)
cvttsd2si -0x40(%rbp),%rax
mov    %eax,-0x48(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x50(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x50(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x44(%rbp),%edx
imul   -0x48(%rbp),%edx
mov    -0x44(%rbp),%eax
mov    %ax,%cx
mov    -0x48(%rbp),%eax
movswl %cx,%ecx
movswl %ax,%r8d
call   1070 <tga_save@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
je     1263 <main+0x93>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1070 <strlen@plt>
lea    0x5(%rax),%rdi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    0x8(%rbx),%rdx
lea    0xdfa(%rip),%rsi
mov    %rax,%rdi
call   10e0 <sprintf@plt>
mov    0x8(%rbx),%rdi
lea    0xdee(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     126f <main+0x9f>
mov    %rax,%r12
mov    %rax,%rdi
call   10a0 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   10f0 <fstat@plt>
mov    (%rsp),%ebp
lea    0xdc7(%rip),%rdi
xor    %r15d,%r15d
mov    %ebp,%esi
xor    %eax,%eax
call   1090 <printf@plt>
cvtsi2sd %rbp,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1277 <main+0xa7>
sqrtsd %xmm0,%xmm0
jmp    127c <main+0xac>
lea    0xda9(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r15d
jmp    12cf <main+0xff>
call   10d0 <sqrt@plt>
cvttsd2si %xmm0,%r13
mov    %rbp,%rdi
call   10b0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %r13d,%edx
imul   %r13d,%edx
movswl %r13w,%ecx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ecx,%r8d
call   1080 <tga_save@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [19 x i8] c"Usage: <src-file>\0A\00"
@.str.1 = [7 x i8] c"%s.tga\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [10 x i8] c"size: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i8*
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca double
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8 0, i8* %7
store i8* null, i8** %8
store i32* null, i32** %14
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %79
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i64 @strlen(i8* %22)
%24 = add i64 %23, 1
%25 = add i64 %24, 4
%26 = call i64 @malloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %36, i32** %14
%37 = load i32*, i32** %14
%38 = icmp ne i32* %37, null
br i1 %38, label %40, label %39
39: ; preds = %19
store i32 1, i32* %3
br label %79
40: ; preds = %19
%41 = load i32*, i32** %14
%42 = call i32 @fileno(i32* %41)
%43 = call i32 @fstat(i32 %42, %struct.stat* %9)
%44 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%45 = load i32, i32* %44
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %10
%49 = uitofp i32 %48 to double
%50 = call double @sqrt(double %49)
store double %50, double* %11
%51 = load double, double* %11
%52 = fptoui double %51 to i32
store i32 %52, i32* %12
%53 = load double, double* %11
%54 = fptoui double %53 to i32
store i32 %54, i32* %13
%55 = load i32, i32* %10
%56 = zext i32 %55 to i64
%57 = call i64 @malloc(i64 %56)
%58 = inttoptr i64 %57 to i8*
store i8* %58, i8** %6
%59 = load i8*, i8** %6
%60 = load i32, i32* %10
%61 = load i32*, i32** %14
%62 = call i32 @fread(i8* %59, i32 1, i32 %60, i32* %61)
%63 = load i32*, i32** %14
%64 = call i32 @fclose(i32* %63)
%65 = load i8*, i8** %8
%66 = load i8*, i8** %6
%67 = load i32, i32* %12
%68 = load i32, i32* %13
%69 = mul i32 %67, %68
%70 = load i32, i32* %12
%71 = trunc i32 %70 to i16
%72 = load i32, i32* %13
%73 = trunc i32 %72 to i16
%74 = call i32 @tga_save(i8* %65, i8* %66, i32 %69, i16 signext %71, i16 signext %73)
%75 = load i8*, i8** %6
%76 = call i32 @free(i8* %75)
%77 = load i8*, i8** %8
%78 = call i32 @free(i8* %77)
store i32 0, i32* %3
br label %79
79: ; preds = %40, %39, %17
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [7 x i8] c"%s.tga\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [10 x i8] c"size: %d\0A\00"
@str = [18 x i8] c"Usage: <src-file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %39
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @strlen(i8* dereferenceable(1) %10)
%12 = add i64 %11, 5
%13 = tail call i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
%15 = load i8*, i8** %9
%16 = tail call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %9
%18 = tail call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %39, label %20
20: ; preds = %8
%21 = tail call i32 @fileno(i32* %18)
%22 = call i32 @fstat(i32 %21, %struct.stat* %3)
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%24 = load i32, i32* %23
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = uitofp i32 %24 to double
%27 = tail call double @sqrt(double %26)
%28 = fptoui double %27 to i32
%29 = zext i32 %24 to i64
%30 = tail call i64 @malloc(i64 %29)
%31 = inttoptr i64 %30 to i8*
%32 = tail call i32 @fread(i8* %31, i32 1, i32 %24, i32* %18)
%33 = tail call i32 @fclose(i32* %18)
%34 = mul i32 %28, %28
%35 = trunc i32 %28 to i16
%36 = tail call i32 @tga_save(i8* %14, i8* %31, i32 %34, i16 signext %35, i16 signext %35)
%37 = tail call i32 @free(i8* %31)
%38 = tail call i32 @free(i8* %14)
br label %39
39: ; preds = %8, %20, %6
%40 = phi i32 [ 0, %20 ], [ 1, %6 ], [ 1, %8 ]
ret i32 %40
}