#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 */
typedef int /*<<< orphan*/ window_size;
struct TYPE_2__ {
  unsigned long s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ ip_addr;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_SNDBUF;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int *);
unsigned long htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
char *inet_ntoa(TYPE_1__);
int listen(int, int);
int /*<<< orphan*/ memcpy(unsigned long *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int setup_net(int isserver, char *hostname, int port, int window_size,
              int *sock) {
  int ssock, status;
  unsigned long ip_addr;
  socklen_t client_len, winlen;
  struct hostent *hostptr;
  struct sockaddr_in server, client;
  *sock = 0;
  memset((char *)&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_port = htons((unsigned short)port);
  ssock = socket(AF_INET, SOCK_STREAM, 0);
  if (ssock == -1) {
    perror("Error creating socket");
    return (1);
  }
  if (window_size > 0) {
    status = setsockopt(ssock, SOL_SOCKET, SO_SNDBUF, (char *)&window_size,
                        sizeof(window_size));
    if (status != 0) {
      perror("Error setting socket send buffer");
      close(ssock);
      return (1);
    }
    status = setsockopt(ssock, SOL_SOCKET, SO_RCVBUF, (char *)&window_size,
                        sizeof(window_size));
    if (status != 0) {
      perror("Error setting socket receive buffer");
      close(ssock);
      return (1);
    }
    winlen = sizeof(window_size);
    status =
        getsockopt(ssock, SOL_SOCKET, SO_SNDBUF, (char *)&window_size, &winlen);
    if (status != 0) {
      close(ssock);
      perror("Getting socket options");
      return (1);
    }
    printf("Sending buffersize set to %d Kbytes\n", window_size / 1024);
  }
  if (isserver) {
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    status = bind(ssock, (struct sockaddr *)&server, sizeof(server));
    if (status != 0) {
      perror("Error binding socket");
      close(ssock);
      return (1);
    }
    status = listen(ssock, 1);
    if (status != 0) {
      perror("Error binding socket");
      close(ssock);
      return (1);
    }
    printf("Waiting for connection\n");
    client_len = sizeof(client);
    *sock = accept(ssock, (struct sockaddr *)&client, &client_len);
    if (*sock == -1) {
      perror("Error connecting to client");
      close(ssock);
      return (1);
    }
    printf("Got a connection from %s\n", inet_ntoa(client.sin_addr));
  } else {
    hostptr = gethostbyname(hostname);
    if (hostptr == NULL) {
      fprintf(stderr, "Failed to look up hostname %s\n", hostname);
      close(ssock);
      return (1);
    }
    memcpy(&ip_addr, (char *)hostptr->h_addr, sizeof(ip_addr));
    server.sin_addr.s_addr = ip_addr;
    printf("Connecting to %s\n", inet_ntoa(server.sin_addr));
    status = connect(ssock, (struct sockaddr *)&server, sizeof(server));
    if (status != 0) {
      perror("Failed to connect to server");
      close(ssock);
      return (1);
    }
    *sock = ssock;
  }
  return (0);
}


x86 assembly (clang, -O0):
<setup_net>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%eax
movzwl %ax,%edi
call   1050 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    129b <setup_net+0x8b>
lea    0xd76(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158f <setup_net+0x37f>
cmpl   $0x0,-0x18(%rbp)
jle    13b8 <setup_net+0x1a8>
mov    -0x24(%rbp),%edi
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
mov    0x2d20(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     12f6 <setup_net+0xe6>
lea    0xd39(%rip),%rdi
call   1100 <perror@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    158f <setup_net+0x37f>
mov    -0x24(%rbp),%edi
mov    0x2ce8(%rip),%rax
mov    (%rax),%esi
mov    0x2cb7(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1347 <setup_net+0x137>
lea    0xd09(%rip),%rdi
call   1100 <perror@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    158f <setup_net+0x37f>
movl   $0x4,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2c90(%rip),%rax
mov    (%rax),%esi
mov    0x2c77(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   10a0 <getsockopt@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     139d <setup_net+0x18d>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
lea    0xccf(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158f <setup_net+0x37f>
mov    -0x18(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
lea    0xcc1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
je     14c5 <setup_net+0x2b5>
mov    0x2c07(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %rax,-0x50(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x10,%edx
call   10f0 <bind@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1412 <setup_net+0x202>
lea    0xc9e(%rip),%rdi
call   1100 <perror@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    158f <setup_net+0x37f>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   10e0 <listen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     144c <setup_net+0x23c>
lea    0xc64(%rip),%rdi
call   1100 <perror@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    158f <setup_net+0x37f>
lea    0xc59(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x10,-0x34(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x60(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1110 <accept@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    14a6 <setup_net+0x296>
lea    0xc37(%rip),%rdi
call   1100 <perror@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    158f <setup_net+0x37f>
mov    -0x60(%rbp),%rdi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xc26(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1588 <setup_net+0x378>
mov    -0x10(%rbp),%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    150a <setup_net+0x2fa>
mov    0x2ad4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xc08(%rip),%rsi
call   10c0 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    158f <setup_net+0x37f>
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
lea    -0x30(%rbp),%rdi
mov    $0x8,%edx
call   10d0 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xbde(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x10,%edx
call   1120 <connect@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     157f <setup_net+0x36f>
lea    0xbc4(%rip),%rdi
call   1100 <perror@plt>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    158f <setup_net+0x37f>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_net>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,%r14
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %rsi,%r15
mov    %edi,%r12d
mov    %ecx,0xc(%rsp)
movl   $0x0,(%r8)
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <memset@plt>
mov    0x2d82(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x24(%rsp)
movzwl %bx,%edi
call   1060 <htons@plt>
mov    %eax,0x20(%rsp)
mov    0x0(%r13),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
cmp    $0xffffffff,%eax
je     144f <setup_net+0x22f>
mov    %eax,%ebx
test   %ebp,%ebp
jle    1336 <setup_net+0x116>
mov    0x2d48(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2d2e(%rip),%r13
mov    0x0(%r13),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %eax,%eax
jne    1458 <setup_net+0x238>
mov    0x0(%rbp),%esi
mov    0x2cee(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %eax,%eax
jne    1461 <setup_net+0x241>
movl   $0x4,0x10(%rsp)
mov    0x0(%rbp),%esi
mov    0x0(%r13),%edx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %ebx,%edi
call   10b0 <getsockopt@plt>
test   %eax,%eax
jne    146a <setup_net+0x24a>
mov    0xc(%rsp),%eax
lea    0x3ff(%rax),%esi
test   %eax,%eax
cmovns %eax,%esi
sar    $0xa,%esi
lea    0xd43(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
test   %r12d,%r12d
je     13d7 <setup_net+0x1b7>
mov    0x2c8a(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %rax,0x18(%rsp)
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1100 <bind@plt>
test   %eax,%eax
jne    147f <setup_net+0x25f>
mov    $0x1,%ebp
mov    %ebx,%edi
mov    $0x1,%esi
call   10f0 <listen@plt>
test   %eax,%eax
jne    1488 <setup_net+0x268>
lea    0xda3(%rip),%rdi
call   1030 <puts@plt>
movl   $0x10,0x14(%rsp)
lea    0x28(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   1120 <accept@plt>
mov    %eax,(%r14)
cmp    $0xffffffff,%eax
je     1491 <setup_net+0x271>
mov    0x28(%rsp),%rdi
call   1050 <inet_ntoa@plt>
lea    0xd01(%rip),%rdi
xor    %ebp,%ebp
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    14be <setup_net+0x29e>
mov    %r15,%rdi
call   10c0 <gethostbyname@plt>
test   %rax,%rax
je     1435 <setup_net+0x215>
mov    (%rax),%rsi
lea    0x28(%rsp),%rdi
mov    $0x8,%edx
call   10e0 <memcpy@plt>
mov    0x28(%rsp),%rdi
mov    %rdi,0x18(%rsp)
call   1050 <inet_ntoa@plt>
lea    0xcf4(%rip),%rdi
xor    %ebp,%ebp
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1130 <connect@plt>
test   %eax,%eax
jne    14a6 <setup_net+0x286>
mov    %ebx,(%r14)
jmp    14be <setup_net+0x29e>
mov    0x2b7c(%rip),%rax
mov    (%rax),%edi
lea    0xc9c(%rip),%rsi
mov    %r15,%rdx
call   10d0 <fprintf@plt>
jmp    14b2 <setup_net+0x292>
lea    0xbaa(%rip),%rdi
jmp    1478 <setup_net+0x258>
lea    0xbb7(%rip),%rdi
jmp    14ad <setup_net+0x28d>
lea    0xbcf(%rip),%rdi
jmp    14ad <setup_net+0x28d>
mov    %ebx,%edi
call   10a0 <close@plt>
lea    0xbe3(%rip),%rdi
call   1110 <perror@plt>
jmp    14b9 <setup_net+0x299>
lea    0xc11(%rip),%rdi
jmp    14ad <setup_net+0x28d>
lea    0xc08(%rip),%rdi
jmp    1498 <setup_net+0x278>
lea    0xc14(%rip),%rdi
call   1110 <perror@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
jmp    14be <setup_net+0x29e>
lea    0xc65(%rip),%rdi
call   1110 <perror@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Error creating socket\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@.str.1 = [33 x i8] c"Error setting socket send buffer\00"
@SO_RCVBUF = global i32 0
@.str.2 = [36 x i8] c"Error setting socket receive buffer\00"
@.str.3 = [23 x i8] c"Getting socket options\00"
@.str.4 = [37 x i8] c"Sending buffersize set to %d Kbytes\0A\00"
@INADDR_ANY = global i32 0
@.str.5 = [21 x i8] c"Error binding socket\00"
@.str.6 = [24 x i8] c"Waiting for connection\0A\00"
@.str.7 = [27 x i8] c"Error connecting to client\00"
@.str.8 = [26 x i8] c"Got a connection from %s\0A\00"
@stderr = global i32 0
@.str.9 = [31 x i8] c"Failed to look up hostname %s\0A\00"
@.str.10 = [18 x i8] c"Connecting to %s\0A\00"
@.str.11 = [28 x i8] c"Failed to connect to server\00"
define i32 @setup_net(i32 %0, i8* %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca %struct.hostent*
%18 = alloca %struct.sockaddr_in
%19 = alloca %struct.sockaddr_in
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%20 = load i32*, i32** %11
store i32 0, i32* %20
%21 = bitcast %struct.sockaddr_in* %18 to i8*
%22 = call i32 @memset(i8* %21, i32 0, i32 16)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* %9
%26 = trunc i32 %25 to i16
%27 = call i32 @htons(i16 zeroext %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %5
%35 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %6
br label %162
36: ; preds = %5
%37 = load i32, i32* %10
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %79
39: ; preds = %36
%40 = load i32, i32* %12
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_SNDBUF
%43 = bitcast i32* %10 to i8*
%44 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i8* %43, i32 4)
store i32 %44, i32* %13
%45 = load i32, i32* %13
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%49 = load i32, i32* %12
%50 = call i32 @close(i32 %49)
store i32 1, i32* %6
br label %162
51: ; preds = %39
%52 = load i32, i32* %12
%53 = load i32, i32* @SOL_SOCKET
%54 = load i32, i32* @SO_RCVBUF
%55 = bitcast i32* %10 to i8*
%56 = call i32 @setsockopt(i32 %52, i32 %53, i32 %54, i8* %55, i32 4)
store i32 %56, i32* %13
%57 = load i32, i32* %13
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %51
%60 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%61 = load i32, i32* %12
%62 = call i32 @close(i32 %61)
store i32 1, i32* %6
br label %162
63: ; preds = %51
store i32 4, i32* %16
%64 = load i32, i32* %12
%65 = load i32, i32* @SOL_SOCKET
%66 = load i32, i32* @SO_SNDBUF
%67 = bitcast i32* %10 to i8*
%68 = call i32 @getsockopt(i32 %64, i32 %65, i32 %66, i8* %67, i32* %16)
store i32 %68, i32* %13
%69 = load i32, i32* %13
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %63
%72 = load i32, i32* %12
%73 = call i32 @close(i32 %72)
%74 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %6
br label %162
75: ; preds = %63
%76 = load i32, i32* %10
%77 = sdiv i32 %76, 1024
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %75, %36
%80 = load i32, i32* %7
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %124
82: ; preds = %79
%83 = load i32, i32* @INADDR_ANY
%84 = call i64 @htonl(i32 %83)
%85 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 0
%86 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %85, i32 0, i32 0
store i64 %84, i64* %86
%87 = load i32, i32* %12
%88 = bitcast %struct.sockaddr_in* %18 to %struct.sockaddr*
%89 = call i32 @bind(i32 %87, %struct.sockaddr* %88, i32 16)
store i32 %89, i32* %13
%90 = load i32, i32* %13
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %96
92: ; preds = %82
%93 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%94 = load i32, i32* %12
%95 = call i32 @close(i32 %94)
store i32 1, i32* %6
br label %162
96: ; preds = %82
%97 = load i32, i32* %12
%98 = call i32 @listen(i32 %97, i32 1)
store i32 %98, i32* %13
%99 = load i32, i32* %13
%100 = icmp ne i32 %99, 0
br i1 %100, label %101, label %105
101: ; preds = %96
%102 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%103 = load i32, i32* %12
%104 = call i32 @close(i32 %103)
store i32 1, i32* %6
br label %162
105: ; preds = %96
%106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
store i32 16, i32* %15
%107 = load i32, i32* %12
%108 = bitcast %struct.sockaddr_in* %19 to %struct.sockaddr*
%109 = call i32 @accept(i32 %107, %struct.sockaddr* %108, i32* %15)
%110 = load i32*, i32** %11
store i32 %109, i32* %110
%111 = load i32*, i32** %11
%112 = load i32, i32* %111
%113 = icmp eq i32 %112, -1
br i1 %113, label %114, label %118
114: ; preds = %105
%115 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%116 = load i32, i32* %12
%117 = call i32 @close(i32 %116)
store i32 1, i32* %6
br label %162
118: ; preds = %105
%119 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 0
%120 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %119, i32 0, i32 0
%121 = load i64, i64* %120
%122 = call i8* @inet_ntoa(i64 %121)
%123 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i8* %122)
br label %161
124: ; preds = %79
%125 = load i8*, i8** %8
%126 = call %struct.hostent* @gethostbyname(i8* %125)
store %struct.hostent* %126, %struct.hostent** %17
%127 = load %struct.hostent*, %struct.hostent** %17
%128 = icmp eq %struct.hostent* %127, null
br i1 %128, label %129, label %135
129: ; preds = %124
%130 = load i32, i32* @stderr
%131 = load i8*, i8** %8
%132 = call i32 @fprintf(i32 %130, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i8* %131)
%133 = load i32, i32* %12
%134 = call i32 @close(i32 %133)
store i32 1, i32* %6
br label %162
135: ; preds = %124
%136 = load %struct.hostent*, %struct.hostent** %17
%137 = getelementptr inbounds %struct.hostent, %struct.hostent* %136, i32 0, i32 0
%138 = load i64, i64* %137
%139 = inttoptr i64 %138 to i8*
%140 = call i32 @memcpy(i64* %14, i8* %139, i32 8)
%141 = load i64, i64* %14
%142 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 0
%143 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %142, i32 0, i32 0
store i64 %141, i64* %143
%144 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 0
%145 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %144, i32 0, i32 0
%146 = load i64, i64* %145
%147 = call i8* @inet_ntoa(i64 %146)
%148 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8* %147)
%149 = load i32, i32* %12
%150 = bitcast %struct.sockaddr_in* %18 to %struct.sockaddr*
%151 = call i32 @connect(i32 %149, %struct.sockaddr* %150, i32 16)
store i32 %151, i32* %13
%152 = load i32, i32* %13
%153 = icmp ne i32 %152, 0
br i1 %153, label %154, label %158
154: ; preds = %135
%155 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0))
%156 = load i32, i32* %12
%157 = call i32 @close(i32 %156)
store i32 1, i32* %6
br label %162
158: ; preds = %135
%159 = load i32, i32* %12
%160 = load i32*, i32** %11
store i32 %159, i32* %160
br label %161
161: ; preds = %158, %118
store i32 0, i32* %6
br label %162
162: ; preds = %161, %154, %129, %114, %101, %92, %71, %59, %47, %34
%163 = load i32, i32* %6
ret i32 %163
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
%struct.hostent = type { i64 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Error creating socket\00"
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@.str.1 = [33 x i8] c"Error setting socket send buffer\00"
@SO_RCVBUF = global i32 0
@.str.2 = [36 x i8] c"Error setting socket receive buffer\00"
@.str.3 = [23 x i8] c"Getting socket options\00"
@.str.4 = [37 x i8] c"Sending buffersize set to %d Kbytes\0A\00"
@INADDR_ANY = global i32 0
@.str.5 = [21 x i8] c"Error binding socket\00"
@.str.7 = [27 x i8] c"Error connecting to client\00"
@.str.8 = [26 x i8] c"Got a connection from %s\0A\00"
@stderr = global i32 0
@.str.9 = [31 x i8] c"Failed to look up hostname %s\0A\00"
@.str.10 = [18 x i8] c"Connecting to %s\0A\00"
@.str.11 = [28 x i8] c"Failed to connect to server\00"
@str = [23 x i8] c"Waiting for connection\00"
define i32 @setup_net(i32 %0, i8* %1, i32 %2, i32 %3, i32* nocapture writeonly %4) {
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
store i32 %3, i32* %6
%12 = bitcast i64* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = bitcast i32* %9 to i8*
%15 = bitcast %struct.sockaddr_in* %10 to i8*
%16 = bitcast %struct.sockaddr_in* %11 to i8*
store i32 0, i32* %4
%17 = call i32 @memset(i8* %15, i32 0, i32 16)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 2
store i32 %18, i32* %19
%20 = trunc i32 %2 to i16
%21 = call i32 @htons(i16 zeroext %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %5
%28 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %114
29: ; preds = %5
%30 = icmp sgt i32 %3, 0
br i1 %30, label %31, label %60
31: ; preds = %29
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_SNDBUF
%34 = bitcast i32* %6 to i8*
%35 = call i32 @setsockopt(i32 %25, i32 %32, i32 %33, i8* %34, i32 4)
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @close(i32 %25)
br label %114
40: ; preds = %31
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_RCVBUF
%43 = call i32 @setsockopt(i32 %25, i32 %41, i32 %42, i8* %34, i32 4)
%44 = icmp eq i32 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @close(i32 %25)
br label %114
48: ; preds = %40
store i32 4, i32* %9
%49 = load i32, i32* @SOL_SOCKET
%50 = load i32, i32* @SO_SNDBUF
%51 = call i32 @getsockopt(i32 %25, i32 %49, i32 %50, i8* %34, i32* %9)
%52 = icmp eq i32 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %48
%54 = call i32 @close(i32 %25)
%55 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %114
56: ; preds = %48
%57 = load i32, i32* %6
%58 = sdiv i32 %57, 1024
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %56, %29
%61 = icmp eq i32 %0, 0
br i1 %61, label %91, label %62
62: ; preds = %60
%63 = load i32, i32* @INADDR_ANY
%64 = call i64 @htonl(i32 %63)
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 0, i32 0
store i64 %64, i64* %65
%66 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%67 = call i32 @bind(i32 %25, %struct.sockaddr* %66, i32 16)
%68 = icmp eq i32 %67, 0
br i1 %68, label %72, label %69
69: ; preds = %62
%70 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%71 = call i32 @close(i32 %25)
br label %114
72: ; preds = %62
%73 = call i32 @listen(i32 %25, i32 1)
%74 = icmp eq i32 %73, 0
br i1 %74, label %78, label %75
75: ; preds = %72
%76 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%77 = call i32 @close(i32 %25)
br label %114
78: ; preds = %72
%79 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
store i32 16, i32* %8
%80 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%81 = call i32 @accept(i32 %25, %struct.sockaddr* %80, i32* %8)
store i32 %81, i32* %4
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %86
83: ; preds = %78
%84 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%85 = call i32 @close(i32 %25)
br label %114
86: ; preds = %78
%87 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i64 0, i32 0, i32 0
%88 = load i64, i64* %87
%89 = call i8* @inet_ntoa(i64 %88)
%90 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i8* %89)
br label %114
91: ; preds = %60
%92 = call %struct.hostent* @gethostbyname(i8* %1)
%93 = icmp eq %struct.hostent* %92, null
br i1 %93, label %94, label %98
94: ; preds = %91
%95 = load i32, i32* @stderr
%96 = call i32 @fprintf(i32 %95, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i8* %1)
%97 = call i32 @close(i32 %25)
br label %114
98: ; preds = %91
%99 = getelementptr inbounds %struct.hostent, %struct.hostent* %92, i64 0, i32 0
%100 = load i64, i64* %99
%101 = inttoptr i64 %100 to i8*
%102 = call i32 @memcpy(i64* %7, i8* %101, i32 8)
%103 = load i64, i64* %7
%104 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i64 0, i32 0, i32 0
store i64 %103, i64* %104
%105 = call i8* @inet_ntoa(i64 %103)
%106 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8* %105)
%107 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%108 = call i32 @connect(i32 %25, %struct.sockaddr* %107, i32 16)
%109 = icmp eq i32 %108, 0
br i1 %109, label %113, label %110
110: ; preds = %98
%111 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.11, i64 0, i64 0))
%112 = call i32 @close(i32 %25)
br label %114
113: ; preds = %98
store i32 %25, i32* %4
br label %114
114: ; preds = %86, %113, %110, %94, %83, %75, %69, %53, %45, %37, %27
%115 = phi i32 [ 1, %27 ], [ 1, %37 ], [ 1, %45 ], [ 1, %53 ], [ 1, %69 ], [ 1, %75 ], [ 1, %83 ], [ 1, %94 ], [ 1, %110 ], [ 0, %113 ], [ 0, %86 ]
ret i32 %115
}