#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 checkopen(char const *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ strchr(char const *, char);
int strlen(char const *);
int openlog(const char *name) {
  if (!strchr(name, '/'))
    return checkopen(name);
  int namelen = strlen(name);
  char *base = "/var/log";
  int baselen = strlen(base);
  int outlen = baselen + namelen + 1;
  char outbuf[outlen + 2];
  memcpy(outbuf, base, baselen);
  outbuf[baselen] = '/';
  memcpy(outbuf + baselen + 1, name, namelen);
  outbuf[outlen] = '\0';
  return checkopen(outbuf);
}


x86 assembly (clang, -O0):
<openlog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
cmp    $0x0,%eax
jne    1174 <openlog+0x34>
mov    -0x10(%rbp),%rdi
call   1060 <checkopen@plt>
mov    %eax,-0x4(%rbp)
jmp    121b <openlog+0xdb>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
lea    0xe79(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edx
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
mov    %edx,%eax
lea    0x1(%rax,%rcx,1),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rdi
movslq -0x24(%rbp),%rax
movb   $0x2f,(%rdi,%rax,1)
movslq -0x24(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rdi
movslq -0x28(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
call   1060 <checkopen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openlog>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %eax,%eax
je     11ed <openlog+0xad>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%r14d
lea    0xe8b(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %rsp,-0x30(%rbp)
lea    (%r14,%r13,1),%eax
movslq %eax,%r15
lea    (%r14,%r13,1),%eax
add    $0x3,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
lea    0xe56(%rip),%rsi
mov    %r13d,%edx
call   1050 <memcpy@plt>
movslq %r13d,%rax
movb   $0x2f,(%rax,%rbx,1)
lea    (%rax,%rbx,1),%rdi
add    $0x1,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1050 <memcpy@plt>
movb   $0x0,0x1(%r15,%rbx,1)
mov    %rbx,%rdi
call   1060 <checkopen@plt>
mov    -0x30(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <checkopen@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/var/log\00"
define i32 @openlog(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @strchr(i8* %10, i8 signext 47)
%12 = icmp ne i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i32 @checkopen(i8* %14)
store i32 %15, i32* %2
br label %48
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %4
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %5
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = load i32, i32* %4
%23 = add nsw i32 %21, %22
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 2
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
store i8* %28, i8** %8
%29 = alloca i8, i64 %27
store i64 %27, i64* %9
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = call i32 @memcpy(i8* %29, i8* %30, i32 %31)
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %29, i64 %34
store i8 47, i8* %35
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %29, i64 %37
%39 = getelementptr inbounds i8, i8* %38, i64 1
%40 = load i8*, i8** %3
%41 = load i32, i32* %4
%42 = call i32 @memcpy(i8* %39, i8* %40, i32 %41)
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %29, i64 %44
store i8 0, i8* %45
%46 = call i32 @checkopen(i8* %29)
store i32 %46, i32* %2
%47 = load i8*, i8** %8
br label %48
48: ; preds = %16, %13
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
@.str = [9 x i8] c"/var/log\00"
define i32 @openlog(i8* %0) {
%2 = tail call i32 @strchr(i8* %0, i8 signext 47)
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @checkopen(i8* %0)
br label %23
6: ; preds = %1
%7 = tail call i32 @strlen(i8* %0)
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%9 = add nsw i32 %8, %7
%10 = add nsw i32 %9, 1
%11 = add nsw i32 %9, 3
%12 = zext i32 %11 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i8, i64 %12
%15 = call i32 @memcpy(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
%16 = sext i32 %8 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 47, i8* %17
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = call i32 @memcpy(i8* %18, i8* %0, i32 %7)
%20 = sext i32 %10 to i64
%21 = getelementptr inbounds i8, i8* %14, i64 %20
store i8 0, i8* %21
%22 = call i32 @checkopen(i8* %14)
br label %23
23: ; preds = %6, %4
%24 = phi i32 [ %22, %6 ], [ %5, %4 ]
ret i32 %24
}