#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;
};
struct hostent {
  int dummy;
};
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ wait_for_work(int);
int main(int argc, char *argv[]) {
  struct sockaddr_in address;
  int socket_fd, connection_fd;
  size_t address_length;
  pid_t child;
  struct hostent *server;
  if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("ERROR: socket() failed\n");
    exit(1);
  }
  bzero((char *)&address, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_port = htons(12340);
  address.sin_addr.s_addr = INADDR_ANY;
  if (bind(socket_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
    printf("ERROR: bind() failed");
    exit(2);
  }
  if (listen(socket_fd, 5) != 0) {
    printf("ERROR: listen() failed\n");
    exit(3);
  }
  wait_for_work(socket_fd);
  close(socket_fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11e1 <main+0x51>
lea    0xe2e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x3034,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1245 <main+0xb5>
lea    0xde2(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
je     1272 <main+0xe2>
lea    0xdca(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%edi
call   10a0 <wait_for_work@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e38(%rip),%r15
mov    (%r15),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1239 <main+0x99>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0x3034,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     124f <main+0xaf>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
test   %rax,%rax
jne    1265 <main+0xc5>
mov    %ebx,%edi
call   10b0 <wait_for_work@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdec(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdaa(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    0xda9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"ERROR: socket() failed\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [21 x i8] c"ERROR: bind() failed\00"
@.str.2 = [24 x i8] c"ERROR: listen() failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = bitcast %struct.sockaddr_in* %6 to i8*
%21 = call i32 @bzero(i8* %20, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 12340)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %7
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %19
%34 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 2)
unreachable
36: ; preds = %19
%37 = load i32, i32* %7
%38 = call i64 @listen(i32 %37, i32 5)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 3)
unreachable
43: ; preds = %36
%44 = load i32, i32* %7
%45 = call i32 @wait_for_work(i32 %44)
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [21 x i8] c"ERROR: bind() failed\00"
@str = [23 x i8] c"ERROR: listen() failed\00"
@str.3 = [23 x i8] c"ERROR: socket() failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.3, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = call i32 @bzero(i8* %4, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 12340)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i64 @bind(i32 %7, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %12
%27 = call i64 @listen(i32 %7, i32 5)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%31 = call i32 @exit(i32 3)
unreachable
32: ; preds = %26
%33 = call i32 @wait_for_work(i32 %7)
%34 = call i32 @close(i32 %7)
ret i32 0
}