#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_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct log_cfg {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int AF_INET;
int AF_INET6;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ log_err(struct log_cfg const *, char *);
int /*<<< orphan*/ log_perror(struct log_cfg const *, int, char *);
int /*<<< orphan*/ memset(struct sockaddr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int net_accept(const struct log_cfg *lcfg, int sockfd, int af,
               struct sockaddr *storeaddr) {
  int ret = -1, storerr = errno;
  socklen_t addr_size =
      ((af == AF_INET) ? (socklen_t)sizeof(struct sockaddr_in)
                       : (socklen_t)sizeof(struct sockaddr_in6));
  if (af != AF_INET && af != AF_INET6) {
    log_err(lcfg, "net: Unsupported address family!");
    return -1;
  }
  memset(storeaddr, 0, addr_size);
  errno = 0;
  ret = accept(sockfd, storeaddr, &addr_size);
  if (ret == -1) {
    storerr = errno;
    log_perror(lcfg, storerr, "net: accept");
  }
  errno = storerr;
  return ret;
}


x86 assembly (clang, -O0):
<net_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%ecx
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%eax
cmp    %edx,%ecx
cmove  %eax,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%eax
je     11c2 <net_accept+0x82>
mov    -0x18(%rbp),%eax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%eax
je     11c2 <net_accept+0x82>
mov    -0x10(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1060 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <net_accept+0xeb>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    0x2df9(%rip),%rax
movl   $0x0,(%rax)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1040 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1219 <net_accept+0xd9>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
lea    0xe0d(%rip),%rdx
call   1050 <log_perror@plt>
mov    -0x28(%rbp),%ecx
mov    0x2dad(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_accept>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e75(%rip),%r12
mov    (%r12),%r15d
movl   $0x4,0xc(%rsp)
mov    0x2e7a(%rip),%rax
cmp    %edx,(%rax)
je     1193 <net_accept+0x53>
mov    0x2e67(%rip),%rax
cmp    %edx,(%rax)
je     1193 <net_accept+0x53>
lea    0xe7c(%rip),%rsi
mov    %r14,%rdi
call   1060 <log_err@plt>
mov    $0xffffffff,%ebx
jmp    11da <net_accept+0x9a>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
movl   $0x0,(%r12)
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11d6 <net_accept+0x96>
mov    (%r12),%r15d
lea    0xe56(%rip),%rdx
mov    %r14,%rdi
mov    %r15d,%esi
call   1050 <log_perror@plt>
mov    %r15d,(%r12)
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.log_cfg = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@AF_INET = global i32 0
@AF_INET6 = global i32 0
@.str = [33 x i8] c"net: Unsupported address family!\00"
@.str.1 = [12 x i8] c"net: accept\00"
define i32 @net_accept(%struct.log_cfg* %0, i32 %1, i32 %2, %struct.sockaddr* %3) {
%5 = alloca i32
%6 = alloca %struct.log_cfg*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.log_cfg* %0, %struct.log_cfg** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store %struct.sockaddr* %3, %struct.sockaddr** %9
store i32 -1, i32* %10
%13 = load i32, i32* @errno
store i32 %13, i32* %11
%14 = load i32, i32* %8
%15 = load i32, i32* @AF_INET
%16 = icmp eq i32 %14, %15
%17 = zext i1 %16 to i64
%18 = select i1 %16, i32 4, i32 4
store i32 %18, i32* %12
%19 = load i32, i32* %8
%20 = load i32, i32* @AF_INET
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %29
22: ; preds = %4
%23 = load i32, i32* %8
%24 = load i32, i32* @AF_INET6
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load %struct.log_cfg*, %struct.log_cfg** %6
%28 = call i32 @log_err(%struct.log_cfg* %27, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %46
29: ; preds = %22, %4
%30 = load %struct.sockaddr*, %struct.sockaddr** %9
%31 = load i32, i32* %12
%32 = call i32 @memset(%struct.sockaddr* %30, i32 0, i32 %31)
store i32 0, i32* @errno
%33 = load i32, i32* %7
%34 = load %struct.sockaddr*, %struct.sockaddr** %9
%35 = call i32 @accept(i32 %33, %struct.sockaddr* %34, i32* %12)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %43
38: ; preds = %29
%39 = load i32, i32* @errno
store i32 %39, i32* %11
%40 = load %struct.log_cfg*, %struct.log_cfg** %6
%41 = load i32, i32* %11
%42 = call i32 @log_perror(%struct.log_cfg* %40, i32 %41, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %38, %29
%44 = load i32, i32* %11
store i32 %44, i32* @errno
%45 = load i32, i32* %10
store i32 %45, i32* %5
br label %46
46: ; preds = %43, %26
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
%struct.log_cfg = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@AF_INET = global i32 0
@AF_INET6 = global i32 0
@.str = [33 x i8] c"net: Unsupported address family!\00"
@.str.1 = [12 x i8] c"net: accept\00"
define i32 @net_accept(%struct.log_cfg* %0, i32 %1, i32 %2, %struct.sockaddr* %3) {
%5 = alloca i32
%6 = load i32, i32* @errno
%7 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%8 = load i32, i32* @AF_INET
%9 = icmp eq i32 %8, %2
%10 = load i32, i32* @AF_INET6
%11 = icmp eq i32 %10, %2
%12 = select i1 %9, i1 true, i1 %11
br i1 %12, label %15, label %13
13: ; preds = %4
%14 = tail call i32 @log_err(%struct.log_cfg* %0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %24
15: ; preds = %4
%16 = tail call i32 @memset(%struct.sockaddr* %3, i32 0, i32 4)
store i32 0, i32* @errno
%17 = call i32 @accept(i32 %1, %struct.sockaddr* %3, i32* %5)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = call i32 @log_perror(%struct.log_cfg* %0, i32 %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19, %15
%23 = phi i32 [ %20, %19 ], [ %6, %15 ]
store i32 %23, i32* @errno
br label %24
24: ; preds = %22, %13
%25 = phi i32 [ -1, %13 ], [ %17, %22 ]
ret i32 %25
}