#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 /*<<< orphan*/ read(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ szm_fini(struct szm_ctx *);
int szm_head_photo(struct szm_ctx *, char *, int /*<<< orphan*/, void **,
                   int *);
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 < 2) {
    printf("you need a arg\n");
    return -1;
  }
  if (argc == 2)
    ctx = szm_init("127.0.0.1");
  else {
    ctx = szm_init(argv[1]);
    argv++;
  }
  fd = open(argv[1], O_RDONLY);
  stat(argv[1], &st);
  in = malloc(st.st_size);
  read(fd, in, st.st_size);
  close(fd);
  ret = szm_head_photo(ctx, in, st.st_size, &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   $0x2,-0x8(%rbp)
jge    11da <main+0x3a>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <main+0x170>
cmpl   $0x2,-0x8(%rbp)
jne    11f9 <main+0x59>
lea    0xe25(%rip),%rdi
call   1080 <szm_init@plt>
mov    %rax,-0x18(%rbp)
jmp    1216 <main+0x76>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <szm_init@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <stat@plt>
mov    -0x30(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1070 <read@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%r8
call   1050 <szm_head_photo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12a7 <main+0x107>
mov    -0x3c(%rbp),%esi
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1310 <main+0x170>
mov    -0x24(%rbp),%esi
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%esi
mov    0x2d30(%rip),%rax
or     (%rax),%esi
mov    0x2d1f(%rip),%rax
or     (%rax),%esi
lea    0xd50(%rip),%rdi
mov    $0x1ff,%edx
mov    $0x0,%al
call   10b0 <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   1060 <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   %r12
push   %rbx
sub    $0x20,%rsp
cmp    $0x1,%edi
jg     11d8 <main+0x28>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r15d
jmp    12d5 <main+0x125>
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11e9 <main+0x39>
lea    0xe19(%rip),%rdi
jmp    11f1 <main+0x41>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
call   1090 <szm_init@plt>
mov    %rax,%r14
mov    0x8(%rbx),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%r12d
mov    0x8(%rbx),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <stat@plt>
mov    0x10(%rsp),%ebx
mov    %ebx,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbp
mov    %r12d,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1080 <read@plt>
mov    %r12d,%edi
call   1070 <close@plt>
lea    0x18(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1060 <szm_head_photo@plt>
test   %eax,%eax
js     12c2 <main+0x112>
mov    0xc(%rsp),%esi
lea    0xdac(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d48(%rip),%rax
mov    0x2d79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d66(%rip),%rax
or     (%rax),%esi
lea    0xd87(%rip),%rdi
mov    $0x1ff,%edx
xor    %eax,%eax
call   10c0 <open@plt>
mov    %eax,%ebx
mov    0x18(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    %eax,%edi
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r14,%rdi
call   10d0 <szm_fini@plt>
jmp    12d5 <main+0x125>
lea    0xd41(%rip),%rdi
xor    %r15d,%r15d
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.szm_ctx = type { i32 }
%struct.stat = type { i32 }
@.str = [16 x i8] c"you need a arg\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\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 slt i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call %struct.szm_ctx* @szm_init(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store %struct.szm_ctx* %21, %struct.szm_ctx** %6
br label %29
22: ; preds = %17
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call %struct.szm_ctx* @szm_init(i8* %25)
store %struct.szm_ctx* %26, %struct.szm_ctx** %6
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i32 1
store i8** %28, i8*** %5
br label %29
29: ; preds = %22, %20
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_RDONLY
%34 = call i32 (i8*, i32, ...) @open(i8* %32, i32 %33)
store i32 %34, i32* %9
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @stat(i8* %37, %struct.stat* %10)
%39 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i8* @malloc(i32 %40)
store i8* %41, i8** %11
%42 = load i32, i32* %9
%43 = load i8*, i8** %11
%44 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @read(i32 %42, i8* %43, i32 %45)
%47 = load i32, i32* %9
%48 = call i32 @close(i32 %47)
%49 = load %struct.szm_ctx*, %struct.szm_ctx** %6
%50 = load i8*, i8** %11
%51 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @szm_head_photo(%struct.szm_ctx* %49, i8* %50, i32 %52, i8** %7, i32* %8)
store i32 %53, i32* %12
%54 = load i32, i32* %12
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %29
%57 = load i32, i32* %12
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %57)
store i32 0, i32* %3
br label %76
59: ; preds = %29
%60 = load i32, i32* %8
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %60)
%62 = load i32, i32* @O_WRONLY
%63 = load i32, i32* @O_CREAT
%64 = or i32 %62, %63
%65 = load i32, i32* @O_TRUNC
%66 = or i32 %64, %65
%67 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %66, i32 511)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = load i8*, i8** %7
%70 = load i32, i32* %8
%71 = call i32 @write(i32 %68, i8* %69, i32 %70)
%72 = load i32, i32* %9
%73 = call i32 @close(i32 %72)
%74 = load %struct.szm_ctx*, %struct.szm_ctx** %6
%75 = call i32 @szm_fini(%struct.szm_ctx* %74)
store i32 0, i32* %3
br label %76
76: ; preds = %59, %56, %15
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.szm_ctx = type { i32 }
@.str.1 = [10 x i8] c"127.0.0.1\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
@str = [15 x i8] c"you need a arg\00"
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 slt i32 %0, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %50
12: ; preds = %2
%13 = icmp eq i32 %0, 2
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
br label %17
17: ; preds = %12, %14
%18 = phi i8* [ %16, %14 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %12 ]
%19 = phi i8** [ %15, %14 ], [ %1, %12 ]
%20 = tail call %struct.szm_ctx* @szm_init(i8* %18)
%21 = getelementptr inbounds i8*, i8** %19, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
%25 = load i8*, i8** %21
%26 = call i32 @stat(i8* %25, %struct.stat* %5)
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%28 = load i32, i32* %27
%29 = tail call i8* @malloc(i32 %28)
%30 = tail call i32 @read(i32 %24, i8* %29, i32 %28)
%31 = tail call i32 @close(i32 %24)
%32 = call i32 @szm_head_photo(%struct.szm_ctx* %20, i8* %29, i32 %28, i8** %3, i32* %4)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %17
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %50
36: ; preds = %17
%37 = load i32, i32* %4
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @O_WRONLY
%40 = load i32, i32* @O_CREAT
%41 = or i32 %40, %39
%42 = load i32, i32* @O_TRUNC
%43 = or i32 %41, %42
%44 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %43, i32 511)
%45 = load i8*, i8** %3
%46 = load i32, i32* %4
%47 = call i32 @write(i32 %44, i8* %45, i32 %46)
%48 = call i32 @close(i32 %44)
%49 = call i32 @szm_fini(%struct.szm_ctx* %20)
br label %50
50: ; preds = %36, %34, %10
%51 = phi i32 [ -1, %10 ], [ 0, %34 ], [ 0, %36 ]
ret i32 %51
}