#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ reuse;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char const *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
int create_server_proc(const char *ip, int port) {
  int fd;
  struct sockaddr_in servaddr;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    fprintf(stderr, "create socket fail,erron:%d,reason:%s\n", errno,
            strerror(errno));
    return -1;
  }
  int reuse = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
    return -1;
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  inet_pton(AF_INET, ip, &servaddr.sin_addr);
  servaddr.sin_port = htons(port);
  if (bind(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
    perror("bind error: ");
    return -1;
  }
  listen(fd, 5);
  return fd;
}


x86 assembly (clang, -O0):
<create_server_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    121c <create_server_proc+0x7c>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%edx
mov    %rax,%rcx
lea    0xdf5(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e0 <create_server_proc+0x140>
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    125c <create_server_proc+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e0 <create_server_proc+0x140>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   1060 <inet_pton@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    12cd <create_server_proc+0x12d>
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e0 <create_server_proc+0x140>
mov    -0x18(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_server_proc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e25(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     126b <create_server_proc+0xcb>
mov    %eax,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     129c <create_server_proc+0xfc>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x0(%r13),%edi
mov    %edi,0x10(%rsp)
lea    0xc(%rsp),%rdx
mov    %r15,%rsi
call   1060 <inet_pton@plt>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     12ad <create_server_proc+0x10d>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
mov    %ebx,%ebp
jmp    129c <create_server_proc+0xfc>
mov    0x2d5e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d45(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10b0 <strerror@plt>
lea    0xd75(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd73(%rip),%rdi
call   1090 <perror@plt>
jmp    129c <create_server_proc+0xfc>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"create socket fail,erron:%d,reason:%s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [13 x i8] c"bind error: \00"
define i32 @create_server_proc(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %16, i8* %18)
store i32 -1, i32* %3
br label %48
20: ; preds = %2
store i32 1, i32* %8
%21 = load i32, i32* %6
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i32* %8, i32 4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -1, i32* %3
br label %48
27: ; preds = %20
%28 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @AF_INET
%32 = load i8*, i8** %4
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%34 = call i32 @inet_pton(i32 %31, i8* %32, i32* %33)
%35 = load i32, i32* %5
%36 = call i32 @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%40 = call i32 @bind(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %27
%43 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %48
44: ; preds = %27
%45 = load i32, i32* %6
%46 = call i32 @listen(i32 %45, i32 5)
%47 = load i32, i32* %6
store i32 %47, i32* %3
br label %48
48: ; preds = %44, %42, %26, %14
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"create socket fail,erron:%d,reason:%s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [13 x i8] c"bind error: \00"
define i32 @create_server_proc(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13)
br label %38
15: ; preds = %2
%16 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %8, i32 %17, i32 %18, i32* %4, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %36, label %21
21: ; preds = %15
%22 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%26 = call i32 @inet_pton(i32 %23, i8* %0, i32* %25)
%27 = call i32 @htons(i32 %1)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @bind(i32 %8, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %36
34: ; preds = %21
%35 = call i32 @listen(i32 %8, i32 5)
br label %36
36: ; preds = %15, %34, %32
%37 = phi i32 [ -1, %32 ], [ %8, %34 ], [ -1, %15 ]
br label %38
38: ; preds = %36, %10
%39 = phi i32 [ -1, %10 ], [ %37, %36 ]
ret i32 %39
}