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

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ env_deserialize(char *, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *);
int fork();
int /*<<< orphan*/ getenv(char *);
int /*<<< orphan*/ http_err(int, int, char *, char const *);
char *http_request_headers(int);
int /*<<< orphan*/ http_serve(int, int /*<<< orphan*/);
int /*<<< orphan*/ http_set_executable_uid_gid(int, int);
scalar_t__ recvfd(int, char *, int, int *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ warnx(char *, int, int);
int main(int argc, char **argv) {
  int fd;
  if (argc != 2 && argc != 4)
    errx(1, "Wrong arguments");
  fd = atoi(argv[1]);
  if (argc == 4) {
    int uid = atoi(argv[2]);
    int gid = atoi(argv[3]);
    warnx("cgi uid %d, gid %d", uid, gid);
    http_set_executable_uid_gid(uid, gid);
  }
  signal(SIGPIPE, SIG_IGN);
  signal(SIGCHLD, SIG_IGN);
  for (;;) {
    char envp[8192];
    int sockfd = -1;
    const char *errmsg;
    if ((recvfd(fd, envp, sizeof(envp), &sockfd) <= 0) || sockfd < 0)
      err(1, "recvfd");
    switch (fork()) {
    case -1:
      err(1, "fork");
    case 0:
      env_deserialize(envp, sizeof(envp));
      if ((errmsg = http_request_headers(sockfd)))
        http_err(sockfd, 500, "http_request_headers: %s", errmsg);
      else
        http_serve(sockfd, getenv("REQUEST_URI"));
      return 0;
    default:
      close(sockfd);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     121e <main+0x3e>
cmpl   $0x4,-0x8(%rbp)
je     121e <main+0x3e>
mov    $0x1,%edi
lea    0xde7(%rip),%rsi
call   1060 <errx@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    1275 <main+0x95>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xdab(%rip),%rdi
call   1070 <warnx@plt>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1080 <http_set_executable_uid_gid@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
movl   $0xffffffff,-0x2024(%rbp)
ff ff ff
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
lea    -0x2024(%rbp),%rcx
call   10c0 <recvfd@plt>
cmp    $0x0,%rax
jle    12df <main+0xff>
cmpl   $0x0,-0x2024(%rbp)
jge    12f0 <main+0x110>
mov    $0x1,%edi
lea    0xd38(%rip),%rsi
call   10b0 <err@plt>
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x2034(%rbp)
sub    $0xffffffff,%eax
je     1320 <main+0x140>
jmp    130d <main+0x12d>
mov    -0x2034(%rbp),%eax
test   %eax,%eax
je     1331 <main+0x151>
jmp    13b1 <main+0x1d1>
mov    $0x1,%edi
lea    0xcfe(%rip),%rsi
call   10b0 <err@plt>
lea    -0x2020(%rbp),%rdi
mov    $0x2000,%esi
call   1030 <env_deserialize@plt>
mov    -0x2024(%rbp),%edi
call   1050 <http_request_headers@plt>
mov    %rax,-0x2030(%rbp)
cmp    $0x0,%rax
je     1381 <main+0x1a1>
mov    -0x2024(%rbp),%edi
mov    -0x2030(%rbp),%rcx
mov    $0x1f4,%esi
lea    0xcb8(%rip),%rdx
call   10f0 <http_err@plt>
jmp    13a6 <main+0x1c6>
mov    -0x2024(%rbp),%eax
mov    %eax,-0x2038(%rbp)
lea    0xcb4(%rip),%rdi
call   1040 <getenv@plt>
mov    -0x2038(%rbp),%edi
mov    %eax,%esi
call   10d0 <http_serve@plt>
xor    %eax,%eax
add    $0x2040,%rsp
pop    %rbp
ret
mov    -0x2024(%rbp),%edi
call   1090 <close@plt>
jmp    12a3 <main+0xc3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2010,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
cmp    $0x2,%edi
je     120f <main+0x2f>
cmp    $0x4,%ebp
je     120f <main+0x2f>
lea    0xdfb(%rip),%rsi
mov    $0x1,%edi
call   1060 <errx@plt>
mov    0x8(%r14),%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebx
cmp    $0x4,%ebp
jne    1251 <main+0x71>
mov    0x10(%r14),%rdi
call   10e0 <atoi@plt>
mov    %eax,%r15d
mov    0x18(%r14),%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebp
lea    0xdd3(%rip),%rdi
mov    %r15d,%esi
mov    %eax,%edx
call   1070 <warnx@plt>
mov    %r15d,%edi
mov    %ebp,%esi
call   1080 <http_set_executable_uid_gid@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    0x2d7f(%rip),%rbp
mov    0x0(%rbp),%esi
call   10a0 <signal@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%esi
call   10a0 <signal@plt>
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xd98(%rip),%r12
nopl   0x0(%rax,%rax,1)
movl   $0xffffffff,0xc(%rsp)
ff
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x2000,%edx
mov    %r15,%rcx
call   10c0 <recvfd@plt>
test   %rax,%rax
jle    12b6 <main+0xd6>
cmpl   $0x0,0xc(%rsp)
jns    12c3 <main+0xe3>
mov    $0x1,%edi
mov    %r12,%rsi
call   10b0 <err@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     12ef <main+0x10f>
cmp    $0xffffffff,%eax
je     12de <main+0xfe>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
jmp    1290 <main+0xb0>
lea    0xd45(%rip),%rsi
mov    $0x1,%edi
call   10b0 <err@plt>
lea    0x10(%rsp),%rdi
mov    $0x2000,%esi
call   1030 <env_deserialize@plt>
mov    0xc(%rsp),%edi
call   1050 <http_request_headers@plt>
mov    0xc(%rsp),%ebx
test   %rax,%rax
je     1328 <main+0x148>
lea    0xd18(%rip),%rdx
mov    %ebx,%edi
mov    $0x1f4,%esi
mov    %rax,%rcx
call   10f0 <http_err@plt>
jmp    133d <main+0x15d>
lea    0xd19(%rip),%rdi
call   1040 <getenv@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   10d0 <http_serve@plt>
xor    %eax,%eax
add    $0x2010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Wrong arguments\00"
@.str.1 = [19 x i8] c"cgi uid %d, gid %d\00"
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@SIGCHLD = global i32 0
@.str.2 = [7 x i8] c"recvfd\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [25 x i8] c"http_request_headers: %s\00"
@.str.5 = [12 x i8] c"REQUEST_URI\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [8192 x i8]
%10 = alloca i32
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 4
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @errx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %14, %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %6
%24 = load i32, i32* %4
%25 = icmp eq i32 %24, 4
br i1 %25, label %26, label %41
26: ; preds = %19
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %7
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 3
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %8
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = call i32 @warnx(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %35, i32 %36)
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = call i32 @http_set_executable_uid_gid(i32 %38, i32 %39)
br label %41
41: ; preds = %26, %19
%42 = load i32, i32* @SIGPIPE
%43 = load i32, i32* @SIG_IGN
%44 = call i32 @signal(i32 %42, i32 %43)
%45 = load i32, i32* @SIGCHLD
%46 = load i32, i32* @SIG_IGN
%47 = call i32 @signal(i32 %45, i32 %46)
br label %48
48: ; preds = %80, %41
store i32 -1, i32* %10
%49 = load i32, i32* %6
%50 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%51 = call i64 @recvfd(i32 %49, i8* %50, i32 8192, i32* %10)
%52 = icmp sle i64 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %48
%54 = load i32, i32* %10
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53, %48
%57 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = call i32 (...) @fork()
switch i32 %59, label %77 [
i32 -1, label %60
i32 0, label %62
]
60: ; preds = %58
%61 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %58, %60
%63 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%64 = call i32 @env_deserialize(i8* %63, i32 8192)
%65 = load i32, i32* %10
%66 = call i8* @http_request_headers(i32 %65)
store i8* %66, i8** %11
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %72
68: ; preds = %62
%69 = load i32, i32* %10
%70 = load i8*, i8** %11
%71 = call i32 @http_err(i32 %69, i32 500, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %70)
br label %76
72: ; preds = %62
%73 = load i32, i32* %10
%74 = call i32 @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%75 = call i32 @http_serve(i32 %73, i32 %74)
br label %76
76: ; preds = %72, %68
ret i32 0
77: ; preds = %58
%78 = load i32, i32* %10
%79 = call i32 @close(i32 %78)
br label %80
80: ; preds = %77
br label %48
}

LLVM IR (-O2):
@.str = [16 x i8] c"Wrong arguments\00"
@.str.1 = [19 x i8] c"cgi uid %d, gid %d\00"
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@SIGCHLD = global i32 0
@.str.2 = [7 x i8] c"recvfd\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [25 x i8] c"http_request_headers: %s\00"
@.str.5 = [12 x i8] c"REQUEST_URI\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = alloca i32
switch i32 %0, label %5 [
i32 4, label %7
i32 2, label %7
]
5: ; preds = %2
%6 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %2, %2, %5
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = icmp eq i32 %0, 4
br i1 %11, label %12, label %21
12: ; preds = %7
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 3
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = tail call i32 @warnx(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %18)
%20 = tail call i32 @http_set_executable_uid_gid(i32 %15, i32 %18)
br label %21
21: ; preds = %12, %7
%22 = load i32, i32* @SIGPIPE
%23 = load i32, i32* @SIG_IGN
%24 = tail call i32 @signal(i32 %22, i32 %23)
%25 = load i32, i32* @SIGCHLD
%26 = load i32, i32* @SIG_IGN
%27 = tail call i32 @signal(i32 %25, i32 %26)
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%29 = bitcast i32* %4 to i8*
br label %30
30: ; preds = %53, %21
store i32 -1, i32* %4
%31 = call i64 @recvfd(i32 %10, i8* %28, i32 8192, i32* %4)
%32 = icmp slt i64 %31, 1
%33 = load i32, i32* %4
%34 = icmp slt i32 %33, 0
%35 = select i1 %32, i1 true, i1 %34
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %30, %36
%39 = call i32 (...) @fork()
switch i32 %39, label %53 [
i32 -1, label %40
i32 0, label %42
]
40: ; preds = %38
%41 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %38, %40
%43 = call i32 @env_deserialize(i8* %28, i32 8192)
%44 = load i32, i32* %4
%45 = call i8* @http_request_headers(i32 %44)
%46 = icmp eq i8* %45, null
%47 = load i32, i32* %4
br i1 %46, label %50, label %48
48: ; preds = %42
%49 = call i32 @http_err(i32 %47, i32 500, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %45)
br label %56
50: ; preds = %42
%51 = call i32 @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%52 = call i32 @http_serve(i32 %47, i32 %51)
br label %56
53: ; preds = %38
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
br label %30
56: ; preds = %48, %50
ret i32 0
}