#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 int /*<<< orphan*/ time_t;
typedef int /*<<< orphan*/ pid;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int LOG_PID;
char *ctime(int /*<<< orphan*/ *);
char *getlogin();
int getpid();
int /*<<< orphan*/ snprintf(char *, int, char *, int, ...);
scalar_t__ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
scalar_t__ write(int, char *, scalar_t__);
void mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
  char buf[1000], pid[30], *cp, *tp;
  time_t now;
  if (fd > -1) {
    if (logflags & LOG_PID)
      snprintf(pid, sizeof(pid), "[%d]", getpid());
    else
      pid[0] = 0;
    if (tag)
      cp = tag;
    else {
      cp = getlogin();
      if (!cp)
        cp = "<someone>";
    }
    (void)time(&now);
    tp = ctime(&now) + 4;
    snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s", pri, tp, cp, pid,
             msg);
    if (write(fd, buf, strlen(buf) + 1) < 0)
      return;
  }
}


x86 assembly (clang, -O0):
<mysyslog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jle    12e4 <mysyslog+0x174>
mov    -0x8(%rbp),%eax
mov    0x2e40(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11e1 <mysyslog+0x71>
lea    -0x430(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x450(%rbp),%rdi
mov    %eax,%ecx
mov    $0x1e,%esi
lea    0xe2b(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
jmp    11e8 <mysyslog+0x78>
movb   $0x0,-0x430(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1203 <mysyslog+0x93>
mov    -0x18(%rbp),%rax
mov    %rax,-0x438(%rbp)
jmp    1232 <mysyslog+0xc2>
mov    $0x0,%al
call   1090 <getlogin@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    122d <mysyslog+0xbd>
lea    0xddf(%rip),%rax
mov    %rax,-0x438(%rbp)
jmp    1232 <mysyslog+0xc2>
lea    -0x444(%rbp),%rdi
call   1080 <time@plt>
lea    -0x444(%rbp),%rdi
call   1050 <ctime@plt>
add    $0x4,%rax
mov    %rax,-0x440(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    -0x440(%rbp),%r8
mov    -0x438(%rbp),%r9
lea    -0x430(%rbp),%r10
mov    -0x20(%rbp),%rax
mov    $0x3e8,%esi
lea    0xd8b(%rip),%rdx
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x45c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x458(%rbp)
lea    -0x410(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x45c(%rbp),%edi
mov    -0x458(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12df <mysyslog+0x16f>
jmp    12e4 <mysyslog+0x174>
jmp    12e4 <mysyslog+0x174>
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mysyslog>:
test   %edi,%edi
js     124a <mysyslog+0xda>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
mov    %r8,%r14
mov    %rcx,%rbp
mov    %edx,%r15d
mov    %edi,%r12d
mov    0x2e46(%rip),%rax
test   %esi,(%rax)
je     124b <mysyslog+0xdb>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe50(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x1e,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <snprintf@plt>
test   %rbp,%rbp
jne    11dd <mysyslog+0x6d>
xor    %eax,%eax
call   1090 <getlogin@plt>
test   %rax,%rax
lea    0xe2c(%rip),%rbp
cmovne %rax,%rbp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <time@plt>
mov    %rbx,%rdi
call   1050 <ctime@plt>
lea    0x4(%rax),%r8
lea    0x10(%rsp),%r10
lea    0xe0d(%rip),%rdx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x3e8,%esi
mov    %r15d,%ecx
mov    %rbp,%r9
xor    %eax,%eax
push   %r14
push   %r10
call   1070 <snprintf@plt>
add    $0x10,%rsp
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r12d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
add    $0x420,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,0x10(%rsp)
test   %rbp,%rbp
jne    11dd <mysyslog+0x6d>
jmp    11c8 <mysyslog+0x58>

LLVM IR (-O0):
@LOG_PID = global i32 0
@.str = [5 x i8] c"[%d]\00"
@.str.1 = [10 x i8] c"<someone>\00"
@.str.2 = [27 x i8] c"<%d>%.15s %.200s%s: %.400s\00"
define void @mysyslog(i32 %0, i32 %1, i32 %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [1000 x i8]
%12 = alloca [30 x i8]
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %60
18: ; preds = %5
%19 = load i32, i32* %7
%20 = load i32, i32* @LOG_PID
%21 = and i32 %19, %20
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = getelementptr inbounds [30 x i8], [30 x i8]* %12, i64 0, i64 0
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %24, i32 30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %25)
br label %29
27: ; preds = %18
%28 = getelementptr inbounds [30 x i8], [30 x i8]* %12, i64 0, i64 0
store i8 0, i8* %28
br label %29
29: ; preds = %27, %23
%30 = load i8*, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i8*, i8** %9
store i8* %33, i8** %13
br label %40
34: ; preds = %29
%35 = call i8* (...) @getlogin()
store i8* %35, i8** %13
%36 = load i8*, i8** %13
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %34
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %13
br label %39
39: ; preds = %38, %34
br label %40
40: ; preds = %39, %32
%41 = call i32 @time(i32* %15)
%42 = call i8* @ctime(i32* %15)
%43 = getelementptr inbounds i8, i8* %42, i64 4
store i8* %43, i8** %14
%44 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%45 = load i32, i32* %8
%46 = load i8*, i8** %14
%47 = load i8*, i8** %13
%48 = getelementptr inbounds [30 x i8], [30 x i8]* %12, i64 0, i64 0
%49 = load i8*, i8** %10
%50 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %44, i32 1000, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %45, i8* %46, i8* %47, i8* %48, i8* %49)
%51 = load i32, i32* %6
%52 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%53 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%54 = call i64 @strlen(i8* %53)
%55 = add nsw i64 %54, 1
%56 = call i64 @write(i32 %51, i8* %52, i64 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %40
br label %60
59: ; preds = %40
br label %60
60: ; preds = %58, %59, %5
ret void
}

LLVM IR (-O2):
@LOG_PID = global i32 0
@.str = [5 x i8] c"[%d]\00"
@.str.1 = [10 x i8] c"<someone>\00"
@.str.2 = [27 x i8] c"<%d>%.15s %.200s%s: %.400s\00"
define void @mysyslog(i32 %0, i32 %1, i32 %2, i8* %3, i8* %4) {
%6 = alloca [1000 x i8]
%7 = alloca [30 x i8]
%8 = alloca i32
%9 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%10 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%11 = bitcast i32* %8 to i8*
%12 = icmp sgt i32 %0, -1
br i1 %12, label %13, label %36
13: ; preds = %5
%14 = load i32, i32* @LOG_PID
%15 = and i32 %14, %1
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = tail call i32 (...) @getpid()
%19 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %10, i32 30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %21
20: ; preds = %13
store i8 0, i8* %10
br label %21
21: ; preds = %20, %17
%22 = icmp eq i8* %3, null
br i1 %22, label %23, label %27
23: ; preds = %21
%24 = call i8* (...) @getlogin()
%25 = icmp eq i8* %24, null
%26 = select i1 %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %24
br label %27
27: ; preds = %21, %23
%28 = phi i8* [ %26, %23 ], [ %3, %21 ]
%29 = call i32 @time(i32* %8)
%30 = call i8* @ctime(i32* %8)
%31 = getelementptr inbounds i8, i8* %30, i64 4
%32 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %9, i32 1000, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %2, i8* %31, i8* %28, i8* %10, i8* %4)
%33 = call i64 @strlen(i8* %9)
%34 = add nsw i64 %33, 1
%35 = call i64 @write(i32 %0, i8* %9, i64 %34)
br label %36
36: ; preds = %27, %5
ret void
}