#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ *stderr;
int write_receiver_json(const char *dir) {
  char path[PATH_MAX];
  FILE *f;
  if (snprintf(path, PATH_MAX, "%s/receiver.json.new", dir) >= PATH_MAX) {
    fprintf(stderr, "write_receiver_json: path too long\n");
    return 0;
  }
  if (!(f = fopen(path, "w"))) {
    fprintf(stderr, "fopen(%s): %m\n", path);
    return 0;
  }
  fprintf(f, "{\n"
             "  \"version\" : \"dump978-uat2json\",\n"
             "  \"refresh\" : 1000,\n"
             "  \"history\" : 0\n"
             "}\n");
  fclose(f);
  return 1;
}


x86 assembly (clang, -O0):
<write_receiver_json>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
lea    0xe74(%rip),%rdx
call   1040 <snprintf@plt>
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%eax
jl     11cb <write_receiver_json+0x8b>
mov    0x2e31(%rip),%rax
mov    (%rax),%rdi
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1241 <write_receiver_json+0x101>
mov    -0x38(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1218 <write_receiver_json+0xd8>
mov    -0x38(%rbp),%rdx
mov    0x2de4(%rip),%rax
mov    (%rax),%rdi
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1241 <write_receiver_json+0x101>
mov    -0x28(%rbp),%rdi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_receiver_json>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rdi,%rcx
mov    0x2e8f(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe81(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
cmp    (%r14),%eax
jge    11cc <write_receiver_json+0x7c>
lea    0xe80(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f1 <write_receiver_json+0xa1>
mov    %rax,%r14
lea    0xe9e(%rip),%rdi
mov    $0x4a,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    $0x1,%r14d
jmp    120f <write_receiver_json+0xbf>
mov    0x2e05(%rip),%rax
mov    (%rax),%rcx
lea    0xe38(%rip),%rdi
mov    $0x23,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
xor    %r14d,%r14d
jmp    120f <write_receiver_json+0xbf>
mov    0x2de0(%rip),%rax
mov    (%rax),%rdi
lea    0xe37(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14d,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [21 x i8] c"%s/receiver.json.new\00"
@stderr = global i32* null
@.str.1 = [36 x i8] c"write_receiver_json: path too long\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [15 x i8] c"fopen(%s): %m\0A\00"
@.str.4 = [75 x i8] c"{\0A \22version\22 : \22dump978-uat2json\22,\0A \22refresh\22 : 1000,\0A \22history\22 : 0\0A}\0A\00"
define i32 @write_receiver_json(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i32, i32* @PATH_MAX
%13 = load i8*, i8** %3
%14 = call i32 @snprintf(i8* %11, i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @PATH_MAX
%16 = icmp sge i32 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i32*, i32** @stderr
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
store i32 1, i32* %7
br label %31
20: ; preds = %1
%21 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32*, i32** @stderr
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %11)
store i32 0, i32* %2
store i32 1, i32* %7
br label %31
26: ; preds = %20
%27 = load i32*, i32** %6
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.4, i64 0, i64 0))
%29 = load i32*, i32** %6
%30 = call i32 @fclose(i32* %29)
store i32 1, i32* %2
store i32 1, i32* %7
br label %31
31: ; preds = %26, %23, %17
%32 = load i8*, i8** %4
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [21 x i8] c"%s/receiver.json.new\00"
@stderr = global i32* null
@.str.1 = [36 x i8] c"write_receiver_json: path too long\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [15 x i8] c"fopen(%s): %m\0A\00"
@.str.4 = [75 x i8] c"{\0A \22version\22 : \22dump978-uat2json\22,\0A \22refresh\22 : 1000,\0A \22history\22 : 0\0A}\0A\00"
define i32 @write_receiver_json(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = load i32, i32* @PATH_MAX
%7 = icmp slt i32 %5, %6
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i32*, i32** @stderr
%10 = call i64 @fwrite(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i64 35, i64 1, i32* %9)
br label %20
11: ; preds = %1
%12 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32*, i32** @stderr
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %20
17: ; preds = %11
%18 = call i64 @fwrite(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.4, i64 0, i64 0), i64 74, i64 1, i32* %12)
%19 = call i32 @fclose(i32* %12)
br label %20
20: ; preds = %17, %14, %8
%21 = phi i32 [ 0, %8 ], [ 1, %17 ], [ 0, %14 ]
ret i32 %21
}