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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, TYPE_1__ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int wheatCreateSocket(char *, int /*<<< orphan*/);
int wheatListen(char *, int, struct sockaddr *, int);
int /*<<< orphan*/ wheatSetError(char *, char *);
int wheatTcpServer(char *err, char *bind_addr, int port) {
  int s;
  struct sockaddr_in sa;
  if ((s = wheatCreateSocket(err, AF_INET)) == -1)
    return -1;
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind_addr && inet_pton(AF_INET, bind_addr, &sa.sin_addr) != 1) {
    wheatSetError(err, "wheatTcpServer: invalid bind address");
    close(s);
    return -1;
  }
  if (wheatListen(err, s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    wheatSetError(err, "wheatTcpServer: listen failed");
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<wheatTcpServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1080 <wheatCreateSocket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11bd <wheatTcpServer+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <wheatTcpServer+0x10b>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1243 <wheatTcpServer+0xc3>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1090 <inet_pton@plt>
cmp    $0x1,%eax
je     1243 <wheatTcpServer+0xc3>
mov    -0x10(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   10a0 <wheatSetError@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <wheatTcpServer+0x10b>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
lea    -0x30(%rbp),%rdx
mov    $0xc,%ecx
call   1030 <wheatListen@plt>
cmp    $0xffffffff,%eax
jne    1285 <wheatTcpServer+0x105>
mov    -0x10(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   10a0 <wheatSetError@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <wheatTcpServer+0x10b>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wheatTcpServer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x2e4b(%rip),%r13
mov    0x0(%r13),%esi
call   1080 <wheatCreateSocket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1245 <wheatTcpServer+0xc5>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
test   %r12,%r12
je     1208 <wheatTcpServer+0x88>
mov    0x0(%r13),%edi
lea    0x8(%rsp),%rdx
mov    %r12,%rsi
call   1090 <inet_pton@plt>
cmp    $0x1,%eax
jne    1226 <wheatTcpServer+0xa6>
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0xc,%ecx
call   1030 <wheatListen@plt>
cmp    $0xffffffff,%eax
je     122f <wheatTcpServer+0xaf>
mov    %ebp,%r14d
jmp    1245 <wheatTcpServer+0xc5>
lea    0xdd3(%rip),%rsi
jmp    1236 <wheatTcpServer+0xb6>
lea    0xdef(%rip),%rsi
mov    %r15,%rdi
call   10a0 <wheatSetError@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [37 x i8] c"wheatTcpServer: invalid bind address\00"
@.str.1 = [30 x i8] c"wheatTcpServer: listen failed\00"
define i32 @wheatTcpServer(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @AF_INET
%12 = call i32 @wheatCreateSocket(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %52
15: ; preds = %3
%16 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* %7
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i8*, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %39
28: ; preds = %15
%29 = load i32, i32* @AF_INET
%30 = load i8*, i8** %6
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%32 = call i32 @inet_pton(i32 %29, i8* %30, %struct.TYPE_2__* %31)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %39
34: ; preds = %28
%35 = load i8*, i8** %5
%36 = call i32 @wheatSetError(i8* %35, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
store i32 -1, i32* %4
br label %52
39: ; preds = %28, %15
%40 = load i8*, i8** %5
%41 = load i32, i32* %8
%42 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%43 = call i32 @wheatListen(i8* %40, i32 %41, %struct.sockaddr* %42, i32 12)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = load i8*, i8** %5
%47 = call i32 @wheatSetError(i8* %46, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %4
br label %52
50: ; preds = %39
%51 = load i32, i32* %8
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %45, %34, %14
%53 = load i32, i32* %4
ret i32 %53
}

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