#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 */
struct buffer_instance {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ die(char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char const *, int, int, int /*<<< orphan*/ *);
char *get_instance_file(struct buffer_instance *, char *);
int strlen(char const *);
scalar_t__ strncmp(char const *, char *, int);
char *tracecmd_get_tracing_file(char *);
int /*<<< orphan*/ tracecmd_put_tracing_file(char *);
void set_plugin_instance(struct buffer_instance *instance, const char *name) {
  FILE *fp;
  char *path;
  char zero = '0';
  path = get_instance_file(instance, "current_tracer");
  fp = fopen(path, "w");
  if (!fp) {
    if (!strncmp(name, "nop", 3)) {
      tracecmd_put_tracing_file(path);
      return;
    }
    die("writing to '%s'", path);
  }
  tracecmd_put_tracing_file(path);
  fwrite(name, 1, strlen(name), fp);
  fclose(fp);
  if (strncmp(name, "function", 8) != 0)
    return;
  path = get_instance_file(instance, "options/func_stack_trace");
  fp = fopen(path, "w");
  tracecmd_put_tracing_file(path);
  if (!fp) {
    path = tracecmd_get_tracing_file("options/func_stack_trace");
    fp = fopen(path, "w");
    tracecmd_put_tracing_file(path);
    if (!fp)
      return;
  }
  fwrite(&zero, 1, 1, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<set_plugin_instance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x30,-0x21(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1050 <get_instance_file@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1214 <set_plugin_instance+0x84>
mov    -0x10(%rbp),%rdi
lea    0xe2f(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1204 <set_plugin_instance+0x74>
mov    -0x20(%rbp),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
jmp    130a <set_plugin_instance+0x17a>
mov    -0x20(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   10a0 <die@plt>
mov    -0x20(%rbp),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rdi
lea    0xdcf(%rip),%rsi
mov    $0x8,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     126f <set_plugin_instance+0xdf>
jmp    130a <set_plugin_instance+0x17a>
mov    -0x8(%rbp),%rdi
lea    0xdb4(%rip),%rsi
call   1050 <get_instance_file@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xd81(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12ed <set_plugin_instance+0x15d>
lea    0xd7c(%rip),%rdi
call   1080 <tracecmd_get_tracing_file@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xd49(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12e8 <set_plugin_instance+0x158>
jmp    130a <set_plugin_instance+0x17a>
jmp    12ed <set_plugin_instance+0x15d>
mov    -0x18(%rbp),%rcx
lea    -0x21(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10b0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_plugin_instance>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe4b(%rip),%rsi
call   1050 <get_instance_file@plt>
mov    %rax,%r12
lea    0xe4b(%rip),%rsi
mov    %rax,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1200 <set_plugin_instance+0x60>
lea    0xe36(%rip),%rsi
mov    %r15,%rdi
mov    $0x3,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     12ce <set_plugin_instance+0x12e>
lea    0xe1d(%rip),%rdi
mov    %r12,%rsi
call   10b0 <die@plt>
mov    %r12,%rdi
call   1030 <tracecmd_put_tracing_file@plt>
mov    %r15,%rdi
call   1070 <strlen@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xdf4(%rip),%rsi
mov    %r15,%rdi
mov    $0x8,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     124f <set_plugin_instance+0xaf>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdd8(%rip),%rsi
mov    %r14,%rdi
call   1050 <get_instance_file@plt>
mov    %rax,%rbx
lea    0xda7(%rip),%rsi
mov    %rax,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <tracecmd_put_tracing_file@plt>
test   %r14,%r14
jne    12ae <set_plugin_instance+0x10e>
lea    0xda7(%rip),%rdi
call   1090 <tracecmd_get_tracing_file@plt>
mov    %rax,%rbx
lea    0xd79(%rip),%rsi
mov    %rax,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <tracecmd_put_tracing_file@plt>
test   %r14,%r14
je     1243 <set_plugin_instance+0xa3>
mov    $0x30,%edi
mov    %r14,%rsi
call   1080 <fputc@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <fclose@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <tracecmd_put_tracing_file@plt>

LLVM IR (-O0):
%struct.buffer_instance = type { i32 }
@.str = [15 x i8] c"current_tracer\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"nop\00"
@.str.3 = [16 x i8] c"writing to '%s'\00"
@.str.4 = [9 x i8] c"function\00"
@.str.5 = [25 x i8] c"options/func_stack_trace\00"
define void @set_plugin_instance(%struct.buffer_instance* %0, i8* %1) {
%3 = alloca %struct.buffer_instance*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8
store %struct.buffer_instance* %0, %struct.buffer_instance** %3
store i8* %1, i8** %4
store i8 48, i8* %7
%8 = load %struct.buffer_instance*, %struct.buffer_instance** %3
%9 = call i8* @get_instance_file(%struct.buffer_instance* %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %24, label %14
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%17 = icmp ne i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i8*, i8** %6
%20 = call i32 @tracecmd_put_tracing_file(i8* %19)
br label %62
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %2
%25 = load i8*, i8** %6
%26 = call i32 @tracecmd_put_tracing_file(i8* %25)
%27 = load i8*, i8** %4
%28 = load i8*, i8** %4
%29 = call i32 @strlen(i8* %28)
%30 = load i32*, i32** %5
%31 = call i32 @fwrite(i8* %27, i32 1, i32 %29, i32* %30)
%32 = load i32*, i32** %5
%33 = call i32 @fclose(i32* %32)
%34 = load i8*, i8** %4
%35 = call i64 @strncmp(i8* %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %24
br label %62
38: ; preds = %24
%39 = load %struct.buffer_instance*, %struct.buffer_instance** %3
%40 = call i8* @get_instance_file(%struct.buffer_instance* %39, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
store i8* %40, i8** %6
%41 = load i8*, i8** %6
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %42, i32** %5
%43 = load i8*, i8** %6
%44 = call i32 @tracecmd_put_tracing_file(i8* %43)
%45 = load i32*, i32** %5
%46 = icmp ne i32* %45, null
br i1 %46, label %57, label %47
47: ; preds = %38
%48 = call i8* @tracecmd_get_tracing_file(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
store i8* %48, i8** %6
%49 = load i8*, i8** %6
%50 = call i32* @fopen(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %50, i32** %5
%51 = load i8*, i8** %6
%52 = call i32 @tracecmd_put_tracing_file(i8* %51)
%53 = load i32*, i32** %5
%54 = icmp ne i32* %53, null
br i1 %54, label %56, label %55
55: ; preds = %47
br label %62
56: ; preds = %47
br label %57
57: ; preds = %56, %38
%58 = load i32*, i32** %5
%59 = call i32 @fwrite(i8* %7, i32 1, i32 1, i32* %58)
%60 = load i32*, i32** %5
%61 = call i32 @fclose(i32* %60)
br label %62
62: ; preds = %57, %55, %37, %18
ret void
}

LLVM IR (-O2):
%struct.buffer_instance = type { i32 }
@.str = [15 x i8] c"current_tracer\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"nop\00"
@.str.3 = [16 x i8] c"writing to '%s'\00"
@.str.4 = [9 x i8] c"function\00"
@.str.5 = [25 x i8] c"options/func_stack_trace\00"
define void @set_plugin_instance(%struct.buffer_instance* %0, i8* %1) {
%3 = tail call i8* @get_instance_file(%struct.buffer_instance* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = tail call i64 @strncmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @tracecmd_put_tracing_file(i8* %3)
br label %34
11: ; preds = %6
%12 = tail call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %13
13: ; preds = %11, %2
%14 = tail call i32 @tracecmd_put_tracing_file(i8* %3)
%15 = tail call i32 @strlen(i8* %1)
%16 = tail call i32 @fwrite(i8* %1, i32 1, i32 %15, i32* %4)
%17 = tail call i32 @fclose(i32* %4)
%18 = tail call i64 @strncmp(i8* %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %13
%21 = tail call i8* @get_instance_file(%struct.buffer_instance* %0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%22 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @tracecmd_put_tracing_file(i8* %21)
%24 = icmp eq i32* %22, null
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = tail call i8* @tracecmd_get_tracing_file(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%27 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @tracecmd_put_tracing_file(i8* %26)
%29 = icmp eq i32* %27, null
br i1 %29, label %34, label %30
30: ; preds = %25, %20
%31 = phi i32* [ %22, %20 ], [ %27, %25 ]
%32 = tail call i32 @fputc(i32 48, i32* %31)
%33 = tail call i32 @fclose(i32* %31)
br label %34
34: ; preds = %25, %13, %30, %9
ret void
}