#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_RDONLY;
int /*<<< orphan*/ close(int);
int getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ openlog(char *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void open_syslog(const char *ident, int option, int facility) {
  char id[16 + 1] = {0}, path[128];
  int fd, len = -1;
  if (!ident) {
    sprintf(path, "/proc/%d/comm", getpid());
    fd = open(path, O_RDONLY);
    if (fd < 0)
      return;
    len = read(fd, id, 16);
    if (len < 0) {
      close(fd);
      return;
    }
    close(fd);
  } else {
    strncpy(id, ident, 16);
  }
  openlog(id, option, facility);
}


x86 assembly (clang, -O0):
<open_syslog>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1050 <memset@plt>
movl   $0xffffffff,-0xb8(%rbp)
ff ff ff
cmpq   $0x0,-0x8(%rbp)
jne    1258 <open_syslog+0xd8>
lea    -0xb0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0xc0(%rbp),%rdi
mov    %eax,%edx
lea    0xe21(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0xb0(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jge    1211 <open_syslog+0x91>
jmp    1279 <open_syslog+0xf9>
mov    -0xb4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,-0xb8(%rbp)
cmpl   $0x0,-0xb8(%rbp)
jge    1248 <open_syslog+0xc8>
mov    -0xb4(%rbp),%edi
call   1060 <close@plt>
jmp    1279 <open_syslog+0xf9>
mov    -0xb4(%rbp),%edi
call   1060 <close@plt>
jmp    126a <open_syslog+0xea>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1090 <openlog@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_syslog>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa8,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
test   %rdi,%rdi
je     11be <open_syslog+0x4e>
mov    %rdi,%rsi
mov    %rsp,%rdi
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1080 <openlog@plt>
add    $0xa8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe34(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11b0 <open_syslog+0x40>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1050 <close@plt>
test   %r15d,%r15d
jns    11a3 <open_syslog+0x33>
jmp    11b0 <open_syslog+0x40>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/comm\00"
@O_RDONLY = global i32 0
define void @open_syslog(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [17 x i8]
%8 = alloca [128 x i8]
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = bitcast [17 x i8]* %7 to i8*
store i32 -1, i32* %10
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %36, label %14
14: ; preds = %3
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%16 = call i32 (...) @getpid()
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %14
br label %45
24: ; preds = %14
%25 = load i32, i32* %9
%26 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 16)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* %9
%32 = call i32 @close(i32 %31)
br label %45
33: ; preds = %24
%34 = load i32, i32* %9
%35 = call i32 @close(i32 %34)
br label %40
36: ; preds = %3
%37 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%38 = load i8*, i8** %4
%39 = call i32 @strncpy(i8* %37, i8* %38, i32 16)
br label %40
40: ; preds = %36, %33
%41 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%42 = load i32, i32* %5
%43 = load i32, i32* %6
%44 = call i32 @openlog(i8* %41, i32 %42, i32 %43)
br label %45
45: ; preds = %40, %30, %23
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/comm\00"
@O_RDONLY = global i32 0
define void @open_syslog(i8* %0, i32 %1, i32 %2) {
%4 = alloca [17 x i8]
%5 = alloca [128 x i8]
%6 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%8 = icmp eq i8* %0, null
br i1 %8, label %9, label %19
9: ; preds = %3
%10 = tail call i32 (...) @getpid()
%11 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %7, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %9
%16 = call i32 @read(i32 %13, i8* %6, i32 16)
%17 = icmp slt i32 %16, 0
%18 = tail call i32 @close(i32 %13)
br i1 %17, label %23, label %21
19: ; preds = %3
%20 = call i32 @strncpy(i8* %6, i8* %0, i32 16)
br label %21
21: ; preds = %15, %19
%22 = call i32 @openlog(i8* %6, i32 %1, i32 %2)
br label %23
23: ; preds = %15, %9, %21
ret void
}