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

/* Variables and functions */
int /*<<< orphan*/ AF_FILE;
int ENOTSUP;
int /*<<< orphan*/ O_RDONLY;
int SOCK_CLOEXEC;
int SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int cr_plugin_restore_unix_sk(int id) {
  struct sockaddr_un addr;
  socklen_t addr_len;
  char buf[4096];
  int sk, fd;
  snprintf(buf, sizeof(buf), "syslog-%x.img", id);
  fd = open(buf, O_RDONLY);
  if (fd < 0)
    return -ENOTSUP;
  close(fd);
  sk = socket(AF_FILE, SOCK_DGRAM | SOCK_CLOEXEC, 0);
  if (sk == -1)
    return sk;
  addr.sun_family = AF_FILE;
  addr_len = strlen("/dev/log");
  strncpy(addr.sun_path, "/dev/log", addr_len);
  addr_len += sizeof(addr.sun_family);
  if (connect(sk, (struct sockaddr *)&addr, addr_len) == -1) {
    close(sk);
    return -1;
  }
  return sk;
}


x86 assembly (clang, -O0):
<cr_plugin_restore_unix_sk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1000,%esi
lea    0xe6c(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x1020(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1028(%rbp)
cmpl   $0x0,-0x1028(%rbp)
jge    11d4 <cr_plugin_restore_unix_sk+0x64>
xor    %eax,%eax
mov    0x2e06(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1295 <cr_plugin_restore_unix_sk+0x125>
mov    -0x1028(%rbp),%edi
call   1060 <close@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0xffffffff,-0x1024(%rbp)
jne    1222 <cr_plugin_restore_unix_sk+0xb2>
mov    -0x1024(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1295 <cr_plugin_restore_unix_sk+0x125>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xdd9(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%edx
lea    0xdc4(%rip),%rsi
call   1030 <strncpy@plt>
movslq -0x14(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x14(%rbp)
mov    -0x1024(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    128c <cr_plugin_restore_unix_sk+0x11c>
mov    -0x1024(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <cr_plugin_restore_unix_sk+0x125>
mov    -0x1024(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cr_plugin_restore_unix_sk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%ecx
lea    0xe7a(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <snprintf@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1222 <cr_plugin_restore_unix_sk+0xb2>
mov    %eax,%edi
call   1060 <close@plt>
mov    0x2e3d(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1237 <cr_plugin_restore_unix_sk+0xc7>
mov    %eax,%r15d
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
lea    0xe1c(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
add    $0x4,%ebp
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     122f <cr_plugin_restore_unix_sk+0xbf>
mov    %r15d,%ebx
jmp    1237 <cr_plugin_restore_unix_sk+0xc7>
mov    0x2da7(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    1237 <cr_plugin_restore_unix_sk+0xc7>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"syslog-%x.img\00"
@O_RDONLY = global i32 0
@ENOTSUP = global i32 0
@AF_FILE = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_CLOEXEC = global i32 0
@.str.1 = [9 x i8] c"/dev/log\00"
define i32 @cr_plugin_restore_unix_sk(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @snprintf(i8* %9, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i32, i32* @ENOTSUP
%19 = sub nsw i32 0, %18
store i32 %19, i32* %2
br label %54
20: ; preds = %1
%21 = load i32, i32* %8
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* @AF_FILE
%24 = load i32, i32* @SOCK_DGRAM
%25 = load i32, i32* @SOCK_CLOEXEC
%26 = or i32 %24, %25
%27 = call i32 @socket(i32 %23, i32 %26, i32 0)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = load i32, i32* %7
store i32 %31, i32* %2
br label %54
32: ; preds = %20
%33 = load i32, i32* @AF_FILE
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %33, i32* %34
%35 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 %35, i32* %5
%36 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* %5
%39 = call i32 @strncpy(i32 %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = add i64 %41, 4
%43 = trunc i64 %42 to i32
store i32 %43, i32* %5
%44 = load i32, i32* %7
%45 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%46 = load i32, i32* %5
%47 = call i32 @connect(i32 %44, %struct.sockaddr* %45, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %32
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %2
br label %54
52: ; preds = %32
%53 = load i32, i32* %7
store i32 %53, i32* %2
br label %54
54: ; preds = %52, %49, %30, %17
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"syslog-%x.img\00"
@O_RDONLY = global i32 0
@ENOTSUP = global i32 0
@AF_FILE = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_CLOEXEC = global i32 0
@.str.1 = [9 x i8] c"/dev/log\00"
define i32 @cr_plugin_restore_unix_sk(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca [4096 x i8]
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @ENOTSUP
%12 = sub nsw i32 0, %11
br label %32
13: ; preds = %1
%14 = call i32 @close(i32 %8)
%15 = load i32, i32* @AF_FILE
%16 = load i32, i32* @SOCK_DGRAM
%17 = load i32, i32* @SOCK_CLOEXEC
%18 = or i32 %17, %16
%19 = call i32 @socket(i32 %15, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %32, label %21
21: ; preds = %13
%22 = load i32, i32* @AF_FILE
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %22, i32* %23
%24 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = add i32 %24, 4
%27 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%28 = call i32 @connect(i32 %19, %struct.sockaddr* %27, i32 %26)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @close(i32 %19)
br label %32
32: ; preds = %21, %13, %30, %10
%33 = phi i32 [ %12, %10 ], [ -1, %30 ], [ -1, %13 ], [ %19, %21 ]
ret i32 %33
}