#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *const);
scalar_t__ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ *open_file(char const *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void write_sys_str(char const *path, char const *val) {
  FILE *const fd = open_file(path, "w");
  if (fprintf(fd, "%s", val) < 0) {
    fprintf(stderr, "write_str_to_file (%s): fprintf failed - %s\n", path,
            strerror(errno));
    exit(1);
  }
  fclose(fd);
}


x86 assembly (clang, -O0):
<write_sys_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1040 <open_file@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    -0x10(%rbp),%rdx
lea    0xe7d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jge    11da <write_sys_str+0x8a>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_sys_str>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe9e(%rip),%rsi
call   1040 <open_file@plt>
mov    %rax,%rbx
lea    0xe91(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %rax,%rax
js     118f <write_sys_str+0x3f>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe58(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.2 = [45 x i8] c"write_str_to_file (%s): fprintf failed - %s\0A\00"
@errno = global i32 0
define void @write_sys_str(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %3
%7 = call i32* @open_file(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = ptrtoint i32* %8 to i32
%10 = load i8*, i8** %4
%11 = call i64 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %3
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i64 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %15, i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32*, i32** %5
%22 = call i32 @fclose(i32* %21)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.2 = [45 x i8] c"write_str_to_file (%s): fprintf failed - %s\0A\00"
@errno = global i32 0
define void @write_sys_str(i8* %0, i8* %1) {
%3 = tail call i32* @open_file(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = ptrtoint i32* %3 to i64
%5 = trunc i64 %4 to i32
%6 = tail call i64 (i32, i8*, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i64 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i32 @fclose(i32* %3)
ret void
}