#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int EXIT_FAILURE;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *sockaddr2str(struct sockaddr *);
int srv_accept(int listen_sock) {
  int ret;
  int cli_sock;
  struct sockaddr_storage cli_addr;
  socklen_t cli_addr_len = sizeof(cli_addr);
  char *addrstr;
  cli_sock = accept(listen_sock, (struct sockaddr *)&cli_addr, &cli_addr_len);
  do {
    if (cli_sock == -1) {
      perror("accept");
      ret = EXIT_FAILURE;
      goto error;
    }
  } while (0);
  addrstr = sockaddr2str((struct sockaddr *)&cli_addr);
  do {
    if (!addrstr) {
      perror("OOM");
      ret = EXIT_FAILURE;
      goto close_cli;
    }
  } while (0);
  printf("Client %s is connected\n", addrstr);
  free(addrstr);
  return cli_sock;
close_cli:
  close(cli_sock);
error:
  return -1;
}


x86 assembly (clang, -O0):
<srv_accept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11ac <srv_accept+0x4c>
lea    0xe6a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    1217 <srv_accept+0xb7>
jmp    11b1 <srv_accept+0x51>
lea    -0x18(%rbp),%rdi
call   1060 <sockaddr2str@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e6 <srv_accept+0x86>
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    120f <srv_accept+0xaf>
jmp    11eb <srv_accept+0x8b>
mov    -0x28(%rbp),%rsi
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <srv_accept+0xbe>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<srv_accept>:
push   %rbp
push   %rbx
push   %rax
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1080 <accept@plt>
cmp    $0xffffffff,%eax
je     11af <srv_accept+0x4f>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1060 <sockaddr2str@plt>
test   %rax,%rax
je     11c2 <srv_accept+0x62>
mov    %rax,%rbx
lea    0xe75(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe4a(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11a6 <srv_accept+0x46>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
jmp    11a6 <srv_accept+0x46>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"OOM\00"
@.str.2 = [24 x i8] c"Client %s is connected\0A\00"
define i32 @srv_accept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_storage
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %3
store i32 4, i32* %7
%9 = load i32, i32* %3
%10 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %7)
store i32 %11, i32* %5
br label %12
12: ; preds = %1
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
store i32 %17, i32* %4
br label %38
18: ; preds = %12
br label %19
19: ; preds = %18
%20 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%21 = call i8* @sockaddr2str(%struct.sockaddr* %20)
store i8* %21, i8** %8
br label %22
22: ; preds = %19
%23 = load i8*, i8** %8
%24 = icmp ne i8* %23, null
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %4
br label %35
28: ; preds = %22
br label %29
29: ; preds = %28
%30 = load i8*, i8** %8
%31 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %8
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* %5
store i32 %34, i32* %2
br label %39
35: ; preds = %25
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %15
store i32 -1, i32* %2
br label %39
39: ; preds = %38, %29
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [4 x i8] c"OOM\00"
@.str.2 = [24 x i8] c"Client %s is connected\0A\00"
define i32 @srv_accept(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = bitcast %struct.sockaddr_storage* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%7 = call i32 @accept(i32 %0, %struct.sockaddr* %6, i32* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %20
11: ; preds = %1
%12 = call i8* @sockaddr2str(%struct.sockaddr* %6)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @close(i32 %7)
br label %20
17: ; preds = %11
%18 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%19 = call i32 @free(i8* %12)
br label %20
20: ; preds = %9, %14, %17
%21 = phi i32 [ %7, %17 ], [ -1, %14 ], [ -1, %9 ]
ret i32 %21
}