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

/* Type definitions */
struct sockaddr_in {
  int sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int listen(int, int);
int /*<<< orphan*/ printf(char *, int, int, char *);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int);
int main(int argc, char **argv) {
  int fd;
  int ret;
  long flags;
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = 12348;
  fd = socket(PF_INET, SOCK_STREAM, 0);
  ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (ret < 0) {
    printf("%d errno %d %s\n", 38, errno, strerror(errno));
  }
  ret = listen(fd, 1);
  if (ret < 0) {
    printf("%d errno %d %s\n", 43, errno, strerror(errno));
  }
  sleep(2);
  close(fd);
  sleep(2);
  fd = socket(PF_INET, SOCK_STREAM, 0);
  ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (ret < 0) {
    printf("%d errno %d %s\n", 56, errno, strerror(errno));
  }
  sleep(2000000000);
  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    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0x303c,-0x28(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1204 <main+0x94>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edx
mov    %rax,%rcx
lea    0xe06(%rip),%rdi
mov    $0x26,%esi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <listen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    124f <main+0xdf>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%edx
mov    %rax,%rcx
lea    0xdbb(%rip),%rdi
mov    $0x2b,%esi
call   1030 <printf@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12d6 <main+0x166>
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xd34(%rip),%rdi
mov    $0x38,%esi
call   1030 <printf@plt>
mov    $0x77359400,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
movl   $0x303c,(%rsp)
mov    0x2e36(%rip),%r14
mov    (%r14),%edi
mov    0x2e3c(%rip),%r15
mov    (%r15),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
test   %eax,%eax
jns    11e1 <main+0x71>
mov    0x2e0e(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xe2e(%rip),%rdi
mov    $0x26,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1030 <printf@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <listen@plt>
test   %eax,%eax
jns    1217 <main+0xa7>
mov    0x2dd8(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xdf8(%rip),%rdi
mov    $0x2b,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1030 <printf@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    (%r14),%edi
mov    (%r15),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
test   %eax,%eax
jns    1278 <main+0x108>
mov    0x2d77(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xd97(%rip),%rdi
mov    $0x38,%esi
mov    %ebx,%edx
mov    %rax,%rcx
call   1030 <printf@plt>
mov    $0x77359400,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"%d errno %d %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 12348, i32* %12
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%18 = call i32 @bind(i32 %16, %struct.sockaddr* %17, i32 8)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %2
%22 = load i32, i32* @errno
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 38, i32 %22, i8* %24)
br label %26
26: ; preds = %21, %2
%27 = load i32, i32* %6
%28 = call i32 @listen(i32 %27, i32 1)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i32, i32* @errno
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 43, i32 %32, i8* %34)
br label %36
36: ; preds = %31, %26
%37 = call i32 @sleep(i32 2)
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = call i32 @sleep(i32 2)
%41 = load i32, i32* @PF_INET
%42 = load i32, i32* @SOCK_STREAM
%43 = call i32 @socket(i32 %41, i32 %42, i32 0)
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%46 = call i32 @bind(i32 %44, %struct.sockaddr* %45, i32 8)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %36
%50 = load i32, i32* @errno
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 56, i32 %50, i8* %52)
br label %54
54: ; preds = %49, %36
%55 = call i32 @sleep(i32 2000000000)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [16 x i8] c"%d errno %d %s\0A\00"
@errno = global i32 0
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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 12348, i32* %7
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%12 = call i32 @bind(i32 %10, %struct.sockaddr* %11, i32 8)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 38, i32 %15, i8* %16)
br label %18
18: ; preds = %14, %2
%19 = call i32 @listen(i32 %10, i32 1)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 43, i32 %22, i8* %23)
br label %25
25: ; preds = %21, %18
%26 = call i32 @sleep(i32 2)
%27 = call i32 @close(i32 %10)
%28 = call i32 @sleep(i32 2)
%29 = load i32, i32* @PF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
%32 = call i32 @bind(i32 %31, %struct.sockaddr* %11, i32 8)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %25
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 56, i32 %35, i8* %36)
br label %38
38: ; preds = %34, %25
%39 = call i32 @sleep(i32 2000000000)
ret i32 0
}