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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ close(int);
struct vmconfig *create_vmconfig(char *);
int /*<<< orphan*/ destroy_vmconfig(struct vmconfig *);
int /*<<< orphan*/ free(char *);
char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int /*<<< orphan*/);
char *serialize_vmconfig(struct vmconfig *, int);
scalar_t__ stat(char *, struct stat *);
int main(int argc, char *argv[]) {
  char *filebuf = NULL;
  int fd = 0;
  struct stat st;
  struct vmconfig *vmcfg = NULL;
  char *new = NULL;
  int fmt = 0;
  if (argc < 2) {
    fmt = 1;
    printf("Default format %d.\n", fmt);
  } else {
    fmt = atoi(argv[1]);
  }
  memset(&st, 0, sizeof(struct stat));
  if (stat("vmconfig.json", &st)) {
    return -1;
  }
  fd = open("vmconfig.json", O_RDWR);
  if (fd < 0) {
    return -1;
  }
  filebuf = malloc(st.st_size);
  if (NULL == filebuf) {
    close(fd);
    return -1;
  }
  read(fd, filebuf, st.st_size);
  close(fd);
  printf("%s", filebuf);
  vmcfg = create_vmconfig(filebuf);
  if (NULL == vmcfg) {
    goto out1;
  }
  new = serialize_vmconfig(vmcfg, fmt);
  if (NULL == new) {
    goto out2;
  }
  printf("New json:\n%s\n", new);
out2:
  destroy_vmconfig(vmcfg);
  free(new);
out1:
  free(filebuf);
  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)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1223 <main+0x63>
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1233 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x34(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    0xdca(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
je     1269 <main+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <main+0x19f>
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
lea    0xd9b(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1297 <main+0xd7>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <main+0x19f>
mov    -0x20(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    12c3 <main+0x103>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <main+0x19f>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1090 <read@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%rsi
lea    0xd3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <create_vmconfig@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    130a <main+0x14a>
jmp    134f <main+0x18f>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10e0 <serialize_vmconfig@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    132b <main+0x16b>
jmp    133d <main+0x17d>
mov    -0x30(%rbp),%rsi
lea    0xcef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <destroy_vmconfig@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@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    $0x10,%rsp
cmp    $0x1,%edi
jg     11ec <main+0x2c>
lea    0xe28(%rip),%rdi
mov    $0x1,%r14d
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f8 <main+0x38>
mov    0x8(%rsi),%rdi
call   10d0 <atoi@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    0xe01(%rip),%rdi
mov    %rbx,%rsi
call   10a0 <stat@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1234 <main+0x74>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
lea    0xdd0(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     1225 <main+0x65>
mov    %eax,%ebp
mov    0x8(%rsp),%r15d
mov    %r15d,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12dc <main+0x11c>
mov    %rax,%r12
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1090 <read@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0xda3(%rip),%rdi
xor    %ebx,%ebx
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
call   1040 <create_vmconfig@plt>
test   %rax,%rax
je     12cf <main+0x10f>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14d,%esi
call   10e0 <serialize_vmconfig@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12bf <main+0xff>
lea    0xd70(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1070 <destroy_vmconfig@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1225 <main+0x65>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1225 <main+0x65>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.vmconfig = type { i32 }
@.str = [20 x i8] c"Default format %d.\0A\00"
@.str.1 = [14 x i8] c"vmconfig.json\00"
@O_RDWR = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [14 x i8] c"New json:\0A%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca %struct.vmconfig*
%10 = alloca i8*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i32 0, i32* %7
store %struct.vmconfig* null, %struct.vmconfig** %9
store i8* null, i8** %10
store i32 0, i32* %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
store i32 1, i32* %11
%15 = load i32, i32* %11
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %15)
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 i32 @atoi(i8* %20)
store i32 %21, i32* %11
br label %22
22: ; preds = %17, %14
%23 = call i32 @memset(%struct.stat* %8, i32 0, i32 4)
%24 = call i64 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %8)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %3
br label %75
27: ; preds = %22
%28 = load i32, i32* @O_RDWR
%29 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 -1, i32* %3
br label %75
33: ; preds = %27
%34 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = icmp eq i8* null, %37
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %3
br label %75
42: ; preds = %33
%43 = load i32, i32* %7
%44 = load i8*, i8** %6
%45 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @read(i32 %43, i8* %44, i32 %46)
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
%50 = load i8*, i8** %6
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = load i8*, i8** %6
%53 = call %struct.vmconfig* @create_vmconfig(i8* %52)
store %struct.vmconfig* %53, %struct.vmconfig** %9
%54 = load %struct.vmconfig*, %struct.vmconfig** %9
%55 = icmp eq %struct.vmconfig* null, %54
br i1 %55, label %56, label %57
56: ; preds = %42
br label %72
57: ; preds = %42
%58 = load %struct.vmconfig*, %struct.vmconfig** %9
%59 = load i32, i32* %11
%60 = call i8* @serialize_vmconfig(%struct.vmconfig* %58, i32 %59)
store i8* %60, i8** %10
%61 = load i8*, i8** %10
%62 = icmp eq i8* null, %61
br i1 %62, label %63, label %64
63: ; preds = %57
br label %67
64: ; preds = %57
%65 = load i8*, i8** %10
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %64, %63
%68 = load %struct.vmconfig*, %struct.vmconfig** %9
%69 = call i32 @destroy_vmconfig(%struct.vmconfig* %68)
%70 = load i8*, i8** %10
%71 = call i32 @free(i8* %70)
br label %72
72: ; preds = %67, %56
%73 = load i8*, i8** %6
%74 = call i32 @free(i8* %73)
store i32 0, i32* %3
br label %75
75: ; preds = %72, %39, %32, %26
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.vmconfig = type { i32 }
@.str = [20 x i8] c"Default format %d.\0A\00"
@.str.1 = [14 x i8] c"vmconfig.json\00"
@O_RDWR = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [14 x i8] c"New json:\0A%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 1)
br label %12
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
br label %12
12: ; preds = %8, %6
%13 = phi i32 [ 1, %6 ], [ %11, %8 ]
%14 = call i32 @memset(%struct.stat* %3, i32 0, i32 4)
%15 = call i64 @stat(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %44
17: ; preds = %12
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %44, label %21
21: ; preds = %17
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i8* @malloc(i32 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @close(i32 %19)
br label %44
28: ; preds = %21
%29 = call i32 @read(i32 %19, i8* %24, i32 %23)
%30 = call i32 @close(i32 %19)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%32 = call %struct.vmconfig* @create_vmconfig(i8* %24)
%33 = icmp eq %struct.vmconfig* %32, null
br i1 %33, label %42, label %34
34: ; preds = %28
%35 = call i8* @serialize_vmconfig(%struct.vmconfig* %32, i32 %13)
%36 = icmp eq i8* %35, null
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %35)
br label %39
39: ; preds = %34, %37
%40 = call i32 @destroy_vmconfig(%struct.vmconfig* %32)
%41 = call i32 @free(i8* %35)
br label %42
42: ; preds = %28, %39
%43 = call i32 @free(i8* %24)
br label %44
44: ; preds = %17, %12, %42, %26
%45 = phi i32 [ -1, %26 ], [ 0, %42 ], [ -1, %12 ], [ -1, %17 ]
ret i32 %45
}