#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_abort(char *, char const *, int /*<<< orphan*/,
                             int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int open(char const *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ const write(int, char const *, scalar_t__ const);
void set_tracing(const char *path, const char *str, const bool carp) {
  int fd;
  const ssize_t len = (ssize_t)strlen(str);
  if ((fd = open(path, O_WRONLY, S_IRUSR | S_IWUSR)) < 0) {
    if (carp) {
      err_abort("Cannot open %s, errno=%d (%s)\n", path, errno,
                strerror(errno));
    }
    return;
  }
  if (write(fd, str, len) != len) {
    (void)close(fd);
    if (carp) {
      err_abort("Cannot write to %s, errno=%d (%s)\n", path, errno,
                strerror(errno));
    }
    return;
  }
  (void)close(fd);
}


x86 assembly (clang, -O0):
<set_tracing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2e49(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11f8 <set_tracing+0x98>
cmpl   $0x0,-0x14(%rbp)
je     11f3 <set_tracing+0x93>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
lea    0xe12(%rip),%rdi
call   1060 <err_abort@plt>
jmp    1268 <set_tracing+0x108>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x20(%rbp),%rax
je     1260 <set_tracing+0x100>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x14(%rbp)
je     125b <set_tracing+0xfb>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
lea    0xdc9(%rip),%rdi
call   1060 <err_abort@plt>
jmp    1268 <set_tracing+0x108>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_tracing>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r13
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e41(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r15,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11e5 <set_tracing+0x85>
mov    %eax,%ebx
movslq %r12d,%rbp
mov    %eax,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
mov    %rax,%r12
mov    %ebx,%edi
call   1050 <close@plt>
cmp    %rbp,%r12
je     121b <set_tracing+0xbb>
test   %r14d,%r14d
je     121b <set_tracing+0xbb>
mov    0x2e05(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe3c(%rip),%rdi
jmp    1201 <set_tracing+0xa1>
test   %r14d,%r14d
je     121b <set_tracing+0xbb>
mov    0x2de7(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xdff(%rip),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <err_abort@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [31 x i8] c"Cannot open %s, errno=%d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"Cannot write to %s, errno=%d (%s)\0A\00"
define void @set_tracing(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
%11 = sext i32 %10 to i64
store i64 %11, i64* %8
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %12, i32 %13, i32 %16)
store i32 %17, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %3
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i8*, i8** %4
%24 = load i32, i32* @errno
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 @err_abort(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %23, i32 %24, i32 %26)
br label %28
28: ; preds = %22, %19
br label %51
29: ; preds = %3
%30 = load i32, i32* %7
%31 = load i8*, i8** %5
%32 = load i64, i64* %8
%33 = call i64 @write(i32 %30, i8* %31, i64 %32)
%34 = load i64, i64* %8
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %48
36: ; preds = %29
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %6
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %36
%42 = load i8*, i8** %4
%43 = load i32, i32* @errno
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = call i32 @err_abort(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %42, i32 %43, i32 %45)
br label %47
47: ; preds = %41, %36
br label %51
48: ; preds = %29
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %47, %28
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [31 x i8] c"Cannot open %s, errno=%d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"Cannot write to %s, errno=%d (%s)\0A\00"
define void @set_tracing(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = sext i32 %4 to i64
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %6, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = icmp eq i32 %2, 0
br i1 %13, label %28, label %14
14: ; preds = %12
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @err_abort(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %15, i32 %16)
br label %28
18: ; preds = %3
%19 = tail call i64 @write(i32 %10, i8* %1, i64 %5)
%20 = icmp eq i64 %19, %5
%21 = tail call i32 @close(i32 %10)
%22 = icmp eq i32 %2, 0
%23 = or i1 %20, %22
br i1 %23, label %28, label %24
24: ; preds = %18
%25 = load i32, i32* @errno
%26 = tail call i32 @strerror(i32 %25)
%27 = tail call i32 @err_abort(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %25, i32 %26)
br label %28
28: ; preds = %18, %24, %12, %14
ret void
}