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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
int sfw_to_jfif(unsigned char *, unsigned char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
scalar_t__ strcasecmp(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char **argv) {
  struct stat filestat;
  int retval;
  size_t sretval, namelen;
  char mesg[256];
  char infilename[256];
  char outfilename[256];
  unsigned char *filebuf;
  FILE *infile;
  if ((argc == 1) || (argc > 3)) {
    fprintf(stderr, "\nusage: sfwjpg inputfile [-|outputfile]\n\n");
    fprintf(stderr, "where inputfile is the .sfw file which\n");
    fprintf(stderr, "will be converted to .jpg (JFIF) format,\n");
    fprintf(stderr, "and outputfile is the name of the resulting\n");
    fprintf(stderr, "file.  - specifies standard output.\n");
    fprintf(stderr, "If no output file is specified, the .sfw in\n");
    fprintf(stderr, "the input file will be replaced with .jpg, and\n");
    fprintf(stderr, "the resulting filename will be used.\n\n");
    exit(1);
  }
  strncpy(infilename, argv[1], 255);
  namelen = strlen(infilename);
  if (strcasecmp(infilename + (namelen - 4), ".sfw") != 0) {
    strcat(infilename, ".sfw");
    namelen += 4;
  }
  if (argc == 3) {
    if (strcasecmp(argv[2], "-") == 0)
      outfilename[0] = '\0';
    else
      strncpy(outfilename, argv[2], 255);
  } else {
    strcpy(outfilename, infilename);
    strcpy(outfilename + (namelen - 4), ".jpg");
  }
  retval = stat(infilename, &filestat);
  if (retval == -1) {
    sprintf(mesg, "Error getting status for file '%s'", infilename);
    fprintf(stderr, "\n");
    perror(mesg);
    fprintf(stderr, "\n");
    exit(1);
  }
  filebuf = (unsigned char *)malloc((size_t)filestat.st_size);
  if (filebuf == NULL) {
    fprintf(stderr, "\n");
    perror("Error allocating memory for filebuf");
    fprintf(stderr, "\n");
    exit(1);
  }
  infile = fopen(infilename, "rb");
  if (infile == NULL) {
    sprintf(mesg, "Error opening file '%s'", infilename);
    fprintf(stderr, "\n");
    perror(mesg);
    fprintf(stderr, "\n");
    exit(1);
  }
  sretval = fread(filebuf, filestat.st_size, 1, infile);
  if (sretval == 0) {
    sprintf(mesg, "Error reading file '%s'", infilename);
    fprintf(stderr, "\n");
    perror(mesg);
    fprintf(stderr, "\n");
    exit(1);
  }
  fclose(infile);
  retval = sfw_to_jfif(filebuf, filebuf + filestat.st_size - 1, outfilename);
  if (retval != 0) {
    fprintf(stderr, "Conversion to %s failed.  Exiting.\n\n", outfilename);
    exit(1);
  }
  free((void *)filebuf);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
je     122d <main+0x2d>
cmpl   $0x3,-0x8(%rbp)
jle    12ef <main+0xef>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0xff,%edx
call   1050 <strncpy@plt>
lea    -0x230(%rbp),%rdi
call   1090 <strlen@plt>
mov    %rax,-0x30(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x30(%rbp),%rax
sub    $0x4,%rax
add    %rax,%rdi
lea    0xe21(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
je     135f <main+0x15f>
lea    -0x230(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1100 <strcat@plt>
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    13b1 <main+0x1b1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xddf(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1393 <main+0x193>
movb   $0x0,-0x330(%rbp)
jmp    13ac <main+0x1ac>
lea    -0x330(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    $0xff,%edx
call   1050 <strncpy@plt>
jmp    13e2 <main+0x1e2>
lea    -0x330(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1060 <strcpy@plt>
lea    -0x330(%rbp),%rdi
mov    -0x30(%rbp),%rax
sub    $0x4,%rax
add    %rax,%rdi
lea    0xd7c(%rip),%rsi
call   1060 <strcpy@plt>
lea    -0x230(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    145d <main+0x25d>
lea    -0x130(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xd4a(%rip),%rsi
call   1110 <sprintf@plt>
mov    0x2bc0(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
lea    -0x130(%rbp),%rdi
call   10f0 <perror@plt>
mov    0x2b9d(%rip),%rax
mov    (%rax),%edi
lea    0xd04(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
movslq -0x18(%rbp),%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x338(%rbp)
cmpq   $0x0,-0x338(%rbp)
jne    14bf <main+0x2bf>
mov    0x2b5e(%rip),%rax
mov    (%rax),%edi
lea    0xcc5(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
lea    0xce8(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2b3b(%rip),%rax
mov    (%rax),%edi
lea    0xca2(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    -0x230(%rbp),%rdi
lea    0xcd8(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x340(%rbp)
cmpq   $0x0,-0x340(%rbp)
jne    1545 <main+0x345>
lea    -0x130(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xcac(%rip),%rsi
call   1110 <sprintf@plt>
mov    0x2ad8(%rip),%rax
mov    (%rax),%edi
lea    0xc3f(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
lea    -0x130(%rbp),%rdi
call   10f0 <perror@plt>
mov    0x2ab5(%rip),%rax
mov    (%rax),%edi
lea    0xc1c(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x338(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x340(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    15cd <main+0x3cd>
lea    -0x130(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xc3c(%rip),%rsi
call   1110 <sprintf@plt>
mov    0x2a50(%rip),%rax
mov    (%rax),%edi
lea    0xbb7(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
lea    -0x130(%rbp),%rdi
call   10f0 <perror@plt>
mov    0x2a2d(%rip),%rax
mov    (%rax),%edi
lea    0xb94(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x340(%rbp),%rdi
call   1080 <fclose@plt>
mov    -0x338(%rbp),%rdi
mov    -0x338(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
add    $0xffffffffffffffff,%rsi
lea    -0x330(%rbp),%rdx
call   10d0 <sfw_to_jfif@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1633 <main+0x433>
mov    0x29ce(%rip),%rax
mov    (%rax),%edi
lea    -0x330(%rbp),%rdx
lea    0xbb6(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    -0x338(%rbp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1120 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x318,%rsp
cmp    $0x1,%edi
je     12a5 <main+0xa5>
mov    %edi,%ebp
cmp    $0x4,%edi
jge    12a5 <main+0xa5>
mov    %rsi,%r14
mov    0x8(%rsi),%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xff,%edx
call   1050 <strncpy@plt>
mov    %r15,%rdi
call   1090 <strlen@plt>
mov    %rax,%rbx
lea    (%rax,%r15,1),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xefe(%rip),%rsi
call   1040 <strcasecmp@plt>
test   %rax,%rax
je     1273 <main+0x73>
lea    0xeed(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1100 <strcat@plt>
add    $0x4,%rbx
cmp    $0x3,%ebp
jne    132a <main+0x12a>
mov    0x10(%r14),%rbx
lea    0xed0(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcasecmp@plt>
test   %rax,%rax
jne    1355 <main+0x155>
movb   $0x0,0x110(%rsp)
jmp    136a <main+0x16a>
mov    0x2d34(%rip),%rbx
mov    (%rbx),%edi
lea    0xd4b(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd65(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd7d(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd97(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xdb4(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xdc9(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xde6(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe06(%rip),%rsi
jmp    144a <main+0x24a>
lea    0x110(%rsp),%rbp
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
call   1060 <strcpy@plt>
lea    (%rbx,%rbp,1),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xe0b(%rip),%rsi
call   1060 <strcpy@plt>
jmp    136a <main+0x16a>
lea    0x110(%rsp),%rdi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1050 <strncpy@plt>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    138a <main+0x18a>
lea    0xdd9(%rip),%rsi
jmp    140e <main+0x20e>
movslq 0x8(%rsp),%rbp
mov    %rbp,%rdi
call   10c0 <malloc@plt>
test   %rax,%rax
jne    13cb <main+0x1cb>
mov    0x2c3d(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xda3(%rip),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
lea    0xdc3(%rip),%rdi
call   10f0 <perror@plt>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
jmp    144a <main+0x24a>
mov    %rax,%rbx
lea    0xdd0(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
jne    13ed <main+0x1ed>
lea    0xdbd(%rip),%rsi
jmp    140e <main+0x20e>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fread@plt>
test   %rax,%rax
jne    145b <main+0x25b>
lea    0xdb2(%rip),%rsi
lea    0x210(%rsp),%r14
lea    0x10(%rsp),%rdx
mov    %r14,%rdi
call   1110 <sprintf@plt>
mov    0x2bb6(%rip),%rbx
mov    (%rbx),%edi
lea    0xd1d(%rip),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %r14,%rdi
call   10f0 <perror@plt>
mov    (%rbx),%edi
mov    %rbp,%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    %r14,%rdi
call   1080 <fclose@plt>
lea    (%rbx,%rbp,1),%rsi
add    $0xffffffffffffffff,%rsi
lea    0x110(%rsp),%rdx
mov    %rbx,%rdi
call   10d0 <sfw_to_jfif@plt>
test   %eax,%eax
jne    148e <main+0x28e>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1120 <exit@plt>
mov    0x2b4b(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
lea    0x110(%rsp),%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [42 x i8] c"\0Ausage: sfwjpg inputfile [-|outputfile]\0A\0A\00"
@.str.1 = [40 x i8] c"where inputfile is the .sfw file which\0A\00"
@.str.2 = [42 x i8] c"will be converted to .jpg (JFIF) format,\0A\00"
@.str.3 = [45 x i8] c"and outputfile is the name of the resulting\0A\00"
@.str.4 = [37 x i8] c"file. - specifies standard output.\0A\00"
@.str.5 = [45 x i8] c"If no output file is specified, the .sfw in\0A\00"
@.str.6 = [48 x i8] c"the input file will be replaced with .jpg, and\0A\00"
@.str.7 = [39 x i8] c"the resulting filename will be used.\0A\0A\00"
@.str.8 = [5 x i8] c".sfw\00"
@.str.9 = [2 x i8] c"-\00"
@.str.10 = [5 x i8] c".jpg\00"
@.str.11 = [35 x i8] c"Error getting status for file '%s'\00"
@.str.12 = [2 x i8] c"\0A\00"
@.str.13 = [36 x i8] c"Error allocating memory for filebuf\00"
@.str.14 = [3 x i8] c"rb\00"
@.str.15 = [24 x i8] c"Error opening file '%s'\00"
@.str.16 = [24 x i8] c"Error reading file '%s'\00"
@.str.17 = [37 x i8] c"Conversion to %s failed. Exiting.\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca [256 x i8]
%11 = alloca [256 x i8]
%12 = alloca [256 x i8]
%13 = alloca i8*
%14 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 1
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 3
br i1 %19, label %20, label %38
20: ; preds = %17, %2
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0))
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %17
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @strncpy(i8* %39, i8* %42, i32 255)
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%45 = call i64 @strlen(i8* %44)
store i64 %45, i64* %9
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%47 = load i64, i64* %9
%48 = sub i64 %47, 4
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = call i64 @strcasecmp(i8* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %38
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%54 = call i32 @strcat(i8* %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%55 = load i64, i64* %9
%56 = add i64 %55, 4
store i64 %56, i64* %9
br label %57
57: ; preds = %52, %38
%58 = load i32, i32* %4
%59 = icmp eq i32 %58, 3
br i1 %59, label %60, label %75
60: ; preds = %57
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 2
%63 = load i8*, i8** %62
%64 = call i64 @strcasecmp(i8* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
store i8 0, i8* %67
br label %74
68: ; preds = %60
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 2
%72 = load i8*, i8** %71
%73 = call i32 @strncpy(i8* %69, i8* %72, i32 255)
br label %74
74: ; preds = %68, %66
br label %84
75: ; preds = %57
%76 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%77 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%78 = call i32 @strcpy(i8* %76, i8* %77)
%79 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%80 = load i64, i64* %9
%81 = sub i64 %80, 4
%82 = getelementptr inbounds i8, i8* %79, i64 %81
%83 = call i32 @strcpy(i8* %82, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0))
br label %84
84: ; preds = %75, %74
%85 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%86 = call i32 @stat(i8* %85, %struct.stat* %6)
store i32 %86, i32* %7
%87 = load i32, i32* %7
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %100
89: ; preds = %84
%90 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%91 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%92 = call i32 @sprintf(i8* %90, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.11, i64 0, i64 0), i8* %91)
%93 = load i32, i32* @stderr
%94 = call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%95 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%96 = call i32 @perror(i8* %95)
%97 = load i32, i32* @stderr
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%99 = call i32 @exit(i32 1)
unreachable
100: ; preds = %84
%101 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%102 = load i32, i32* %101
%103 = sext i32 %102 to i64
%104 = call i64 @malloc(i64 %103)
%105 = inttoptr i64 %104 to i8*
store i8* %105, i8** %13
%106 = load i8*, i8** %13
%107 = icmp eq i8* %106, null
br i1 %107, label %108, label %115
108: ; preds = %100
%109 = load i32, i32* @stderr
%110 = call i32 (i32, i8*, ...) @fprintf(i32 %109, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%111 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.13, i64 0, i64 0))
%112 = load i32, i32* @stderr
%113 = call i32 (i32, i8*, ...) @fprintf(i32 %112, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%114 = call i32 @exit(i32 1)
unreachable
115: ; preds = %100
%116 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%117 = call i32* @fopen(i8* %116, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i64 0, i64 0))
store i32* %117, i32** %14
%118 = load i32*, i32** %14
%119 = icmp eq i32* %118, null
br i1 %119, label %120, label %131
120: ; preds = %115
%121 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%122 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%123 = call i32 @sprintf(i8* %121, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.15, i64 0, i64 0), i8* %122)
%124 = load i32, i32* @stderr
%125 = call i32 (i32, i8*, ...) @fprintf(i32 %124, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%126 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%127 = call i32 @perror(i8* %126)
%128 = load i32, i32* @stderr
%129 = call i32 (i32, i8*, ...) @fprintf(i32 %128, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%130 = call i32 @exit(i32 1)
unreachable
131: ; preds = %115
%132 = load i8*, i8** %13
%133 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%134 = load i32, i32* %133
%135 = load i32*, i32** %14
%136 = call i64 @fread(i8* %132, i32 %134, i32 1, i32* %135)
store i64 %136, i64* %8
%137 = load i64, i64* %8
%138 = icmp eq i64 %137, 0
br i1 %138, label %139, label %150
139: ; preds = %131
%140 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%141 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%142 = call i32 @sprintf(i8* %140, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.16, i64 0, i64 0), i8* %141)
%143 = load i32, i32* @stderr
%144 = call i32 (i32, i8*, ...) @fprintf(i32 %143, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%145 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%146 = call i32 @perror(i8* %145)
%147 = load i32, i32* @stderr
%148 = call i32 (i32, i8*, ...) @fprintf(i32 %147, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%149 = call i32 @exit(i32 1)
unreachable
150: ; preds = %131
%151 = load i32*, i32** %14
%152 = call i32 @fclose(i32* %151)
%153 = load i8*, i8** %13
%154 = load i8*, i8** %13
%155 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%156 = load i32, i32* %155
%157 = sext i32 %156 to i64
%158 = getelementptr inbounds i8, i8* %154, i64 %157
%159 = getelementptr inbounds i8, i8* %158, i64 -1
%160 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%161 = call i32 @sfw_to_jfif(i8* %153, i8* %159, i8* %160)
store i32 %161, i32* %7
%162 = load i32, i32* %7
%163 = icmp ne i32 %162, 0
br i1 %163, label %164, label %169
164: ; preds = %150
%165 = load i32, i32* @stderr
%166 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%167 = call i32 (i32, i8*, ...) @fprintf(i32 %165, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.17, i64 0, i64 0), i8* %166)
%168 = call i32 @exit(i32 1)
unreachable
169: ; preds = %150
%170 = load i8*, i8** %13
%171 = call i32 @free(i8* %170)
%172 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [42 x i8] c"\0Ausage: sfwjpg inputfile [-|outputfile]\0A\0A\00"
@.str.1 = [40 x i8] c"where inputfile is the .sfw file which\0A\00"
@.str.2 = [42 x i8] c"will be converted to .jpg (JFIF) format,\0A\00"
@.str.3 = [45 x i8] c"and outputfile is the name of the resulting\0A\00"
@.str.4 = [37 x i8] c"file. - specifies standard output.\0A\00"
@.str.5 = [45 x i8] c"If no output file is specified, the .sfw in\0A\00"
@.str.6 = [48 x i8] c"the input file will be replaced with .jpg, and\0A\00"
@.str.7 = [39 x i8] c"the resulting filename will be used.\0A\0A\00"
@.str.8 = [5 x i8] c".sfw\00"
@.str.9 = [2 x i8] c"-\00"
@.str.10 = [5 x i8] c".jpg\00"
@.str.11 = [35 x i8] c"Error getting status for file '%s'\00"
@.str.12 = [2 x i8] c"\0A\00"
@.str.13 = [36 x i8] c"Error allocating memory for filebuf\00"
@.str.14 = [3 x i8] c"rb\00"
@.str.15 = [24 x i8] c"Error opening file '%s'\00"
@.str.16 = [24 x i8] c"Error reading file '%s'\00"
@.str.17 = [37 x i8] c"Conversion to %s failed. Exiting.\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = bitcast %struct.stat* %3 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%11 = icmp eq i32 %0, 1
%12 = icmp sgt i32 %0, 3
%13 = or i1 %11, %12
br i1 %13, label %14, label %32
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0))
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %2
%33 = getelementptr inbounds i8*, i8** %1, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @strncpy(i8* %9, i8* %34, i32 255)
%36 = call i64 @strlen(i8* %9)
%37 = add i64 %36, -4
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %37
%39 = call i64 @strcasecmp(i8* %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %32
%42 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%43 = add i64 %36, 4
br label %44
44: ; preds = %41, %32
%45 = phi i64 [ %43, %41 ], [ %36, %32 ]
%46 = icmp eq i32 %0, 3
br i1 %46, label %47, label %55
47: ; preds = %44
%48 = getelementptr inbounds i8*, i8** %1, i64 2
%49 = load i8*, i8** %48
%50 = call i64 @strcasecmp(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
store i8 0, i8* %10
br label %60
53: ; preds = %47
%54 = call i32 @strncpy(i8* %10, i8* %49, i32 255)
br label %60
55: ; preds = %44
%56 = call i32 @strcpy(i8* %10, i8* %9)
%57 = add i64 %45, -4
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %57
%59 = call i32 @strcpy(i8* %58, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.10, i64 0, i64 0))
br label %60
60: ; preds = %52, %53, %55
%61 = call i32 @stat(i8* %9, %struct.stat* %3)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %71
63: ; preds = %60
%64 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.11, i64 0, i64 0), i8* %9)
%65 = load i32, i32* @stderr
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%67 = call i32 @perror(i8* %8)
%68 = load i32, i32* @stderr
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %60
%72 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%73 = load i32, i32* %72
%74 = sext i32 %73 to i64
%75 = call i64 @malloc(i64 %74)
%76 = inttoptr i64 %75 to i8*
%77 = icmp eq i64 %75, 0
br i1 %77, label %78, label %85
78: ; preds = %71
%79 = load i32, i32* @stderr
%80 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%81 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.13, i64 0, i64 0))
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%84 = call i32 @exit(i32 1)
unreachable
85: ; preds = %71
%86 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i64 0, i64 0))
%87 = icmp eq i32* %86, null
br i1 %87, label %88, label %96
88: ; preds = %85
%89 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.15, i64 0, i64 0), i8* %9)
%90 = load i32, i32* @stderr
%91 = call i32 (i32, i8*, ...) @fprintf(i32 %90, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%92 = call i32 @perror(i8* %8)
%93 = load i32, i32* @stderr
%94 = call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%95 = call i32 @exit(i32 1)
unreachable
96: ; preds = %85
%97 = call i64 @fread(i8* %76, i32 %73, i32 1, i32* %86)
%98 = icmp eq i64 %97, 0
br i1 %98, label %99, label %107
99: ; preds = %96
%100 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.16, i64 0, i64 0), i8* %9)
%101 = load i32, i32* @stderr
%102 = call i32 (i32, i8*, ...) @fprintf(i32 %101, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%103 = call i32 @perror(i8* %8)
%104 = load i32, i32* @stderr
%105 = call i32 (i32, i8*, ...) @fprintf(i32 %104, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%106 = call i32 @exit(i32 1)
unreachable
107: ; preds = %96
%108 = call i32 @fclose(i32* %86)
%109 = getelementptr inbounds i8, i8* %76, i64 %74
%110 = getelementptr inbounds i8, i8* %109, i64 -1
%111 = call i32 @sfw_to_jfif(i8* %76, i8* %110, i8* %10)
%112 = icmp eq i32 %111, 0
br i1 %112, label %117, label %113
113: ; preds = %107
%114 = load i32, i32* @stderr
%115 = call i32 (i32, i8*, ...) @fprintf(i32 %114, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.17, i64 0, i64 0), i8* %10)
%116 = call i32 @exit(i32 1)
unreachable
117: ; preds = %107
%118 = call i32 @free(i8* %76)
%119 = call i32 @exit(i32 0)
unreachable
}