#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 */
char *calloc(int, size_t);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int sprintf(char *, char *, ...);
int /*<<< orphan*/ stderr;
char *create_listen_fds_env(int *listen_fds, int worker_count) {
  size_t buf_size = sizeof("listen_fds=") + worker_count * 5 + worker_count - 1;
  char *buf = calloc(1, buf_size);
  if (buf == NULL) {
    fprintf(stderr, "failed to allocated mem for listen fds, aborting ...");
    exit(1);
  }
  char *cur = buf;
  cur += sprintf(cur, "listen_fds=");
  for (int i = 0; i < worker_count - 1; i++) {
    int plus = sprintf(cur, "%d ", listen_fds[i]);
    cur = cur + plus;
  }
  cur += sprintf(cur, "%d", listen_fds[worker_count - 1]);
  return buf;
}


x86 assembly (clang, -O0):
<create_listen_fds_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
imul   $0x5,-0xc(%rbp),%eax
cltq
add    $0xc,%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    $0x1,%edi
call   1030 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a4 <create_listen_fds_env+0x64>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe7e(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1223 <create_listen_fds_env+0xe3>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d5 <create_listen_fds_env+0x95>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_listen_fds_env>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %rdi,%r15
lea    (%r12,%r12,4),%eax
cltq
movslq %esi,%rcx
lea    (%rcx,%rax,1),%rsi
add    $0xb,%rsi
mov    $0x1,%edi
call   1030 <calloc@plt>
test   %rax,%rax
je     1201 <create_listen_fds_env+0xc1>
mov    %rax,%r14
movabs $0x665f6e657473696c,%rax
6e 5f 66
mov    %rax,(%r14)
movl   $0x3d7364,0x8(%r14)
mov    %r14,%rbx
add    $0xb,%rbx
lea    -0x1(%r12),%eax
cmp    $0x2,%r12d
mov    %eax,0x4(%rsp)
jl     11d5 <create_listen_fds_env+0x95>
mov    %eax,%ebp
lea    0xe95(%rip),%r12
xor    %r13d,%r13d
nop
mov    (%r15,%r13,4),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
movslq %eax,%rdi
add    %rbx,%rdi
add    $0x1,%r13
mov    %rdi,%rbx
cmp    %r13,%rbp
jne    11b0 <create_listen_fds_env+0x70>
jmp    11d8 <create_listen_fds_env+0x98>
mov    %rbx,%rdi
movslq 0x4(%rsp),%rax
mov    (%r15,%rax,4),%edx
lea    0xe5d(%rip),%rsi
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [53 x i8] c"failed to allocated mem for listen fds, aborting ...\00"
@.str.1 = [12 x i8] c"listen_fds=\00"
@.str.2 = [4 x i8] c"%d \00"
@.str.3 = [3 x i8] c"%d\00"
define i8* @create_listen_fds_env(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = mul nsw i32 %10, 5
%12 = sext i32 %11 to i64
%13 = add i64 12, %12
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = add i64 %13, %15
%17 = sub i64 %16, 1
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = call i8* @calloc(i32 1, i64 %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load i8*, i8** %6
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = call i32 (i8*, i8*, ...) @sprintf(i8* %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%30 = load i8*, i8** %7
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8* %32, i8** %7
store i32 0, i32* %8
br label %33
33: ; preds = %50, %26
%34 = load i32, i32* %8
%35 = load i32, i32* %4
%36 = sub nsw i32 %35, 1
%37 = icmp slt i32 %34, %36
br i1 %37, label %38, label %53
38: ; preds = %33
%39 = load i8*, i8** %7
%40 = load i32*, i32** %3
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = load i32, i32* %43
%45 = call i32 (i8*, i8*, ...) @sprintf(i8* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %44)
store i32 %45, i32* %9
%46 = load i8*, i8** %7
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
store i8* %49, i8** %7
br label %50
50: ; preds = %38
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
br label %33
53: ; preds = %33
%54 = load i8*, i8** %7
%55 = load i32*, i32** %3
%56 = load i32, i32* %4
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32, i32* %55, i64 %58
%60 = load i32, i32* %59
%61 = call i32 (i8*, i8*, ...) @sprintf(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %60)
%62 = load i8*, i8** %7
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds i8, i8* %62, i64 %63
store i8* %64, i8** %7
%65 = load i8*, i8** %6
ret i8* %65
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [53 x i8] c"failed to allocated mem for listen fds, aborting ...\00"
@.str.1 = [12 x i8] c"listen_fds=\00"
@.str.2 = [4 x i8] c"%d \00"
@.str.3 = [3 x i8] c"%d\00"
define i8* @create_listen_fds_env(i32* nocapture %0, i32 %1) {
%3 = mul nsw i32 %1, 5
%4 = sext i32 %3 to i64
%5 = sext i32 %1 to i64
%6 = add nsw i64 %5, 11
%7 = add nsw i64 %6, %4
%8 = tail call i8* @calloc(i32 1, i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(12) %8, i8* align 1 dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 12, i1 false)
%15 = getelementptr inbounds i8, i8* %8, i64 11
%16 = add i32 %1, -1
%17 = icmp sgt i32 %1, 1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = zext i32 %16 to i64
br label %26
20: ; preds = %26, %14
%21 = phi i8* [ %15, %14 ], [ %33, %26 ]
%22 = sext i32 %16 to i64
%23 = getelementptr inbounds i32, i32* %0, i64 %22
%24 = load i32, i32* %23
%25 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %21, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %24)
ret i8* %8
26: ; preds = %18, %26
%27 = phi i64 [ 0, %18 ], [ %34, %26 ]
%28 = phi i8* [ %15, %18 ], [ %33, %26 ]
%29 = getelementptr inbounds i32, i32* %0, i64 %27
%30 = load i32, i32* %29
%31 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %28, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %28, i64 %32
%34 = add nuw nsw i64 %27, 1
%35 = icmp eq i64 %34, %19
br i1 %35, label %20, label %26
}