#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
scalar_t__ EINTR;
int accept(int, struct sockaddr *, int *);
scalar_t__ errno;
int /*<<< orphan*/ ltr_int_my_perror(char *);
int /*<<< orphan*/ printf(char *);
int accept_connection(int socket) {
  int cfd;
  struct sockaddr_in client;
  socklen_t client_len = sizeof(struct sockaddr);
  printf("Going to wait!\n");
  while (1) {
    cfd = accept(socket, (struct sockaddr *)&client, &client_len);
    if (cfd == -1) {
      if (errno == EINTR) {
        continue;
      } else {
        ltr_int_my_perror("accept");
        return -1;
      }
    } else {
      break;
    }
  }
  return cfd;
}


x86 assembly (clang, -O0):
<accept_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x14(%rbp)
lea    0xeb7(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11a2 <accept_connection+0x72>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <accept_connection+0x5a>
jmp    114e <accept_connection+0x1e>
lea    0xe7f(%rip),%rdi
call   1040 <ltr_int_my_perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <accept_connection+0x7d>
jmp    11a7 <accept_connection+0x77>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0x4,0xc(%rsp)
lea    0xeb8(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
mov    0x2e7b(%rip),%r12
mov    0x2e6c(%rip),%r13
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    119a <accept_connection+0x6a>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1170 <accept_connection+0x40>
lea    0xe6b(%rip),%rdi
call   1040 <ltr_int_my_perror@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 { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"Going to wait!\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [7 x i8] c"accept\00"
define i32 @accept_connection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i32 %0, i32* %3
store i32 4, i32* %6
%7 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %1, %18
%9 = load i32, i32* %3
%10 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %6)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %21
14: ; preds = %8
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
br label %8
19: ; preds = %14
%20 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
21: ; preds = %8
br label %22
22: ; preds = %21
%23 = load i32, i32* %4
store i32 %23, i32* %2
br label %24
24: ; preds = %22, %19
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [7 x i8] c"accept\00"
@str = [15 x i8] c"Going to wait!\00"
define i32 @accept_connection(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%7 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %8
8: ; preds = %11, %1
%9 = call i32 @accept(i32 %0, %struct.sockaddr* %7, i32* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %8, label %15
15: ; preds = %11
%16 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %8, %15
ret i32 %9
}