#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, struct sockaddr_storage *,
                          int);
int /*<<< orphan*/ perror(char *);
int socket_tcp_accept(int socket, struct sockaddr_storage *addr) {
  struct sockaddr_storage address;
  socklen_t addr_size;
  int sd;
  addr_size = sizeof(struct sockaddr_storage);
  if ((sd = accept(socket, (struct sockaddr *)&address, &addr_size)) < 0) {
    perror("accept()");
    return (-1);
  }
  if (addr != NULL)
    memcpy(addr, &address, sizeof(struct sockaddr_storage));
  return (sd);
}


x86 assembly (clang, -O0):
<socket_tcp_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <accept@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    117a <socket_tcp_accept+0x4a>
lea    0xe97(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    119d <socket_tcp_accept+0x6d>
cmpq   $0x0,-0x10(%rbp)
je     1197 <socket_tcp_accept+0x67>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_tcp_accept>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1050 <accept@plt>
test   %eax,%eax
js     1171 <socket_tcp_accept+0x41>
mov    %eax,%ebx
test   %r14,%r14
je     1167 <socket_tcp_accept+0x37>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe88(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1167 <socket_tcp_accept+0x37>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"accept()\00"
define i32 @socket_tcp_accept(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_storage*
%6 = alloca %struct.sockaddr_storage
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_storage* %1, %struct.sockaddr_storage** %5
store i32 4, i32* %7
%9 = load i32, i32* %4
%10 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %7)
store i32 %11, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %23
15: ; preds = %2
%16 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%17 = icmp ne %struct.sockaddr_storage* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%20 = call i32 @memcpy(%struct.sockaddr_storage* %19, %struct.sockaddr_storage* %6, i32 4)
br label %21
21: ; preds = %18, %15
%22 = load i32, i32* %8
store i32 %22, i32* %3
br label %23
23: ; preds = %21, %13
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"accept()\00"
define i32 @socket_tcp_accept(i32 %0, %struct.sockaddr_storage* %1) {
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = bitcast %struct.sockaddr_storage* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%7 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%8 = call i32 @accept(i32 %0, %struct.sockaddr* %7, i32* %4)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %16
12: ; preds = %2
%13 = icmp eq %struct.sockaddr_storage* %1, null
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = call i32 @memcpy(%struct.sockaddr_storage* %1, %struct.sockaddr_storage* %3, i32 4)
br label %16
16: ; preds = %12, %14, %10
%17 = phi i32 [ -1, %10 ], [ %8, %14 ], [ %8, %12 ]
ret i32 %17
}