#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__ {
  void *s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  TYPE_1__ sin_addr;
  void *sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ connection;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(int /*<<< orphan*/, int);
void *htons(int);
int /*<<< orphan*/ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int abre_conexao(int *pporta) {
  struct sockaddr_in connection;
  int sock, resultado;
  int porta = 1024;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  do {
    porta++;
    connection.sin_family = AF_INET;
    connection.sin_port = htons(porta);
    connection.sin_addr.s_addr = htons(INADDR_ANY);
    bzero(connection.sin_zero, 8);
    resultado = bind(sock, (struct sockaddr *)&connection, sizeof(connection));
  } while (resultado != 0);
  listen(sock, 2);
  *pporta = porta;
  return sock;
}


x86 assembly (clang, -O0):
<abre_conexao>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x400,-0x34(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x20,%edx
call   1050 <bind@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    117f <abre_conexao+0x2f>
mov    -0x2c(%rbp),%edi
mov    $0x2,%esi
call   1040 <listen@plt>
mov    -0x34(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<abre_conexao>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    0x2e80(%rip),%r12
mov    (%r12),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
mov    $0x400,%ebx
mov    0x2e56(%rip),%r13
lea    0x8(%rsp),%r15
nop
add    $0x1,%ebx
mov    (%r12),%eax
mov    %eax,0x20(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %rax,0x18(%rsp)
mov    0x0(%r13),%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%edi
mov    $0x8,%esi
call   1060 <bzero@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x20,%edx
call   1050 <bind@plt>
test   %eax,%eax
jne    1190 <abre_conexao+0x40>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <listen@plt>
mov    %ebx,(%r14)
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i8*, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
define i32 @abre_conexao(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %2
store i32 1024, i32* %6
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
br label %10
10: ; preds = %28, %1
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1
store i32 %12, i32* %6
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* %6
%16 = call i8* @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i8* %16, i8** %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i8* @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i8* %19, i8** %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @bzero(i32 %23, i32 8)
%25 = load i32, i32* %4
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 32)
store i32 %27, i32* %5
br label %28
28: ; preds = %10
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 0
br i1 %30, label %10, label %31
31: ; preds = %28
%32 = load i32, i32* %4
%33 = call i32 @listen(i32 %32, i32 2)
%34 = load i32, i32* %6
%35 = load i32*, i32** %2
store i32 %34, i32* %35
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i8*, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
define i32 @abre_conexao(i32* nocapture writeonly %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%11 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %12
12: ; preds = %12, %1
%13 = phi i32 [ 1024, %1 ], [ %14, %12 ]
%14 = add nuw nsw i32 %13, 1
%15 = load i32, i32* @AF_INET
store i32 %15, i32* %7
%16 = call i8* @htons(i32 %14)
store i8* %16, i8** %8
%17 = load i32, i32* @INADDR_ANY
%18 = call i8* @htons(i32 %17)
store i8* %18, i8** %9
%19 = load i32, i32* %10
%20 = call i32 @bzero(i32 %19, i32 8)
%21 = call i32 @bind(i32 %6, %struct.sockaddr* %11, i32 32)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %12
23: ; preds = %12
%24 = call i32 @listen(i32 %6, i32 2)
store i32 %14, i32* %0
ret i32 %6
}