#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 szm_ctx {
  int dummy;
};
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
char *malloc(int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stat(char *, struct stat *);
int szm_box_bmp(struct szm_ctx *, char *, int /*<<< orphan*/, int, void **,
                int *);
int /*<<< orphan*/ szm_fini(struct szm_ctx *);
struct szm_ctx *szm_init(char *);
int /*<<< orphan*/ write(int, void *, int);
int main(int argc, char *argv[]) {
  struct szm_ctx *ctx;
  void *out;
  int size;
  int fd;
  struct stat st;
  char *in;
  int ret;
  if (argc == 1)
    ctx = szm_init("127.0.0.1");
  else
    ctx = szm_init(argv[1]);
  fd = open("in.bmp", O_RDONLY);
  stat("in.bmp", &st);
  in = malloc(st.st_size);
  read(fd, in, st.st_size);
  close(fd);
  ret = szm_box_bmp(ctx, in, st.st_size, 400, &out, &size);
  if (ret < 0) {
    printf("err code is %d\n", ret);
    return 0;
  }
  printf("%d\n", size);
  fd = open("out.jpg", O_WRONLY | O_CREAT | O_TRUNC, 0777);
  write(fd, out, size);
  close(fd);
  szm_fini(ctx);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11d5 <main+0x35>
lea    0xe39(%rip),%rdi
call   1070 <szm_init@plt>
mov    %rax,-0x18(%rbp)
jmp    11e6 <main+0x46>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <szm_init@plt>
mov    %rax,-0x18(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x28(%rbp)
lea    0xe03(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x30(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1060 <read@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    $0x190,%ecx
lea    -0x20(%rbp),%r8
lea    -0x24(%rbp),%r9
call   10b0 <szm_box_bmp@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1278 <main+0xd8>
mov    -0x3c(%rbp),%esi
lea    0xdaa(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12df <main+0x13f>
mov    -0x24(%rbp),%esi
lea    0xd9b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
mov    0x2d61(%rip),%rax
or     (%rax),%esi
mov    0x2d50(%rip),%rax
or     (%rax),%esi
lea    0xd78(%rip),%rdi
mov    $0x1ff,%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <szm_fini@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jne    11b8 <main+0x18>
lea    0xe4a(%rip),%rdi
jmp    11bc <main+0x1c>
mov    0x8(%rsi),%rdi
call   1070 <szm_init@plt>
mov    %rax,%r14
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%r15d
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1060 <read@plt>
mov    %r15d,%edi
call   1050 <close@plt>
lea    0x10(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
mov    $0x190,%ecx
call   10b0 <szm_box_bmp@plt>
test   %eax,%eax
js     1292 <main+0xf2>
mov    0x4(%rsp),%esi
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d78(%rip),%rax
mov    0x2da9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d96(%rip),%rax
or     (%rax),%esi
lea    0xdbe(%rip),%rdi
mov    $0x1ff,%edx
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%ebx
mov    0x10(%rsp),%rsi
mov    0x4(%rsp),%edx
mov    %eax,%edi
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14,%rdi
call   10c0 <szm_fini@plt>
jmp    12a0 <main+0x100>
lea    0xd78(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.szm_ctx = type { i32 }
%struct.stat = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [7 x i8] c"in.bmp\00"
@O_RDONLY = global i32 0
@.str.2 = [16 x i8] c"err code is %d\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [8 x i8] c"out.jpg\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.szm_ctx*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i8*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call %struct.szm_ctx* @szm_init(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store %struct.szm_ctx* %16, %struct.szm_ctx** %6
br label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call %struct.szm_ctx* @szm_init(i8* %20)
store %struct.szm_ctx* %21, %struct.szm_ctx** %6
br label %22
22: ; preds = %17, %15
%23 = load i32, i32* @O_RDONLY
%24 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 %24, i32* %9
%25 = call i32 @stat(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %10)
%26 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %11
%29 = load i32, i32* %9
%30 = load i8*, i8** %11
%31 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @read(i32 %29, i8* %30, i32 %32)
%34 = load i32, i32* %9
%35 = call i32 @close(i32 %34)
%36 = load %struct.szm_ctx*, %struct.szm_ctx** %6
%37 = load i8*, i8** %11
%38 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @szm_box_bmp(%struct.szm_ctx* %36, i8* %37, i32 %39, i32 400, i8** %7, i32* %8)
store i32 %40, i32* %12
%41 = load i32, i32* %12
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %22
%44 = load i32, i32* %12
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %44)
store i32 0, i32* %3
br label %63
46: ; preds = %22
%47 = load i32, i32* %8
%48 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = load i32, i32* @O_WRONLY
%50 = load i32, i32* @O_CREAT
%51 = or i32 %49, %50
%52 = load i32, i32* @O_TRUNC
%53 = or i32 %51, %52
%54 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %53, i32 511)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = load i8*, i8** %7
%57 = load i32, i32* %8
%58 = call i32 @write(i32 %55, i8* %56, i32 %57)
%59 = load i32, i32* %9
%60 = call i32 @close(i32 %59)
%61 = load %struct.szm_ctx*, %struct.szm_ctx** %6
%62 = call i32 @szm_fini(%struct.szm_ctx* %61)
store i32 0, i32* %3
br label %63
63: ; preds = %46, %43
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.szm_ctx = type { i32 }
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [7 x i8] c"in.bmp\00"
@O_RDONLY = global i32 0
@.str.2 = [16 x i8] c"err code is %d\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [8 x i8] c"out.jpg\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = bitcast i8** %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.stat* %5 to i8*
%9 = icmp eq i32 %0, 1
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
br label %13
13: ; preds = %2, %10
%14 = phi i8* [ %12, %10 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %2 ]
%15 = tail call %struct.szm_ctx* @szm_init(i8* %14)
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = call i32 @stat(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %5)
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i8* @malloc(i32 %20)
%22 = tail call i32 @read(i32 %17, i8* %21, i32 %20)
%23 = tail call i32 @close(i32 %17)
%24 = call i32 @szm_box_bmp(%struct.szm_ctx* %15, i8* %21, i32 %20, i32 400, i8** %3, i32* %4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %13
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %42
28: ; preds = %13
%29 = load i32, i32* %4
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @O_WRONLY
%32 = load i32, i32* @O_CREAT
%33 = or i32 %32, %31
%34 = load i32, i32* @O_TRUNC
%35 = or i32 %33, %34
%36 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %35, i32 511)
%37 = load i8*, i8** %3
%38 = load i32, i32* %4
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
%40 = call i32 @close(i32 %36)
%41 = call i32 @szm_fini(%struct.szm_ctx* %15)
br label %42
42: ; preds = %28, %26
ret i32 0
}