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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int PATH_MAX;
int safe_open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int safe_read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int accept_client(const char listen_path[]) {
  char new_session[PATH_MAX], client_data[PATH_MAX];
  int listen = safe_open(listen_path, O_RDONLY, 0);
  int size = safe_read(listen, new_session, sizeof new_session);
  new_session[size - 1] = '\0';
  snprintf(client_data, sizeof client_data, "/net/tcp/%s/data", new_session);
  return safe_open(client_data, O_RDWR, 0);
}


x86 assembly (clang, -O0):
<accept_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%ecx
mov    %rcx,-0x48(%rbp)
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x40(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x18(%rbp)
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <safe_open@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    %eax,%edx
call   1040 <safe_read@plt>
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
mov    -0x38(%rbp),%rax
mov    %edx,-0x28(%rbp)
mov    -0x28(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
movb   $0x0,(%rcx,%rdx,1)
mov    %eax,%esi
lea    0xe20(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <safe_open@plt>
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_client>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2eaf(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <safe_open@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <safe_read@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe74(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %edx,%edx
call   1050 <safe_open@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@O_RDONLY = global i32 0
@.str = [17 x i8] c"/net/tcp/%s/data\00"
@O_RDWR = global i32 0
define i32 @accept_client(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %3
%11 = alloca i8, i64 %9
store i64 %9, i64* %4
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = alloca i8, i64 %13
store i64 %13, i64* %5
%15 = load i8*, i8** %2
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @safe_open(i8* %15, i32 %16, i32 0)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = trunc i64 %9 to i32
%20 = call i32 @safe_read(i32 %18, i8* %11, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %11, i64 %23
store i8 0, i8* %24
%25 = trunc i64 %13 to i32
%26 = call i32 @snprintf(i8* %14, i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %11)
%27 = load i32, i32* @O_RDWR
%28 = call i32 @safe_open(i8* %14, i32 %27, i32 0)
%29 = load i8*, i8** %3
ret i32 %28
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@O_RDONLY = global i32 0
@.str = [17 x i8] c"/net/tcp/%s/data\00"
@O_RDWR = global i32 0
define i32 @accept_client(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = alloca i8, i64 %3
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @safe_open(i8* %0, i32 %6, i32 0)
%8 = call i32 @safe_read(i32 %7, i8* %4, i32 %2)
%9 = add nsw i32 %8, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %4, i64 %10
store i8 0, i8* %11
%12 = call i32 @snprintf(i8* %5, i32 %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %4)
%13 = load i32, i32* @O_RDWR
%14 = call i32 @safe_open(i8* %5, i32 %13, i32 0)
ret i32 %14
}