#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_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sock_name;
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ TCP_NODELAY;
int accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int socket_accept(int sock, char *ip, int flags) {
  struct sockaddr_in sock_name;
  unsigned addrlen = sizeof(sock_name);
  int new = accept(sock, (struct sockaddr *)&sock_name, &addrlen);
  if (new < 0)
    return -1;
  if (ip)
    strcpy(ip, inet_ntoa(sock_name.sin_addr));
  int opt = 1;
  setsockopt(new, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
  return new;
}


x86 assembly (clang, -O0):
<socket_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1182 <socket_accept+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <socket_accept+0x99>
cmpq   $0x0,-0x10(%rbp)
je     11a8 <socket_accept+0x68>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_accept>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
movl   $0x4,0x14(%rsp)
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   1060 <accept@plt>
test   %eax,%eax
js     11ad <socket_accept+0x6d>
mov    %eax,%ebx
test   %r14,%r14
je     117f <socket_accept+0x3f>
mov    0x8(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
movl   $0x1,0x10(%rsp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
jmp    11b2 <socket_accept+0x72>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @socket_accept(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 4, i32* %9
%12 = load i32, i32* %5
%13 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%14 = call i32 @accept(i32 %12, %struct.sockaddr* %13, i32* %9)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %33
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i8*, i8** %6
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @inet_ntoa(i32 %24)
%26 = call i32 @strcpy(i8* %22, i32 %25)
br label %27
27: ; preds = %21, %18
store i32 1, i32* %11
%28 = load i32, i32* %10
%29 = load i32, i32* @IPPROTO_TCP
%30 = load i32, i32* @TCP_NODELAY
%31 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i32* %11, i32 4)
%32 = load i32, i32* %10
store i32 %32, i32* %4
br label %33
33: ; preds = %27, %17
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define i32 @socket_accept(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%9 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%10 = call i32 @accept(i32 %0, %struct.sockaddr* %9, i32* %5)
%11 = icmp slt i32 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %3
%13 = icmp eq i8* %1, null
br i1 %13, label %19, label %14
14: ; preds = %12
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @inet_ntoa(i32 %16)
%18 = call i32 @strcpy(i8* %1, i32 %17)
br label %19
19: ; preds = %14, %12
%20 = bitcast i32* %6 to i8*
store i32 1, i32* %6
%21 = load i32, i32* @IPPROTO_TCP
%22 = load i32, i32* @TCP_NODELAY
%23 = call i32 @setsockopt(i32 %10, i32 %21, i32 %22, i32* %6, i32 4)
br label %24
24: ; preds = %3, %19
%25 = phi i32 [ %10, %19 ], [ -1, %3 ]
ret i32 %25
}