#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 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) {
    ("json_object_to_file: object is null\n");
    return -1;
  }
  if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
    ("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 = (unsigned int)(strlen(json_str) & UINT_MAX);
  wpos = 0;
  while (wpos < wsize) {
    if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0) {
      close(fd);
      ("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    1187 <json_object_to_file+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <json_object_to_file+0x124>
mov    -0x10(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11d6 <json_object_to_file+0x76>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <json_object_to_file+0x124>
mov    -0x18(%rbp),%rdi
call   1030 <json_object_to_json_string@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11f9 <json_object_to_file+0x99>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <json_object_to_file+0x124>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    0x2dcf(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jae    1275 <json_object_to_file+0x115>
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    1267 <json_object_to_file+0x107>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <json_object_to_file+0x124>
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1215 <json_object_to_file+0xb5>
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     120e <json_object_to_file+0xae>
mov    %rsi,%rbx
mov    0x2e37(%rip),%rax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e65(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     1200 <json_object_to_file+0xa0>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <json_object_to_json_string@plt>
test   %rax,%rax
je     120e <json_object_to_file+0xae>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    0x2e11(%rip),%rax
and    (%rax),%ebp
je     11eb <json_object_to_file+0x8b>
xor    %ebx,%ebx
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     11f8 <json_object_to_file+0x98>
add    %eax,%ebx
cmp    %ebx,%ebp
ja     11d0 <json_object_to_file+0x70>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %r14d,%r14d
jmp    120e <json_object_to_file+0xae>
mov    %r15d,%edi
call   1060 <close@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
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
@UINT_MAX = 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 %14, label %13
13: ; preds = %2
store i32 -1, i32* %3
br label %65
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 %27
23: ; preds = %14
%24 = load i8*, i8** %4
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
store i32 -1, i32* %3
br label %65
27: ; preds = %14
%28 = load %struct.json_object*, %struct.json_object** %5
%29 = call i8* @json_object_to_json_string(%struct.json_object* %28)
store i8* %29, i8** %6
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %27
store i32 -1, i32* %3
br label %65
32: ; preds = %27
%33 = load i8*, i8** %6
%34 = call i32 @strlen(i8* %33)
%35 = load i32, i32* @UINT_MAX
%36 = and i32 %34, %35
store i32 %36, i32* %10
store i32 0, i32* %9
br label %37
37: ; preds = %58, %32
%38 = load i32, i32* %9
%39 = load i32, i32* %10
%40 = icmp ult i32 %38, %39
br i1 %40, label %41, label %62
41: ; preds = %37
%42 = load i32, i32* %7
%43 = load i8*, i8** %6
%44 = load i32, i32* %9
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i32, i32* %10
%48 = load i32, i32* %9
%49 = sub 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 %58
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)
store i32 -1, i32* %3
br label %65
58: ; preds = %41
%59 = load i32, i32* %8
%60 = load i32, i32* %9
%61 = add i32 %60, %59
store i32 %61, i32* %9
br label %37
62: ; preds = %37
%63 = load i32, i32* %7
%64 = call i32 @close(i32 %63)
store i32 0, i32* %3
br label %65
65: ; preds = %62, %52, %31, %23, %13
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.json_object = type { i32 }
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@errno = global i32 0
@UINT_MAX = 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 %39, 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 %12, label %15
12: ; preds = %4
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
br label %39
15: ; preds = %4
%16 = tail call i8* @json_object_to_json_string(%struct.json_object* %1)
%17 = icmp eq i8* %16, null
br i1 %17, label %39, label %18
18: ; preds = %15
%19 = tail call i32 @strlen(i8* %16)
%20 = load i32, i32* @UINT_MAX
%21 = and i32 %20, %19
%22 = icmp eq i32 %21, 0
br i1 %22, label %37, label %25
23: ; preds = %25
%24 = icmp ugt i32 %21, %32
br i1 %24, label %25, label %37
25: ; preds = %18, %23
%26 = phi i32 [ %32, %23 ], [ 0, %18 ]
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %16, i64 %27
%29 = sub i32 %21, %26
%30 = tail call i32 @write(i32 %10, i8* %28, i32 %29)
%31 = icmp slt i32 %30, 0
%32 = add i32 %30, %26
br i1 %31, label %33, label %23
33: ; preds = %25
%34 = tail call i32 @close(i32 %10)
%35 = load i32, i32* @errno
%36 = tail call i32 @strerror(i32 %35)
br label %39
37: ; preds = %23, %18
%38 = tail call i32 @close(i32 %10)
br label %39
39: ; preds = %15, %2, %37, %33, %12
%40 = phi i32 [ -1, %12 ], [ -1, %33 ], [ 0, %37 ], [ -1, %2 ], [ -1, %15 ]
ret i32 %40
}