#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int soc;
  struct sockaddr_in addr;
  soc = socket(AF_INET, SOCK_STREAM, 0);
  if (soc == -1) {
    printf("socket error!\n");
    return -1;
  }
  printf("soc = %d\n", soc);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(3000);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(soc, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    printf("bind error!\n");
    close(soc);
    return -1;
  }
  printf("bind success.\n");
  if (listen(soc, 5) == -1) {
    printf("listen error!\n");
    close(soc);
    return -1;
  }
  printf("listen success.\n");
  close(soc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11af <main+0x4f>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <main+0x124>
mov    -0x8(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0xbb8,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1221 <main+0xc1>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <main+0x124>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1267 <main+0x107>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <main+0x124>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e6a(%rip),%r14
mov    (%r14),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1214 <main+0xa4>
mov    %eax,%ebx
lea    0xe61(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    $0xbb8,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1222 <main+0xb2>
lea    0xe23(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     122b <main+0xbb>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    1243 <main+0xd3>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
jmp    123e <main+0xce>
lea    0xe0d(%rip),%rdi
jmp    1232 <main+0xc2>
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
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
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"socket error!\0A\00"
@.str.1 = [10 x i8] c"soc = %d\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [13 x i8] c"bind error!\0A\00"
@.str.3 = [15 x i8] c"bind success.\0A\00"
@.str.4 = [15 x i8] c"listen error!\0A\00"
@.str.5 = [17 x i8] c"listen success.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %42
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 3000)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* %2
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i32 @bind(i32 %21, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %11
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %1
br label %42
29: ; preds = %11
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* %2
%32 = call i32 @listen(i32 %31, i32 5)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* %2
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %1
br label %42
38: ; preds = %29
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
store i32 0, i32* %1
br label %42
42: ; preds = %38, %34, %25, %9
%43 = load i32, i32* %1
ret i32 %43
}

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
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"soc = %d\0A\00"
@INADDR_ANY = global i32 0
@str = [14 x i8] c"bind success.\00"
@str.6 = [16 x i8] c"listen success.\00"
@str.7 = [14 x i8] c"listen error!\00"
@str.8 = [12 x i8] c"bind error!\00"
@str.9 = [14 x i8] c"socket error!\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 eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
br label %33
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%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 = tail call i32 @htons(i32 3000)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i32 @bind(i32 %5, %struct.sockaddr* %17, i32 12)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %9
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0))
%22 = call i32 @close(i32 %5)
br label %33
23: ; preds = %9
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%25 = call i32 @listen(i32 %5, i32 5)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
%29 = call i32 @close(i32 %5)
br label %33
30: ; preds = %23
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0))
%32 = call i32 @close(i32 %5)
br label %33
33: ; preds = %30, %27, %20, %7
%34 = phi i32 [ -1, %7 ], [ -1, %20 ], [ -1, %27 ], [ 0, %30 ]
ret i32 %34
}