#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*/ tv;
struct timeval {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int SOCK_CLOEXEC;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_SNDTIMEO;
int /*<<< orphan*/ close_nointr_nofail(int);
int errno;
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct timeval *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ timeval_store(struct timeval *, int);
int create_log_socket(int type) {
  struct timeval tv;
  int fd;
  if ((fd = socket(AF_UNIX, type | SOCK_CLOEXEC, 0)) < 0)
    return -errno;
  timeval_store(&tv, (5 * 1000000ULL));
  if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
    close_nointr_nofail(fd);
    return -errno;
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_log_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    0x2e8a(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1186 <create_log_socket+0x46>
xor    %eax,%eax
mov    0x2e44(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e3 <create_log_socket+0xa3>
lea    -0x10(%rbp),%rdi
mov    $0x4c4b40,%esi
call   1030 <timeval_store@plt>
mov    -0x14(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    11dd <create_log_socket+0x9d>
mov    -0x14(%rbp),%edi
call   1050 <close_nointr_nofail@plt>
xor    %eax,%eax
mov    0x2ded(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e3 <create_log_socket+0xa3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_log_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%esi
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
mov    0x2e8e(%rip),%rax
or     (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11b8 <create_log_socket+0x78>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x4c4b40,%esi
call   1030 <timeval_store@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jns    11c3 <create_log_socket+0x83>
mov    %ebx,%edi
call   1050 <close_nointr_nofail@plt>
mov    0x2e0e(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11c3 <create_log_socket+0x83>
mov    0x2e01(%rip),%rax
sub    (%rax),%ebp
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@AF_UNIX = global i32 0
@SOCK_CLOEXEC = global i32 0
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
define i32 @create_log_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* %3
%8 = load i32, i32* @SOCK_CLOEXEC
%9 = or i32 %7, %8
%10 = call i32 @socket(i32 %6, i32 %9, i32 0)
store i32 %10, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
store i32 %14, i32* %2
br label %29
15: ; preds = %1
%16 = call i32 @timeval_store(%struct.timeval* %4, i32 5000000)
%17 = load i32, i32* %5
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_SNDTIMEO
%20 = call i64 @setsockopt(i32 %17, i32 %18, i32 %19, %struct.timeval* %4, i32 4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %15
%23 = load i32, i32* %5
%24 = call i32 @close_nointr_nofail(i32 %23)
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
store i32 %26, i32* %2
br label %29
27: ; preds = %15
%28 = load i32, i32* %5
store i32 %28, i32* %2
br label %29
29: ; preds = %27, %22, %12
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@AF_UNIX = global i32 0
@SOCK_CLOEXEC = global i32 0
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDTIMEO = global i32 0
define i32 @create_log_socket(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_CLOEXEC
%6 = or i32 %5, %0
%7 = tail call i32 @socket(i32 %4, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
br label %22
12: ; preds = %1
%13 = call i32 @timeval_store(%struct.timeval* %2, i32 5000000)
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_SNDTIMEO
%16 = call i64 @setsockopt(i32 %7, i32 %14, i32 %15, %struct.timeval* %2, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = call i32 @close_nointr_nofail(i32 %7)
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
br label %22
22: ; preds = %12, %18, %9
%23 = phi i32 [ %11, %9 ], [ %21, %18 ], [ %7, %12 ]
ret i32 %23
}