#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;

/* Variables and functions */
int LOG_PID;
char *ctime(int /*<<< orphan*/ *);
char *getlogin();
int getpid();
int /*<<< orphan*/ sprintf(char *, 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)
      sprintf(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;
    sprintf(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    12d5 <mysyslog+0x165>
mov    -0x8(%rbp),%eax
mov    0x2e40(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11dc <mysyslog+0x6c>
lea    -0x430(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x450(%rbp),%rdi
mov    %eax,%edx
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
jmp    11e3 <mysyslog+0x73>
movb   $0x0,-0x430(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11fe <mysyslog+0x8e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x438(%rbp)
jmp    122d <mysyslog+0xbd>
mov    $0x0,%al
call   1080 <getlogin@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    1228 <mysyslog+0xb8>
lea    0xde4(%rip),%rax
mov    %rax,-0x438(%rbp)
jmp    122d <mysyslog+0xbd>
lea    -0x444(%rbp),%rdi
call   1070 <time@plt>
lea    -0x444(%rbp),%rdi
call   1050 <ctime@plt>
add    $0x4,%rax
mov    %rax,-0x440(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x440(%rbp),%rcx
mov    -0x438(%rbp),%r8
lea    -0x430(%rbp),%r9
mov    -0x20(%rbp),%rax
lea    0xd95(%rip),%rsi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1090 <sprintf@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    12d0 <mysyslog+0x160>
jmp    12d5 <mysyslog+0x165>
jmp    12d5 <mysyslog+0x165>
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mysyslog>:
test   %edi,%edi
js     123c <mysyslog+0xcc>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
mov    %r8,%r15
mov    %rcx,%rbp
mov    %edx,%r14d
mov    %edi,%r12d
mov    0x2e46(%rip),%rax
test   %esi,(%rax)
je     123d <mysyslog+0xcd>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe50(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
test   %rbp,%rbp
jne    11d8 <mysyslog+0x68>
xor    %eax,%eax
call   1080 <getlogin@plt>
test   %rax,%rax
lea    0xe31(%rip),%rbp
cmovne %rax,%rbp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <time@plt>
mov    %rbx,%rdi
call   1050 <ctime@plt>
lea    0x4(%rax),%rcx
mov    %r15,(%rsp)
lea    0xe13(%rip),%rsi
lea    0x30(%rsp),%rbx
lea    0x10(%rsp),%r9
mov    %rbx,%rdi
mov    %r14d,%edx
mov    %rbp,%r8
xor    %eax,%eax
call   1090 <sprintf@plt>
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    11d8 <mysyslog+0x68>
jmp    11c3 <mysyslog+0x53>

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*, i8*, i32, ...) @sprintf(i8* %24, 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*, i8*, i32, ...) @sprintf(i8* %44, 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*, i8*, i32, ...) @sprintf(i8* %10, i8* dereferenceable(1) 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 = tail 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*, i8*, i32, ...) @sprintf(i8* %9, i8* dereferenceable(1) 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
}