#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 */

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ debug(char *, char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int write_to_path(char *path, char *val) {
  debug("Writing to file: %s %s\n", path, val);
  int fd = open(path, O_WRONLY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open fd: %s\n", path);
    return 1;
  }
  if (write(fd, val, strlen(val)) < 0) {
    close(fd);
    fprintf(stderr, "Failed to write '%s' to the fd: %s\n", val, path);
    return 1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<write_to_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe81(%rip),%rdi
call   1050 <debug@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ca <write_to_path+0x6a>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe61(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <write_to_path+0xdb>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    122c <write_to_path+0xcc>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <write_to_path+0xdb>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe8c(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <debug@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11da <write_to_path+0x7a>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
test   %r15,%r15
jns    11f9 <write_to_path+0x99>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11f4 <write_to_path+0x94>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Writing to file: %s %s\0A\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"Failed to open fd: %s\0A\00"
@.str.2 = [36 x i8] c"Failed to write '%s' to the fd: %s\0A\00"
define i32 @write_to_path(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = load i8*, i8** %5
%9 = call i32 @debug(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %8)
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %36
19: ; preds = %2
%20 = load i32, i32* %6
%21 = load i8*, i8** %5
%22 = load i8*, i8** %5
%23 = call i32 @strlen(i8* %22)
%24 = call i64 @write(i32 %20, i8* %21, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %19
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %5
%31 = load i8*, i8** %4
%32 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %30, i8* %31)
store i32 1, i32* %3
br label %36
33: ; preds = %19
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i32 0, i32* %3
br label %36
36: ; preds = %33, %26, %15
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [24 x i8] c"Writing to file: %s %s\0A\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"Failed to open fd: %s\0A\00"
@.str.2 = [36 x i8] c"Failed to write '%s' to the fd: %s\0A\00"
define i32 @write_to_path(i8* %0, i8* %1) {
%3 = tail call i32 @debug(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %18
10: ; preds = %2
%11 = tail call i32 @strlen(i8* %1)
%12 = tail call i64 @write(i32 %5, i8* %1, i32 %11)
%13 = icmp slt i64 %12, 0
%14 = tail call i32 @close(i32 %5)
br i1 %13, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %0)
br label %18
18: ; preds = %10, %15, %7
%19 = phi i32 [ 1, %7 ], [ 1, %15 ], [ 0, %10 ]
ret i32 %19
}