#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 bind_socket(unsigned short);
int fcgi_spawn_connection(char *, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getopt(int, char **, char *);
char *optarg;
int /*<<< orphan*/ stderr;
void *strtol(char *, char **, int);
int main(int argc, char **argv) {
  char *fcgi_app = NULL;
  char *endptr = NULL;
  unsigned short port = 0;
  int fork_count = 1;
  int o;
  int fcgi_fd = -1;
  if (argc < 2) {
    return -1;
  }
  while (-1 != (o = getopt(argc, argv, "f:p:F:"))) {
    switch (o) {
    case 'f':
      fcgi_app = optarg;
      break;
    case 'p':
      port = strtol(optarg, &endptr, 10);
      if (*endptr) {
        fprintf(stderr, "spawn-fcgi: invalid port: %u\n", (unsigned int)port);
        return -1;
      }
      break;
    case 'F':
      fork_count = strtol(optarg, NULL, 10);
      break;
    default:
      return -1;
    }
  }
  if (NULL == fcgi_app) {
    fprintf(stderr, "spawn-fcgi: no FastCGI application given\n");
    return -1;
  }
  if (0 == port) {
    fprintf(stderr, "spawn-fcgi: no socket given (use either -p or -s)\n");
    return -1;
  }
  if (-1 == (fcgi_fd = bind_socket(port))) {
    return -1;
  }
  return fcgi_spawn_connection(fcgi_app, fcgi_fd, fork_count);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movw   $0x0,-0x22(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0xffffffff,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a0 <main+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <main+0x1e2>
jmp    11a5 <main+0x55>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe4d(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     1299 <main+0x149>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x46,%eax
je     1268 <main+0x118>
jmp    11de <main+0x8e>
mov    -0x34(%rbp),%eax
sub    $0x66,%eax
je     1200 <main+0xb0>
jmp    11ef <main+0x9f>
mov    -0x34(%rbp),%eax
sub    $0x70,%eax
je     1213 <main+0xc3>
jmp    1288 <main+0x138>
mov    0x2df1(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1294 <main+0x144>
mov    0x2dde(%rip),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %ax,-0x22(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1263 <main+0x113>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
movzwl -0x22(%rbp),%edx
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <main+0x1e2>
jmp    1294 <main+0x144>
mov    0x2d89(%rip),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x28(%rbp)
jmp    1294 <main+0x144>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <main+0x1e2>
jmp    11a5 <main+0x55>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    12c8 <main+0x178>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <main+0x1e2>
movzwl -0x22(%rbp),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    12f9 <main+0x1a9>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <main+0x1e2>
movzwl -0x22(%rbp),%edi
call   1030 <bind_socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1320 <main+0x1d0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <main+0x1e2>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1040 <fcgi_spawn_connection@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
cmp    $0x2,%edi
jl     127f <main+0x12f>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x1,%r15d
xor    %eax,%eax
mov    %rax,0x8(%rsp)
lea    0xe77(%rip),%r13
mov    0x2e68(%rip),%r14
xor    %r12d,%r12d
jmp    11b2 <main+0x62>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1070 <getopt@plt>
cmp    $0x46,%eax
je     11a0 <main+0x50>
cmp    $0xffffffff,%eax
je     121d <main+0xcd>
cmp    $0x70,%eax
je     11e1 <main+0x91>
cmp    $0x66,%eax
jne    127f <main+0x12f>
mov    (%r14),%rax
mov    %rax,0x8(%rsp)
jmp    11b2 <main+0x62>
mov    (%r14),%rdi
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%r12
mov    0x10(%rsp),%rax
cmpb   $0x0,(%rax)
je     11b2 <main+0x62>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
movzwl %r12w,%edx
lea    0xdf3(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    127f <main+0x12f>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1256 <main+0x106>
test   %r12w,%r12w
je     1268 <main+0x118>
movzwl %r12w,%edi
call   1030 <bind_socket@plt>
cmp    $0xffffffff,%eax
je     127f <main+0x12f>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r15d,%edx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fcgi_spawn_connection@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
jmp    1278 <main+0x128>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"f:p:F:\00"
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [30 x i8] c"spawn-fcgi: invalid port: %u\0A\00"
@.str.2 = [42 x i8] c"spawn-fcgi: no FastCGI application given\0A\00"
@.str.3 = [51 x i8] c"spawn-fcgi: no socket given (use either -p or -s)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i16
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i16 0, i16* %8
store i32 1, i32* %9
store i32 -1, i32* %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %67
15: ; preds = %2
br label %16
16: ; preds = %43, %15
%17 = load i32, i32* %4
%18 = load i8**, i8*** %5
%19 = call i32 @getopt(i32 %17, i8** %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %19, i32* %10
%20 = icmp ne i32 -1, %19
br i1 %20, label %21, label %44
21: ; preds = %16
%22 = load i32, i32* %10
switch i32 %22, label %42 [
i32 102, label %23
i32 112, label %25
i32 70, label %38
]
23: ; preds = %21
%24 = load i8*, i8** @optarg
store i8* %24, i8** %6
br label %43
25: ; preds = %21
%26 = load i8*, i8** @optarg
%27 = call i8* @strtol(i8* %26, i8** %7, i32 10)
%28 = ptrtoint i8* %27 to i16
store i16 %28, i16* %8
%29 = load i8*, i8** %7
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = load i16, i16* %8
%35 = zext i16 %34 to i32
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %35)
store i32 -1, i32* %3
br label %67
37: ; preds = %25
br label %43
38: ; preds = %21
%39 = load i8*, i8** @optarg
%40 = call i8* @strtol(i8* %39, i8** null, i32 10)
%41 = ptrtoint i8* %40 to i32
store i32 %41, i32* %9
br label %43
42: ; preds = %21
store i32 -1, i32* %3
br label %67
43: ; preds = %38, %37, %23
br label %16
44: ; preds = %16
%45 = load i8*, i8** %6
%46 = icmp eq i8* null, %45
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
50: ; preds = %44
%51 = load i16, i16* %8
%52 = zext i16 %51 to i32
%53 = icmp eq i32 0, %52
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %67
57: ; preds = %50
%58 = load i16, i16* %8
%59 = call i32 @bind_socket(i16 zeroext %58)
store i32 %59, i32* %11
%60 = icmp eq i32 -1, %59
br i1 %60, label %61, label %62
61: ; preds = %57
store i32 -1, i32* %3
br label %67
62: ; preds = %57
%63 = load i8*, i8** %6
%64 = load i32, i32* %11
%65 = load i32, i32* %9
%66 = call i32 @fcgi_spawn_connection(i8* %63, i32 %64, i32 %65)
store i32 %66, i32* %3
br label %67
67: ; preds = %62, %61, %54, %47, %42, %32, %14
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [7 x i8] c"f:p:F:\00"
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [30 x i8] c"spawn-fcgi: invalid port: %u\0A\00"
@.str.2 = [42 x i8] c"spawn-fcgi: no FastCGI application given\0A\00"
@.str.3 = [51 x i8] c"spawn-fcgi: no socket given (use either -p or -s)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = icmp slt i32 %0, 2
br i1 %5, label %52, label %6
6: ; preds = %2, %16
%7 = phi i8* [ %17, %16 ], [ null, %2 ]
%8 = phi i16 [ %11, %16 ], [ 0, %2 ]
%9 = phi i32 [ %14, %16 ], [ 1, %2 ]
br label %10
10: ; preds = %6, %18
%11 = phi i16 [ %8, %6 ], [ %22, %18 ]
%12 = phi i32 [ %9, %6 ], [ %14, %18 ]
br label %13
13: ; preds = %10, %32
%14 = phi i32 [ %36, %32 ], [ %12, %10 ]
%15 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
switch i32 %15, label %52 [
i32 -1, label %37
i32 102, label %16
i32 112, label %18
i32 70, label %32
]
16: ; preds = %13
%17 = load i8*, i8** @optarg
br label %6
18: ; preds = %13
%19 = load i8*, i8** @optarg
%20 = call i8* @strtol(i8* %19, i8** %3, i32 10)
%21 = ptrtoint i8* %20 to i64
%22 = trunc i64 %21 to i16
%23 = load i8*, i8** %3
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %10, label %26
26: ; preds = %18
%27 = ptrtoint i8* %20 to i64
%28 = load i32, i32* @stderr
%29 = trunc i64 %27 to i32
%30 = and i32 %29, 65535
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %30)
br label %52
32: ; preds = %13
%33 = load i8*, i8** @optarg
%34 = tail call i8* @strtol(i8* nocapture %33, i8** null, i32 10)
%35 = ptrtoint i8* %34 to i64
%36 = trunc i64 %35 to i32
br label %13
37: ; preds = %13
%38 = icmp eq i8* %7, null
br i1 %38, label %39, label %42
39: ; preds = %37
%40 = load i32, i32* @stderr
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %52
42: ; preds = %37
%43 = icmp eq i16 %11, 0
br i1 %43, label %44, label %47
44: ; preds = %42
%45 = load i32, i32* @stderr
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
br label %52
47: ; preds = %42
%48 = tail call i32 @bind_socket(i16 zeroext %11)
%49 = icmp eq i32 %48, -1
br i1 %49, label %52, label %50
50: ; preds = %47
%51 = tail call i32 @fcgi_spawn_connection(i8* %7, i32 %48, i32 %14)
br label %52
52: ; preds = %13, %47, %2, %50, %44, %39, %26
%53 = phi i32 [ -1, %26 ], [ -1, %39 ], [ -1, %44 ], [ %51, %50 ], [ -1, %2 ], [ -1, %47 ], [ -1, %13 ]
ret i32 %53
}