#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int create_server_socket(struct sockaddr_in *server_addr, int portnumber) {
  int sockfd;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "Socket error:%s\n\a", strerror(errno));
    return -1;
  }
  printf("dbg: create socket!\n");
  bzero(server_addr, sizeof(struct sockaddr_in));
  server_addr->sin_family = AF_INET;
  server_addr->sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr->sin_port = htons(portnumber);
  if (bind(sockfd, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) ==
      -1) {
    printf("bind error:%s\n\a", strerror(errno));
    return -1;
  }
  printf("dbg: bind socket!\n");
  if (listen(sockfd, 20) == -1) {
    printf("listen error:%s\n\a", strerror(errno));
    return -1;
  }
  printf("dbg: listening...\n");
  return sockfd;
}


x86 assembly (clang, -O0):
<create_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11fc <create_server_socket+0x6c>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe15(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <create_server_socket+0x167>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1294 <create_server_socket+0x104>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <create_server_socket+0x167>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    $0x14,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    12e3 <create_server_socket+0x153>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <create_server_socket+0x167>
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e34(%rip),%r15
mov    (%r15),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1247 <create_server_socket+0xa7>
mov    %eax,%ebp
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rbx)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x4(%rbx)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1271 <create_server_socket+0xd1>
lea    0xe25(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    $0x14,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     1288 <create_server_socket+0xe8>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
jmp    12ac <create_server_socket+0x10c>
mov    0x2d82(%rip),%rax
mov    (%rax),%ebx
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd9b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
jmp    12a7 <create_server_socket+0x107>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd8c(%rip),%rdi
jmp    129d <create_server_socket+0xfd>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd85(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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