#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ set_cloexec(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
void serve(int sockfd) {
  int child_fd;
  FILE *fp;
  char buf[128];
  set_cloexec(sockfd);
  for (;;) {
    if ((child_fd = accept(sockfd, NULL, NULL)) < 0) {
      syslog(LOG_ERR, "ruptimed: accept error: %s", strerror(errno));
      exit(1);
    }
    set_cloexec(child_fd);
    if ((fp = popen("/usr/bin/uptime", "r")) == NULL) {
      sprintf(buf, "error: %s\n", strerror(errno));
      send(child_fd, buf, strlen(buf), 0);
    } else {
      while (fgets(buf, 128, fp) != NULL) {
        printf("response\n");
        send(child_fd, buf, strlen(buf), 0);
      }
      pclose(fp);
    }
    close(child_fd);
  }
}


x86 assembly (clang, -O0):
<serve>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <set_cloexec@plt>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10c0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    123d <serve+0x6d>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x94(%rbp),%edi
mov    %rax,%rdx
lea    0xdd2(%rip),%rsi
call   10a0 <syslog@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1030 <set_cloexec@plt>
lea    0xdcf(%rip),%rdi
lea    0xdd8(%rip),%rsi
call   10b0 <popen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    12d6 <serve+0x106>
lea    -0x90(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0xb0(%rbp),%rdi
mov    %rax,%rdx
lea    0xd9a(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xa4(%rbp),%edi
mov    -0xa0(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
jmp    134d <serve+0x17d>
jmp    12db <serve+0x10b>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     1344 <serve+0x174>
lea    0xd37(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xbc(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0x90(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xbc(%rbp),%edi
mov    -0xb8(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
jmp    12db <serve+0x10b>
mov    -0x10(%rbp),%rdi
call   1070 <pclose@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    11e6 <serve+0x16>

x86 assembly (clang, -O2):
<serve>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %edi,%r13d
call   1040 <set_cloexec@plt>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
test   %eax,%eax
jns    122b <serve+0x5b>
mov    0x2dd0(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xde9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10a0 <syslog@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %eax,%ebp
lea    0xdf7(%rip),%r15
mov    %rsp,%rbx
lea    0xdfa(%rip),%r12
jmp    1261 <serve+0x91>
mov    %r14,%rdi
call   1070 <pclose@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11f9 <serve+0x29>
mov    %ebp,%edi
call   1040 <set_cloexec@plt>
lea    0xdac(%rip),%rdi
mov    %r15,%rsi
call   10b0 <popen@plt>
test   %rax,%rax
je     12c0 <serve+0xf0>
mov    %rax,%r14
nop
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1240 <serve+0x70>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    1280 <serve+0xb0>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rbx,%rdi
lea    0xd55(%rip),%rsi
mov    %rax,%rdx
call   10d0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    1248 <serve+0x78>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [27 x i8] c"ruptimed: accept error: %s\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"/usr/bin/uptime\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"error: %s\0A\00"
@.str.4 = [10 x i8] c"response\0A\00"
define void @serve(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [128 x i8]
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @set_cloexec(i32 %6)
br label %8
8: ; preds = %49, %1
%9 = load i32, i32* %2
%10 = call i32 @accept(i32 %9, i32* null, i32* null)
store i32 %10, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = load i32, i32* @LOG_ERR
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @syslog(i32 %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %8
%19 = load i32, i32* %3
%20 = call i32 @set_cloexec(i32 %19)
%21 = call i32* @popen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %4
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %33
23: ; preds = %18
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %3
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @send(i32 %28, i8* %29, i32 %31, i32 0)
br label %49
33: ; preds = %18
br label %34
34: ; preds = %39, %33
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%36 = load i32*, i32** %4
%37 = call i32* @fgets(i8* %35, i32 128, i32* %36)
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %46
39: ; preds = %34
%40 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%41 = load i32, i32* %3
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @send(i32 %41, i8* %42, i32 %44, i32 0)
br label %34
46: ; preds = %34
%47 = load i32*, i32** %4
%48 = call i32 @pclose(i32* %47)
br label %49
49: ; preds = %46, %23
%50 = load i32, i32* %3
%51 = call i32 @close(i32 %50)
br label %8
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [27 x i8] c"ruptimed: accept error: %s\00"
@errno = global i32 0
@.str.1 = [16 x i8] c"/usr/bin/uptime\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"error: %s\0A\00"
@str = [9 x i8] c"response\00"
define void @serve(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @set_cloexec(i32 %0)
%5 = call i32 @accept(i32 %0, i32* null, i32* null)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %35, %1
%8 = load i32, i32* @LOG_ERR
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @syslog(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1, %35
%14 = phi i32 [ %37, %35 ], [ %5, %1 ]
%15 = call i32 @set_cloexec(i32 %14)
%16 = call i32* @popen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = call i32* @fgets(i8* %3, i32 128, i32* %16)
%20 = icmp eq i32* %19, null
br i1 %20, label %33, label %27
21: ; preds = %13
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = call i32 @strlen(i8* %3)
%26 = call i32 @send(i32 %14, i8* %3, i32 %25, i32 0)
br label %35
27: ; preds = %18, %27
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%29 = call i32 @strlen(i8* %3)
%30 = call i32 @send(i32 %14, i8* %3, i32 %29, i32 0)
%31 = call i32* @fgets(i8* %3, i32 128, i32* %16)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %27
33: ; preds = %27, %18
%34 = call i32 @pclose(i32* %16)
br label %35
35: ; preds = %33, %21
%36 = call i32 @close(i32 %14)
%37 = call i32 @accept(i32 %0, i32* null, i32* null)
%38 = icmp slt i32 %37, 0
br i1 %38, label %7, label %13
}