#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__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  void *sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
void *htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, TYPE_1__ *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int startserver(char *addr, int port) {
  int listenfd = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in servaddr;
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(port);
  if (addr != NULL) {
    if (inet_pton(AF_INET, addr, &servaddr.sin_addr) == 0)
      perror("addr error");
  } else
    servaddr.sin_addr.s_addr = htons(INADDR_ANY);
  if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
    perror("bind");
  if (listen(listenfd, 1) == -1)
    perror("listen");
  return listenfd;
}


x86 assembly (clang, -O0):
<startserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11ff <startserver+0x8f>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x0,%rax
jne    11fa <startserver+0x8a>
lea    0xe0b(%rip),%rdi
call   1080 <perror@plt>
jmp    1211 <startserver+0xa1>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1237 <startserver+0xc7>
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1259 <startserver+0xe9>
lea    0xdbc(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startserver>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e62(%rip),%r15
mov    (%r15),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%r14d
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %rax,0x8(%rsp)
test   %rbx,%rbx
je     11e4 <startserver+0x74>
mov    (%r15),%edi
mov    %rsp,%rdx
mov    %rbx,%rsi
call   1050 <inet_pton@plt>
test   %rax,%rax
jne    11f6 <startserver+0x86>
lea    0xe23(%rip),%rdi
call   1080 <perror@plt>
jmp    11f6 <startserver+0x86>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x18,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     122b <startserver+0xbb>
mov    %r14d,%edi
mov    $0x1,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     1239 <startserver+0xc9>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
jmp    120b <startserver+0x9b>
lea    0xdd0(%rip),%rdi
call   1080 <perror@plt>
jmp    121d <startserver+0xad>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"addr error\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
define i32 @startserver(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i32 %1, i32* %4
%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* %5
%10 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 24)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* %4
%14 = call i8* @htons(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i8* %14, i8** %15
%16 = load i8*, i8** %3
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %27
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i8*, i8** %3
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%22 = call i64 @inet_pton(i32 %19, i8* %20, %struct.TYPE_2__* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
br label %32
27: ; preds = %2
%28 = load i32, i32* @INADDR_ANY
%29 = call i8* @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i8* %29, i8** %31
br label %32
32: ; preds = %27, %26
%33 = load i32, i32* %5
%34 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%35 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 24)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %32
%40 = load i32, i32* %5
%41 = call i32 @listen(i32 %40, i32 1)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"addr error\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
define i32 @startserver(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 24)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = call i8* @htons(i32 %1)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i8* %11, i8** %12
%13 = icmp eq i8* %0, null
br i1 %13, label %21, label %14
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%17 = call i64 @inet_pton(i32 %15, i8* %0, %struct.TYPE_2__* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %25
21: ; preds = %2
%22 = load i32, i32* @INADDR_ANY
%23 = call i8* @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i8* %23, i8** %24
br label %25
25: ; preds = %14, %19, %21
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @bind(i32 %6, %struct.sockaddr* %26, i32 24)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = call i32 @listen(i32 %6, i32 1)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
ret i32 %6
}