#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*/ EXIT_FAILURE;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ LOG_DAEMON;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_NOTICE;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ closelog();
scalar_t__ dup2(int, int);
int execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int worker_main(int sockfd, char connect_script[]) {
  int fd;
  int ret;
  if (sockfd >= 0) {
    if ((fd = dup2(sockfd, 4) < 0)) {
      fprintf(stderr, "Couldn't open file descriptor 4: %m");
      syslog(LOG_ERR, "Couldn't open file descriptor 4: %m");
      exit(EXIT_FAILURE);
    }
    fcntl(fd, F_SETFD, 0);
  }
  syslog(LOG_NOTICE, "executing %s", connect_script);
  closelog();
  ret = execl("/bin/sh", "sh", "-c", connect_script, NULL);
  openlog("chap-proxy", LOG_PID, LOG_DAEMON);
  syslog(LOG_ERR, "execl: %m");
  return ret;
}


x86 assembly (clang, -O0):
<worker_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1212 <worker_main+0x92>
mov    -0x4(%rbp),%edi
mov    $0x4,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x14(%rbp)
test   $0x1,%al
jne    11c5 <worker_main+0x45>
jmp    11ff <worker_main+0x7f>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    $0x0,%al
call   1030 <closelog@plt>
mov    -0x10(%rbp),%rcx
lea    0xdf2(%rip),%rdi
lea    0xdf0(%rip),%rsi
lea    0xdec(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   10a0 <execl@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
lea    0xdc9(%rip),%rdi
call   1080 <openlog@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<worker_main>:
push   %rbx
mov    %rsi,%rbx
test   %edi,%edi
js     11b1 <worker_main+0x31>
mov    $0x4,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     122d <worker_main+0xad>
shr    $0x3f,%rax
mov    0x2e22(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <syslog@plt>
xor    %eax,%eax
call   1030 <closelog@plt>
lea    0xe58(%rip),%rdi
lea    0xe56(%rip),%rsi
lea    0xe52(%rip),%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   10a0 <execl@plt>
mov    %eax,%ebx
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
lea    0xe2f(%rip),%rdi
call   1080 <openlog@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rbx
mov    %rbx,%rsi
call   1060 <fprintf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"Couldn't open file descriptor 4: %m\00"
@LOG_ERR = global i32 0
@EXIT_FAILURE = global i32 0
@F_SETFD = global i32 0
@LOG_NOTICE = global i32 0
@.str.1 = [13 x i8] c"executing %s\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [11 x i8] c"chap-proxy\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
@.str.6 = [10 x i8] c"execl: %m\00"
define i32 @worker_main(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i32, i32* %3
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %25
9: ; preds = %2
%10 = load i32, i32* %3
%11 = call i64 @dup2(i32 %10, i32 4)
%12 = icmp slt i64 %11, 0
%13 = zext i1 %12 to i32
store i32 %13, i32* %5
br i1 %12, label %14, label %21
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @LOG_ERR
%18 = call i32 (i32, i8*, ...) @syslog(i32 %17, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %9
%22 = load i32, i32* %5
%23 = load i32, i32* @F_SETFD
%24 = call i32 @fcntl(i32 %22, i32 %23, i32 0)
br label %25
25: ; preds = %21, %2
%26 = load i32, i32* @LOG_NOTICE
%27 = load i8*, i8** %4
%28 = call i32 (i32, i8*, ...) @syslog(i32 %26, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = call i32 (...) @closelog()
%30 = load i8*, i8** %4
%31 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %30, i32* null)
store i32 %31, i32* %6
%32 = load i32, i32* @LOG_PID
%33 = load i32, i32* @LOG_DAEMON
%34 = call i32 @openlog(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %32, i32 %33)
%35 = load i32, i32* @LOG_ERR
%36 = call i32 (i32, i8*, ...) @syslog(i32 %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
%37 = load i32, i32* %6
ret i32 %37
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"Couldn't open file descriptor 4: %m\00"
@LOG_ERR = global i32 0
@EXIT_FAILURE = global i32 0
@F_SETFD = global i32 0
@LOG_NOTICE = global i32 0
@.str.1 = [13 x i8] c"executing %s\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [11 x i8] c"chap-proxy\00"
@LOG_PID = global i32 0
@LOG_DAEMON = global i32 0
@.str.6 = [10 x i8] c"execl: %m\00"
define i32 @worker_main(i32 %0, i8* %1) {
%3 = icmp sgt i32 %0, -1
br i1 %3, label %4, label %19
4: ; preds = %2
%5 = tail call i64 @dup2(i32 %0, i32 4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @LOG_ERR
%11 = tail call i32 (i32, i8*, ...) @syslog(i32 %10, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4
%15 = lshr i64 %5, 63
%16 = trunc i64 %15 to i32
%17 = load i32, i32* @F_SETFD
%18 = tail call i32 @fcntl(i32 %16, i32 %17, i32 0)
br label %19
19: ; preds = %14, %2
%20 = load i32, i32* @LOG_NOTICE
%21 = tail call i32 (i32, i8*, ...) @syslog(i32 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%22 = tail call i32 (...) @closelog()
%23 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %1, i32* null)
%24 = load i32, i32* @LOG_PID
%25 = load i32, i32* @LOG_DAEMON
%26 = tail call i32 @openlog(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* @LOG_ERR
%28 = tail call i32 (i32, i8*, ...) @syslog(i32 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
ret i32 %23
}