#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;
};
typedef int /*<<< orphan*/ servaddr;

/* 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 close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  struct sockaddr_in servaddr;
  int sockfd;
  int i, n;
  int ret = 0;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("socket failed\n");
    return -3;
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(8000);
  ret = bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if (0 != ret) {
    perror("bind failed\n");
    return -1;
  }
  ret = close(sockfd);
  if (0 != ret) {
    perror("close failed\n");
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c4 <main+0x54>
lea    0xe4d(%rip),%rdi
call   1070 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1268 <main+0xf8>
lea    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    $0x1f40,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%eax
je     1233 <main+0xc3>
lea    0xded(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <main+0xf8>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%eax
je     1261 <main+0xf1>
lea    0xdcc(%rip),%rdi
call   1070 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1268 <main+0xf8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%r15
mov    (%r15),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11fb <main+0x8b>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x1f40,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %eax,%eax
jne    120e <main+0x9e>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    1221 <main+0xb1>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
mov    $0xfffffffd,%eax
jmp    11f1 <main+0x81>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11f1 <main+0x81>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
mov    $0xfffffffe,%eax
jmp    11f1 <main+0x81>

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
@.str = [15 x i8] c"socket failed\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"bind failed\0A\00"
@.str.2 = [14 x i8] c"close failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
%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* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -3, i32* %1
br label %39
14: ; preds = %0
%15 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = call i32 @htons(i32 8000)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 0, %27
br i1 %28, label %29, label %31
29: ; preds = %14
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %39
31: ; preds = %14
%32 = load i32, i32* %3
%33 = call i32 @close(i32 %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp ne i32 0, %34
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -2, i32* %1
br label %39
38: ; preds = %31
store i32 0, i32* %1
br label %39
39: ; preds = %38, %36, %29, %12
%40 = load i32, i32* %1
ret i32 %40
}

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
@.str = [15 x i8] c"socket failed\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"bind failed\0A\00"
@.str.2 = [14 x i8] c"close failed\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%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 slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %28
9: ; preds = %0
%10 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = tail call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 8000)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%19 = call i32 @bind(i32 %5, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %9
%22 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %28
23: ; preds = %9
%24 = call i32 @close(i32 %5)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %23, %26, %21, %7
%29 = phi i32 [ -3, %7 ], [ -1, %21 ], [ -2, %26 ], [ 0, %23 ]
ret i32 %29
}