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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to(char *addr, int port) {
  int s;
  struct sockaddr_in a;
  printf("connecting to %s:%d\n", addr, port);
again:
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s == -1) {
    perror("socket");
    exit(1);
  }
  a.sin_family = AF_INET;
  a.sin_port = htons(port);
  a.sin_addr.s_addr = inet_addr(addr);
  if (connect(s, (struct sockaddr *)&a, sizeof(a)) == -1) {
    perror("connect");
    close(s);
    printf("trying again in 1s\n");
    sleep(1);
    goto again;
  }
  return s;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11f0 <connect_to+0x60>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    125e <connect_to+0xce>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    11b4 <connect_to+0x24>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r13d
mov    %rdi,%rbx
lea    0xe45(%rip),%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e19(%rip),%r15
mov    (%r15),%edi
mov    0x2dff(%rip),%r12
mov    (%r12),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1261 <connect_to+0xc1>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %r13d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1277 <connect_to+0xd7>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xde7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    (%r15),%edi
mov    (%r12),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11f0 <connect_to+0x50>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"connecting to %s:%d\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [20 x i8] c"trying again in 1s\0A\00"
define i32 @connect_to(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %8)
br label %10
10: ; preds = %33, %2
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* %4
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i8*, i8** %3
%26 = call i32 @inet_addr(i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %5
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %39
33: ; preds = %19
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @sleep(i32 1)
br label %10
39: ; preds = %19
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"connecting to %s:%d\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@str = [19 x i8] c"trying again in 1s\00"
define i32 @connect_to(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%14 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %18
15: ; preds = %25, %2
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10, %25
%19 = phi i32 [ %8, %10 ], [ %32, %25 ]
%20 = load i32, i32* @AF_INET
store i32 %20, i32* %11
%21 = call i32 @htons(i32 %1)
store i32 %21, i32* %12
%22 = call i32 @inet_addr(i8* %0)
store i32 %22, i32* %13
%23 = call i32 @connect(i32 %19, %struct.sockaddr* %14, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %34
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @close(i32 %19)
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%29 = call i32 @sleep(i32 1)
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %15, label %18
34: ; preds = %18
ret i32 %19
}