#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);
char *get_debugfileprefix();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ warn(char *, char *, ...);
int write(int, char *, int /*<<< orphan*/);
int setevent(char *event, char *val) {
  char *prefix = get_debugfileprefix();
  char buffer[256];
  int fd;
  int ret;
  sprintf(buffer, "%s%s", prefix, event);
  if ((fd = open(buffer, O_WRONLY)) < 0) {
    warn("unable to open %s\n", buffer);
    return -1;
  }
  if ((ret = write(fd, val, strlen(val))) < 0) {
    warn("unable to write %s to %s\n", val, buffer);
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<setevent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1070 <get_debugfileprefix@plt>
mov    %rax,-0x20(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe5c(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x124(%rbp)
cmp    $0x0,%eax
jge    11ee <setevent+0x7e>
lea    -0x120(%rbp),%rsi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <setevent+0x103>
mov    -0x124(%rbp),%eax
mov    %eax,-0x134(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x130(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x134(%rbp),%edi
mov    -0x130(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x128(%rbp)
cmp    $0x0,%eax
jge    1261 <setevent+0xf1>
mov    -0x18(%rbp),%rsi
lea    -0x120(%rbp),%rdx
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
mov    -0x124(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <setevent+0x103>
mov    -0x124(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setevent>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
xor    %eax,%eax
call   1070 <get_debugfileprefix@plt>
lea    0xe71(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
mov    %rbp,%rcx
call   1090 <sprintf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11da <setevent+0x6a>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
js     11ed <setevent+0x7d>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    120d <setevent+0x9d>
lea    0xe24(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
jmp    1208 <setevent+0x98>
lea    0xe24(%rip),%rdi
mov    %rsp,%rdx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@O_WRONLY = global i32 0
@.str.1 = [19 x i8] c"unable to open %s\0A\00"
@.str.2 = [26 x i8] c"unable to write %s to %s\0A\00"
define i32 @setevent(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = tail call i8* (...) @get_debugfileprefix()
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %0)
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %21
12: ; preds = %2
%13 = tail call i32 @strlen(i8* %1)
%14 = tail call i32 @write(i32 %8, i8* %1, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 (i8*, i8*, ...) @warn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %5)
%18 = call i32 @close(i32 %8)
br label %21
19: ; preds = %12
%20 = tail call i32 @close(i32 %8)
br label %21
21: ; preds = %19, %16, %10
%22 = phi i32 [ -1, %10 ], [ -1, %16 ], [ 0, %19 ]
ret i32 %22
}