#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;
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
scalar_t__ strlen(char *);
scalar_t__ write_text(char *, char *, scalar_t__);
int cg_write(const char *cgroup, const char *control, char *buf) {
  char path[PATH_MAX];
  ssize_t len = strlen(buf);
  snprintf(path, sizeof(path), "%s/%s", cgroup, control);
  if (write_text(path, buf, len) == len)
    return 0;
  return -1;
}


x86 assembly (clang, -O0):
<cg_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
mov    %rcx,-0x38(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe63(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <write_text@plt>
cmp    -0x38(%rbp),%rax
jne    11d0 <cg_write+0xa0>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    11de <cg_write+0xae>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cg_write>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r15
mov    0x2ea1(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
mov    %r13,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r14
lea    0xe88(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %r15,%rcx
mov    -0x30(%rbp),%r8
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1050 <write_text@plt>
xor    %ecx,%ecx
cmp    %r14,%rax
setne  %cl
neg    %ecx
mov    %ecx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @cg_write(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = load i8*, i8** %7
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %10
%18 = trunc i64 %13 to i32
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = call i32 @snprintf(i8* %15, i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20)
%22 = load i8*, i8** %7
%23 = load i64, i64* %10
%24 = call i64 @write_text(i8* %15, i8* %22, i64 %23)
%25 = load i64, i64* %10
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %3
store i32 0, i32* %4
store i32 1, i32* %11
br label %29
28: ; preds = %3
store i32 -1, i32* %4
store i32 1, i32* %11
br label %29
29: ; preds = %28, %27
%30 = load i8*, i8** %8
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define i32 @cg_write(i8* %0, i8* %1, i8* %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%8 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%9 = call i64 @write_text(i8* %6, i8* %2, i64 %7)
%10 = icmp ne i64 %9, %7
%11 = sext i1 %10 to i32
ret i32 %11
}