#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*/ val;
struct TYPE_2__ {
  int 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*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int fd;
  int val;
  int ret;
  struct sockaddr_in addr;
  fd = socket(PF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    perror("socket");
  val = 1;
  (void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(80);
  addr.sin_addr.s_addr = 0x7f000001;
  ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (ret < 0)
    perror("connect");
  ret = close(fd);
  if (ret)
    perror("close");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a1 <main+0x41>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x7f000001,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1216 <main+0xb6>
lea    0xdf6(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1237 <main+0xd7>
lea    0xddd(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fb <main+0x9b>
movl   $0x1,0xc(%rsp)
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
movl   $0x7f000001,0x10(%rsp)
7f
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %eax,%eax
js     120c <main+0xac>
mov    %ebx,%edi
call   1050 <close@plt>
test   %eax,%eax
jne    121a <main+0xba>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdfe(%rip),%rdi
call   1060 <perror@plt>
jmp    1184 <main+0x24>
lea    0xdf4(%rip),%rdi
call   1060 <perror@plt>
jmp    11e8 <main+0x88>
lea    0xdee(%rip),%rdi
call   1060 <perror@plt>
jmp    11f3 <main+0x93>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_INET = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
store i32 1, i32* %3
%14 = load i32, i32* %2
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %3, i32 4)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 80)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 2130706433, i32* %23
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%26 = call i32 @connect(i32 %24, %struct.sockaddr* %25, i32 12)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %13
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %13
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %31
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_INET = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @PF_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 %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
store i32 1, i32* %1
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_REUSEADDR
%14 = call i32 @setsockopt(i32 %7, i32 %12, i32 %13, i32* %1, i32 4)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 80)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 2130706433, i32* %19
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%21 = call i32 @connect(i32 %7, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %11
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %11
%26 = call i32 @close(i32 %7)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
ret i32 0
}