#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 json_object {
  int dummy;
};

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
char *json_object_to_json_string(struct json_object *);
int /*<<< orphan*/ mc_error(char *, ...);
int open(char *, int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
int json_object_to_file(char *filename, struct json_object *obj) {
  char *json_str;
  int fd, ret, wpos, wsize;
  if (!obj) {
    mc_error("json_object_to_file: object is null\n");
    return -1;
  }
  if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
    mc_error("json_object_to_file: error opening file %s: %s\n", filename,
             strerror(errno));
    return -1;
  }
  if (!(json_str = json_object_to_json_string(obj)))
    return -1;
  wsize = strlen(json_str);
  wpos = 0;
  while (wpos < wsize) {
    if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0) {
      close(fd);
      mc_error("json_object_to_file: error writing file %s: %s\n", filename,
               strerror(errno));
      return -1;
    }
    wpos += ret;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<json_object_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a5 <json_object_to_file+0x35>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1080 <mc_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <json_object_to_file+0x162>
mov    -0x10(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1210 <json_object_to_file+0xa0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1080 <mc_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <json_object_to_file+0x162>
mov    -0x18(%rbp),%rdi
call   1030 <json_object_to_json_string@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1233 <json_object_to_file+0xc3>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <json_object_to_file+0x162>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    12c3 <json_object_to_file+0x153>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x30(%rbp),%edx
sub    -0x2c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    12b5 <json_object_to_file+0x145>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1080 <mc_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <json_object_to_file+0x162>
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1246 <json_object_to_file+0xd6>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<json_object_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     120a <json_object_to_file+0x9a>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e2f(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     1220 <json_object_to_file+0xb0>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1030 <json_object_to_json_string@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1272 <json_object_to_file+0x102>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    11fd <json_object_to_file+0x8d>
mov    %eax,%ebx
xor    %ebp,%ebp
movslq %ebp,%rax
lea    (%rax,%r13,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1040 <write@plt>
test   %eax,%eax
js     1249 <json_object_to_file+0xd9>
add    %eax,%ebp
cmp    %ebp,%ebx
jg     11e0 <json_object_to_file+0x70>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %r15d,%r15d
jmp    1272 <json_object_to_file+0x102>
lea    0xdef(%rip),%rdi
xor    %eax,%eax
call   1080 <mc_error@plt>
mov    $0xffffffff,%r15d
jmp    1272 <json_object_to_file+0x102>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf0(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <mc_error@plt>
mov    $0xffffffff,%r15d
jmp    1272 <json_object_to_file+0x102>
mov    %r12d,%edi
call   1060 <close@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdef(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <mc_error@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.json_object = type { i32 }
@.str = [37 x i8] c"json_object_to_file: object is null\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [48 x i8] c"json_object_to_file: error opening file %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [48 x i8] c"json_object_to_file: error writing file %s: %s\0A\00"
define i32 @json_object_to_file(i8* %0, %struct.json_object* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.json_object*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store %struct.json_object* %1, %struct.json_object** %5
%11 = load %struct.json_object*, %struct.json_object** %5
%12 = icmp ne %struct.json_object* %11, null
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 (i8*, ...) @mc_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %16, i32 %21, i32 420)
store i32 %22, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %15
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 (i8*, ...) @mc_error(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 %27)
store i32 -1, i32* %3
br label %66
29: ; preds = %15
%30 = load %struct.json_object*, %struct.json_object** %5
%31 = call i8* @json_object_to_json_string(%struct.json_object* %30)
store i8* %31, i8** %6
%32 = icmp ne i8* %31, null
br i1 %32, label %34, label %33
33: ; preds = %29
store i32 -1, i32* %3
br label %66
34: ; preds = %29
%35 = load i8*, i8** %6
%36 = call i32 @strlen(i8* %35)
store i32 %36, i32* %10
store i32 0, i32* %9
br label %37
37: ; preds = %59, %34
%38 = load i32, i32* %9
%39 = load i32, i32* %10
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %63
41: ; preds = %37
%42 = load i32, i32* %7
%43 = load i8*, i8** %6
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i32, i32* %10
%48 = load i32, i32* %9
%49 = sub nsw i32 %47, %48
%50 = call i32 @write(i32 %42, i8* %46, i32 %49)
store i32 %50, i32* %8
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %41
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
%55 = load i8*, i8** %4
%56 = load i32, i32* @errno
%57 = call i32 @strerror(i32 %56)
%58 = call i32 (i8*, ...) @mc_error(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %55, i32 %57)
store i32 -1, i32* %3
br label %66
59: ; preds = %41
%60 = load i32, i32* %8
%61 = load i32, i32* %9
%62 = add nsw i32 %61, %60
store i32 %62, i32* %9
br label %37
63: ; preds = %37
%64 = load i32, i32* %7
%65 = call i32 @close(i32 %64)
store i32 0, i32* %3
br label %66
66: ; preds = %63, %52, %33, %24, %13
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.json_object = type { i32 }
@.str = [37 x i8] c"json_object_to_file: object is null\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [48 x i8] c"json_object_to_file: error opening file %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [48 x i8] c"json_object_to_file: error writing file %s: %s\0A\00"
define i32 @json_object_to_file(i8* %0, %struct.json_object* %1) {
%3 = icmp eq %struct.json_object* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @mc_error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %41
6: ; preds = %2
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %8, %7
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %0, i32 %11, i32 420)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %6
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @mc_error(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %16)
br label %41
18: ; preds = %6
%19 = tail call i8* @json_object_to_json_string(%struct.json_object* %1)
%20 = icmp eq i8* %19, null
br i1 %20, label %41, label %21
21: ; preds = %18
%22 = tail call i32 @strlen(i8* %19)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %26, label %39
24: ; preds = %26
%25 = icmp sgt i32 %22, %33
br i1 %25, label %26, label %39
26: ; preds = %21, %24
%27 = phi i32 [ %33, %24 ], [ 0, %21 ]
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %19, i64 %28
%30 = sub nsw i32 %22, %27
%31 = tail call i32 @write(i32 %12, i8* %29, i32 %30)
%32 = icmp slt i32 %31, 0
%33 = add nsw i32 %31, %27
br i1 %32, label %34, label %24
34: ; preds = %26
%35 = tail call i32 @close(i32 %12)
%36 = load i32, i32* @errno
%37 = tail call i32 @strerror(i32 %36)
%38 = tail call i32 (i8*, ...) @mc_error(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %37)
br label %41
39: ; preds = %24, %21
%40 = tail call i32 @close(i32 %12)
br label %41
41: ; preds = %18, %39, %34, %14, %4
%42 = phi i32 [ -1, %14 ], [ -1, %34 ], [ 0, %39 ], [ -1, %4 ], [ -1, %18 ]
ret i32 %42
}