#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;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int fd, r;
  struct sockaddr_in sa;
  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (fd < 0) {
    printf("socket failed\n");
    return -1;
  }
  printf("socket %d\n", fd);
  memset(&sa, 0, sizeof(sa));
  sa.sin_addr.s_addr = inet_addr("127.0.0.1");
  sa.sin_family = AF_INET;
  sa.sin_port = htons(80);
  r = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
  if (!r) {
    printf("connect failed\n");
    return -2;
  }
  sleep(60);
  close(fd);
  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    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d6 <main+0x56>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <main+0xf0>
mov    -0x8(%rbp),%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    0xe1c(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x10(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x50,%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1257 <main+0xd7>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1270 <main+0xf0>
mov    $0x3c,%edi
call   1090 <sleep@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e47(%rip),%r15
mov    (%r15),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1232 <main+0xa2>
mov    %eax,%ebp
lea    0xe38(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
lea    0xe1d(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %eax,%eax
je     1245 <main+0xb5>
mov    $0x3c,%edi
call   10a0 <sleep@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1256 <main+0xc6>
lea    0xdeb(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1256 <main+0xc6>
lea    0xdc9(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [15 x i8] c"socket failed\0A\00"
@.str.1 = [11 x i8] c"socket %d\0A\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [16 x i8] c"connect failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
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 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %35
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%17 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %20, i32* %21
%22 = call i32 @htons(i32 80)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i32 @connect(i32 %24, %struct.sockaddr* %25, i32 12)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %13
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
store i32 -2, i32* %1
br label %35
31: ; preds = %13
%32 = call i32 @sleep(i32 60)
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
store i32 0, i32* %1
br label %35
35: ; preds = %31, %29, %11
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [11 x i8] c"socket %d\0A\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@str = [15 x i8] c"connect failed\00"
@str.4 = [14 x i8] c"socket failed\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 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0))
br label %27
10: ; preds = %0
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%12 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%13 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @htons(i32 80)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%20 = call i32 @connect(i32 %6, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %27
24: ; preds = %10
%25 = call i32 @sleep(i32 60)
%26 = call i32 @close(i32 %6)
br label %27
27: ; preds = %24, %22, %8
%28 = phi i32 [ -1, %8 ], [ 0, %24 ], [ -2, %22 ]
ret i32 %28
}