#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 */
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ unlink(char *);
int open_syslog_fd() {
  int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (fd < 0)
    err(EXIT_FAILURE, "socket");
  unlink("/dev/log");
  struct sockaddr_un addr;
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, "/dev/log");
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    err(EXIT_FAILURE, "bind");
  chmod("/dev/log", 0666);
  return fd;
}


x86 assembly (clang, -O0):
<open_syslog_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11b3 <open_syslog_fd+0x43>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1060 <err@plt>
lea    0xe4d(%rip),%rdi
call   1030 <unlink@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe22(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    121a <open_syslog_fd+0xaa>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1060 <err@plt>
lea    0xde6(%rip),%rdi
mov    $0x1b6,%esi
call   1070 <chmod@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_syslog_fd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e69(%rip),%r12
mov    (%r12),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ae <open_syslog_fd+0x3e>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1060 <err@plt>
lea    0xe52(%rip),%r14
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    120b <open_syslog_fd+0x9b>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1060 <err@plt>
lea    0xdf5(%rip),%rdi
mov    $0x1b6,%esi
call   1070 <chmod@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [9 x i8] c"/dev/log\00"
@.str.2 = [5 x i8] c"bind\00"
define i32 @open_syslog_fd() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_DGRAM
%5 = call i32 @socket(i32 %3, i32 %4, i32 0)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @err(i32 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %8, %0
%12 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strcpy(i32 %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %1
%20 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%21 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %11
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @err(i32 %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %23, %11
%27 = call i32 @chmod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 438)
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [9 x i8] c"/dev/log\00"
@.str.2 = [5 x i8] c"bind\00"
define i32 @open_syslog_fd() {
%1 = alloca %struct.sockaddr_un
%2 = load i32, i32* @AF_UNIX
%3 = load i32, i32* @SOCK_DGRAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @err(i32 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %6, %0
%10 = tail call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%11 = bitcast %struct.sockaddr_un* %1 to i8*
%12 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 8)
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strcpy(i32 %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%18 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%19 = call i64 @bind(i32 %4, %struct.sockaddr* %18, i32 8)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %9
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @err(i32 %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %21, %9
%25 = call i32 @chmod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 438)
ret i32 %4
}