#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 UINT_MAX;
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 const *);
int write(int, char const *, unsigned int);
int json_object_to_file(char *filename, struct json_object *obj) {
  const char *json_str;
  int fd, ret;
  unsigned int wpos, wsize;
  if (!obj) {
    if (0)
      mc_error("json_object_to_file: object is null\n");
    return -1;
  }
  if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
    if (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))) {
    close(fd);
    return -1;
  }
  wsize = (unsigned int)(strlen(json_str) & UINT_MAX);
  wpos = 0;
  while (wpos < wsize) {
    if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0) {
      close(fd);
      if (0)
        mc_error("json_object_to_file: error writing file %s: %s\n", filename,
                 strerror(errno));
      return -1;
    }
    wpos += (unsigned int)ret;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<json_object_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1177 <json_object_to_file+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <json_object_to_file+0x110>
mov    -0x10(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e65(%rip),%rax
or     (%rax),%esi
mov    0x2e64(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11b8 <json_object_to_file+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <json_object_to_file+0x110>
mov    -0x18(%rbp),%rdi
call   1030 <json_object_to_json_string@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e3 <json_object_to_file+0x93>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <json_object_to_file+0x110>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    0x2de5(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jae    1251 <json_object_to_file+0x101>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%eax
add    %rax,%rsi
mov    -0x30(%rbp),%edx
sub    -0x2c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1243 <json_object_to_file+0xf3>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <json_object_to_file+0x110>
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11ff <json_object_to_file+0xaf>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<json_object_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%r14d
test   %rsi,%rsi
je     11f6 <json_object_to_file+0xa6>
mov    %rsi,%rbx
mov    0x2e4f(%rip),%rax
mov    0x2e78(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e75(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11f6 <json_object_to_file+0xa6>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <json_object_to_json_string@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11ee <json_object_to_file+0x9e>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    0x2e1b(%rip),%rax
and    (%rax),%ebp
je     11eb <json_object_to_file+0x9b>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%esi
add    %r12,%rsi
mov    %ebp,%edx
sub    %ebx,%edx
mov    %r15d,%edi
call   1040 <write@plt>
test   %eax,%eax
js     11ee <json_object_to_file+0x9e>
add    %eax,%ebx
cmp    %ebx,%ebp
ja     11d0 <json_object_to_file+0x80>
xor    %r14d,%r14d
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.json_object = type { i32 }
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@UINT_MAX = global i32 0
@errno = global i32 0
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 %14, label %13
13: ; preds = %2
store i32 -1, i32* %3
br label %61
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %15, i32 %20, i32 420)
store i32 %21, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %14
store i32 -1, i32* %3
br label %61
24: ; preds = %14
%25 = load %struct.json_object*, %struct.json_object** %5
%26 = call i8* @json_object_to_json_string(%struct.json_object* %25)
store i8* %26, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %3
br label %61
31: ; preds = %24
%32 = load i8*, i8** %6
%33 = call i32 @strlen(i8* %32)
%34 = load i32, i32* @UINT_MAX
%35 = and i32 %33, %34
store i32 %35, i32* %10
store i32 0, i32* %9
br label %36
36: ; preds = %54, %31
%37 = load i32, i32* %9
%38 = load i32, i32* %10
%39 = icmp ult i32 %37, %38
br i1 %39, label %40, label %58
40: ; preds = %36
%41 = load i32, i32* %7
%42 = load i8*, i8** %6
%43 = load i32, i32* %9
%44 = zext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i32, i32* %10
%47 = load i32, i32* %9
%48 = sub i32 %46, %47
%49 = call i32 @write(i32 %41, i8* %45, i32 %48)
store i32 %49, i32* %8
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %40
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
store i32 -1, i32* %3
br label %61
54: ; preds = %40
%55 = load i32, i32* %8
%56 = load i32, i32* %9
%57 = add i32 %56, %55
store i32 %57, i32* %9
br label %36
58: ; preds = %36
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
store i32 0, i32* %3
br label %61
61: ; preds = %58, %51, %28, %23, %13
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.json_object = type { i32 }
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@UINT_MAX = global i32 0
@errno = global i32 0
define i32 @json_object_to_file(i8* nocapture %0, %struct.json_object* %1) {
%3 = icmp eq %struct.json_object* %1, null
br i1 %3, label %33, label %4
4: ; preds = %2
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %6, %5
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 420)
%11 = icmp slt i32 %10, 0
br i1 %11, label %33, label %12
12: ; preds = %4
%13 = tail call i8* @json_object_to_json_string(%struct.json_object* %1)
%14 = icmp eq i8* %13, null
br i1 %14, label %30, label %15
15: ; preds = %12
%16 = tail call i32 @strlen(i8* %13)
%17 = load i32, i32* @UINT_MAX
%18 = and i32 %17, %16
%19 = icmp eq i32 %18, 0
br i1 %19, label %30, label %22
20: ; preds = %22
%21 = icmp ugt i32 %18, %29
br i1 %21, label %22, label %30
22: ; preds = %15, %20
%23 = phi i32 [ %29, %20 ], [ 0, %15 ]
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %13, i64 %24
%26 = sub i32 %18, %23
%27 = tail call i32 @write(i32 %10, i8* %25, i32 %26)
%28 = icmp slt i32 %27, 0
%29 = add i32 %27, %23
br i1 %28, label %30, label %20
30: ; preds = %20, %22, %15, %12
%31 = phi i32 [ -1, %12 ], [ 0, %15 ], [ -1, %22 ], [ 0, %20 ]
%32 = tail call i32 @close(i32 %10)
br label %33
33: ; preds = %30, %4, %2
%34 = phi i32 [ -1, %2 ], [ -1, %4 ], [ %31, %30 ]
ret i32 %34
}