#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_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int sapi_set_max_conn(int conn) {
  int fd;
  int ret;
  char buf[10];
  fd = open("/proc/sys/net/core/somaxconn", O_WRONLY);
  if (fd < 0)
    return -1;
  sprintf(buf, "%d\n", conn);
  ret = write(fd, buf, strlen(buf));
  close(fd);
  return ret > 0 ? 0 : -1;
}


x86 assembly (clang, -O0):
<sapi_set_max_conn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1189 <sapi_set_max_conn+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <sapi_set_max_conn+0x8f>
lea    -0x1a(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe86(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x1a(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x1a(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovg  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sapi_set_max_conn>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0xe96(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11b3 <sapi_set_max_conn+0x63>
mov    %eax,%ebx
lea    0xea1(%rip),%rsi
lea    0x6(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
test   %ebp,%ebp
setle  %al
neg    %eax
jmp    11b8 <sapi_set_max_conn+0x68>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"/proc/sys/net/core/somaxconn\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @sapi_set_max_conn(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [10 x i8]
store i32 %0, i32* %3
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %27
12: ; preds = %1
%13 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%14 = load i32, i32* %3
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %4
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @write(i32 %16, i8* %17, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %5
%24 = icmp sgt i32 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i32 0, i32 -1
store i32 %26, i32* %2
br label %27
27: ; preds = %12, %11
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@.str = [29 x i8] c"/proc/sys/net/core/somaxconn\00"
@O_WRONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @sapi_set_max_conn(i32 %0) {
%2 = alloca [10 x i8]
%3 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %1
%8 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%9 = call i32 @strlen(i8* %3)
%10 = call i32 @write(i32 %5, i8* %3, i32 %9)
%11 = call i32 @close(i32 %5)
%12 = icmp slt i32 %10, 1
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %1, %7
%15 = phi i32 [ %13, %7 ], [ -1, %1 ]
ret i32 %15
}