#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*/ sendbuffer;
typedef int /*<<< orphan*/ reuse;
typedef int /*<<< orphan*/ recvbuffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ SO_SNDBUF;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int newSocket(void) {
  int reuse = 1;
  int sendbuffer = 32768;
  int recvbuffer = 32768;
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("can not create socket:");
    return -1;
  }
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuffer, sizeof(sendbuffer));
  setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &recvbuffer, sizeof(recvbuffer));
  int old = fcntl(sockfd, F_GETFL);
  int new = old | O_NONBLOCK;
  fcntl(sockfd, F_SETFL, new);
  return sockfd;
}


x86 assembly (clang, -O0):
<newSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0x8(%rbp)
movl   $0x8000,-0xc(%rbp)
movl   $0x8000,-0x10(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119b <newSocket+0x5b>
lea    0xe76(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <newSocket+0x108>
mov    -0x14(%rbp),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2db1(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newSocket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x1,0x14(%rsp)
movl   $0x8000,0x10(%rsp)
movl   $0x8000,0xc(%rsp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     121d <newSocket+0xdd>
mov    0x2e5e(%rip),%r14
mov    (%r14),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r14),%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r14),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2ddb(%rip),%rcx
or     (%rcx),%eax
mov    0x2db2(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xddc(%rip),%rdi
call   1050 <perror@plt>
jmp    1213 <newSocket+0xd3>

LLVM IR (-O0):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"can not create socket:\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @newSocket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 1, i32* %2
store i32 32768, i32* %3
store i32 32768, i32* %4
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %39
15: ; preds = %0
%16 = load i32, i32* %5
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %2, i32 4)
%20 = load i32, i32* %5
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_SNDBUF
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %3, i32 4)
%24 = load i32, i32* %5
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_RCVBUF
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %4, i32 4)
%28 = load i32, i32* %5
%29 = load i32, i32* @F_GETFL
%30 = call i32 (i32, i32, ...) @fcntl(i32 %28, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = load i32, i32* @O_NONBLOCK
%33 = or i32 %31, %32
store i32 %33, i32* %7
%34 = load i32, i32* %5
%35 = load i32, i32* @F_SETFL
%36 = load i32, i32* %7
%37 = call i32 (i32, i32, ...) @fcntl(i32 %34, i32 %35, i32 %36)
%38 = load i32, i32* %5
store i32 %38, i32* %1
br label %39
39: ; preds = %15, %13
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"can not create socket:\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @newSocket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%5 = bitcast i32* %2 to i8*
store i32 32768, i32* %2
%6 = bitcast i32* %3 to i8*
store i32 32768, i32* %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %29
13: ; preds = %0
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %9, i32 %14, i32 %15, i32* %1, i32 4)
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_SNDBUF
%19 = call i32 @setsockopt(i32 %9, i32 %17, i32 %18, i32* %2, i32 4)
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_RCVBUF
%22 = call i32 @setsockopt(i32 %9, i32 %20, i32 %21, i32* %3, i32 4)
%23 = load i32, i32* @F_GETFL
%24 = call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %23)
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %25, %24
%27 = load i32, i32* @F_SETFL
%28 = call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %27, i32 %26)
br label %29
29: ; preds = %13, %11
ret i32 %9
}