#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fileno(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
scalar_t__ isatty(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strerror(int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd;
  struct stat buf;
  int dofork = 0;
  if (argc > 1 && !strcmp(argv[1], "-f")) {
    dofork = 1;
    argc--;
    argv++;
  }
  if (argc < 3) {
    fprintf(stderr, "usage: setctsid [-f] tty program [arg ...]\n");
    exit(1);
  }
  if (stat(argv[1], &buf) < 0) {
    perror(argv[1]);
    exit(1);
  }
  if (!(S_ISCHR(buf.st_mode))) {
    fprintf(stderr, "%s: not a character device\n", argv[1]);
    exit(1);
  }
  if (dofork) {
    switch (fork()) {
    case -1:
      perror("fork");
      exit(1);
    case 0:
      break;
    default:
      exit(0);
    }
  }
  if (setsid() < 0) {
    perror("setsid");
    exit(1);
  }
  if ((fd = open(argv[1], O_RDWR, 0)) < 0) {
    do {
      int error = errno;
      if (!isatty(fileno(stderr)))
        syslog(LOG_ERR,
               "open"
               ": %s",
               strerror(error));
      else
        errno = error, perror("open");
    } while (0);
    exit(1);
  }
  dup2(fd, fileno(stdin));
  dup2(fd, fileno(stdout));
  dup2(fd, fileno(stderr));
  if (isatty(fd)) {
    if (ioctl(fileno(stdin), TIOCSCTTY, argv[1]) < 0) {
      do {
        int error = errno;
        if (!isatty(fileno(stderr)))
          syslog(LOG_ERR,
                 "ioctl"
                 ": %s",
                 strerror(error));
        else
          errno = error, perror("ioctl");
      } while (0);
      exit(1);
    }
  }
  if (fd > fileno(stderr))
    close(fd);
  execvp(argv[2], argv + 2);
  do {
    int error = errno;
    if (!isatty(fileno(stderr)))
      syslog(LOG_ERR,
             "execvp"
             ": %s",
             strerror(error));
    else
      errno = error, perror("execvp");
  } while (0);
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1270 <main+0x60>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdba(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    1270 <main+0x60>
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    129b <main+0x8b>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jge    12cd <main+0xbd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x18(%rbp),%edi
call   10d0 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    1307 <main+0xf7>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd39(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1362 <main+0x152>
xor    %eax,%eax
call   1130 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     133b <main+0x12b>
jmp    132b <main+0x11b>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     1351 <main+0x141>
jmp    1356 <main+0x146>
lea    0xd09(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
jmp    135d <main+0x14d>
xor    %edi,%edi
call   1110 <exit@plt>
jmp    1362 <main+0x152>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0x0,%rax
jge    1389 <main+0x179>
lea    0xcd6(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2c50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    142e <main+0x21e>
jmp    13b2 <main+0x1a2>
mov    0x2c0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2c0b(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1407 <main+0x1f7>
mov    0x2bc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1120 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xc5a(%rip),%rsi
call   10c0 <syslog@plt>
jmp    141f <main+0x20f>
mov    -0x20(%rbp),%ecx
mov    0x2bb7(%rip),%rax
mov    %ecx,(%rax)
lea    0xc46(%rip),%rdi
call   10f0 <perror@plt>
jmp    1424 <main+0x214>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2ba5(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    -0x3c(%rbp),%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2b67(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    -0x38(%rbp),%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2b59(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     1550 <main+0x340>
mov    0x2b3f(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
mov    0x2b07(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    154b <main+0x33b>
jmp    14cf <main+0x2bf>
mov    0x2af2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2aee(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1524 <main+0x314>
mov    0x2aa7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
call   1120 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %eax,%edx
lea    0xb4b(%rip),%rsi
call   10c0 <syslog@plt>
jmp    153c <main+0x32c>
mov    -0x24(%rbp),%ecx
mov    0x2a9a(%rip),%rax
mov    %ecx,(%rax)
lea    0xb38(%rip),%rdi
call   10f0 <perror@plt>
jmp    1541 <main+0x331>
mov    $0x1,%edi
call   1110 <exit@plt>
jmp    1550 <main+0x340>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2a73(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jle    1579 <main+0x369>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1100 <execvp@plt>
mov    0x2a33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2a2f(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    15e3 <main+0x3d3>
mov    0x29e8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%edi
call   1120 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %eax,%edx
lea    0xa9c(%rip),%rsi
call   10c0 <syslog@plt>
jmp    15fb <main+0x3eb>
mov    -0x28(%rbp),%ecx
mov    0x29db(%rip),%rax
mov    %ecx,(%rax)
lea    0xa8a(%rip),%rdi
call   10f0 <perror@plt>
jmp    1600 <main+0x3f0>
mov    $0x1,%edi
call   1110 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     124b <main+0x3b>
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0x8(%rsi),%r14
mov    0x8(%rsi),%rdi
lea    0xdd0(%rip),%rsi
call   1080 <strcmp@plt>
mov    %eax,%r15d
xor    %eax,%eax
test   %r15d,%r15d
sete   %al
cmovne %rbp,%r14
sub    %eax,%ebx
cmp    $0x2,%ebx
ja     126c <main+0x5c>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    0x8(%r14),%rdi
mov    %rsp,%rsi
call   10a0 <stat@plt>
test   %rax,%rax
jns    1290 <main+0x80>
mov    0x8(%r14),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    (%rsp),%edi
call   10d0 <S_ISCHR@plt>
test   %eax,%eax
jne    12c1 <main+0xb1>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd7f(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
test   %r15d,%r15d
jne    12df <main+0xcf>
xor    %eax,%eax
call   1130 <fork@plt>
test   %eax,%eax
je     12df <main+0xcf>
cmp    $0xffffffff,%eax
jne    1343 <main+0x133>
lea    0xd6e(%rip),%rdi
jmp    1281 <main+0x71>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
jns    12f4 <main+0xe4>
lea    0xd5e(%rip),%rdi
jmp    1281 <main+0x71>
mov    0x8(%r14),%rdi
mov    0x2ce9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <open@plt>
test   %eax,%eax
jns    134a <main+0x13a>
mov    0x2cb5(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     143f <main+0x22f>
mov    %ebx,0x0(%rbp)
lea    0xd22(%rip),%rdi
jmp    1281 <main+0x71>
xor    %edi,%edi
call   1110 <exit@plt>
mov    %eax,%ebp
mov    0x2c8d(%rip),%rbx
mov    (%rbx),%edi
call   10b0 <fileno@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    0x2c4f(%rip),%r15
mov    (%r15),%edi
call   10b0 <fileno@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     13ef <main+0x1df>
mov    (%rbx),%edi
call   10b0 <fileno@plt>
mov    0x2c0c(%rip),%rcx
mov    (%rcx),%esi
mov    0x8(%r14),%rdx
mov    %eax,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
jns    13ef <main+0x1df>
mov    0x2c03(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    (%r15),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     1471 <main+0x261>
mov    %ebx,0x0(%rbp)
lea    0xc85(%rip),%rdi
jmp    1281 <main+0x71>
mov    (%r15),%edi
call   10b0 <fileno@plt>
cmp    %eax,%ebp
jle    1402 <main+0x1f2>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x10(%r14),%rdi
add    $0x10,%r14
mov    %r14,%rsi
call   1100 <execvp@plt>
mov    0x2baf(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    (%r15),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     1458 <main+0x248>
mov    %ebx,0x0(%rbp)
lea    0xc46(%rip),%rdi
jmp    1281 <main+0x71>
mov    0x2b62(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1120 <strerror@plt>
lea    0xc01(%rip),%rsi
jmp    1488 <main+0x278>
mov    0x2b49(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1120 <strerror@plt>
lea    0xc06(%rip),%rsi
jmp    1488 <main+0x278>
mov    0x2b30(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1120 <strerror@plt>
lea    0xbdd(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   10c0 <syslog@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"-f\00"
@stderr = global i32 0
@.str.1 = [44 x i8] c"usage: setctsid [-f] tty program [arg ...]\0A\00"
@.str.2 = [28 x i8] c"%s: not a character device\0A\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [7 x i8] c"setsid\00"
@O_RDWR = global i32 0
@errno = global i32 0
@LOG_ERR = global i32 0
@.str.5 = [9 x i8] c"open: %s\00"
@.str.6 = [5 x i8] c"open\00"
@stdin = global i32 0
@stdout = global i32 0
@TIOCSCTTY = global i32 0
@.str.7 = [10 x i8] c"ioctl: %s\00"
@.str.8 = [6 x i8] c"ioctl\00"
@.str.9 = [11 x i8] c"execvp: %s\00"
@.str.10 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%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 i32 0, i32* %8
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %25
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %14
store i32 1, i32* %8
%21 = load i32, i32* %4
%22 = add nsw i32 %21, -1
store i32 %22, i32* %4
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i32 1
store i8** %24, i8*** %5
br label %25
25: ; preds = %20, %14, %2
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 3
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i64 @stat(i8* %35, %struct.stat* %7)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %32
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @perror(i8* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %32
%45 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @S_ISCHR(i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %56, label %49
49: ; preds = %44
%50 = load i32, i32* @stderr
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %44
%57 = load i32, i32* %8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %68
59: ; preds = %56
%60 = call i32 (...) @fork()
switch i32 %60, label %65 [
i32 -1, label %61
i32 0, label %64
]
61: ; preds = %59
%62 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %59
br label %67
65: ; preds = %59
%66 = call i32 @exit(i32 0)
unreachable
67: ; preds = %64
br label %68
68: ; preds = %67, %56
%69 = call i64 (...) @setsid()
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %68
%75 = load i8**, i8*** %5
%76 = getelementptr inbounds i8*, i8** %75, i64 1
%77 = load i8*, i8** %76
%78 = load i32, i32* @O_RDWR
%79 = call i32 @open(i8* %77, i32 %78, i32 0)
store i32 %79, i32* %6
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %99
81: ; preds = %74
br label %82
82: ; preds = %81
%83 = load i32, i32* @errno
store i32 %83, i32* %9
%84 = load i32, i32* @stderr
%85 = call i32 @fileno(i32 %84)
%86 = call i64 @isatty(i32 %85)
%87 = icmp ne i64 %86, 0
br i1 %87, label %93, label %88
88: ; preds = %82
%89 = load i32, i32* @LOG_ERR
%90 = load i32, i32* %9
%91 = call i32 @strerror(i32 %90)
%92 = call i32 @syslog(i32 %89, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %91)
br label %96
93: ; preds = %82
%94 = load i32, i32* %9
store i32 %94, i32* @errno
%95 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %96
96: ; preds = %93, %88
br label %97
97: ; preds = %96
%98 = call i32 @exit(i32 1)
unreachable
99: ; preds = %74
%100 = load i32, i32* %6
%101 = load i32, i32* @stdin
%102 = call i32 @fileno(i32 %101)
%103 = call i32 @dup2(i32 %100, i32 %102)
%104 = load i32, i32* %6
%105 = load i32, i32* @stdout
%106 = call i32 @fileno(i32 %105)
%107 = call i32 @dup2(i32 %104, i32 %106)
%108 = load i32, i32* %6
%109 = load i32, i32* @stderr
%110 = call i32 @fileno(i32 %109)
%111 = call i32 @dup2(i32 %108, i32 %110)
%112 = load i32, i32* %6
%113 = call i64 @isatty(i32 %112)
%114 = icmp ne i64 %113, 0
br i1 %114, label %115, label %143
115: ; preds = %99
%116 = load i32, i32* @stdin
%117 = call i32 @fileno(i32 %116)
%118 = load i32, i32* @TIOCSCTTY
%119 = load i8**, i8*** %5
%120 = getelementptr inbounds i8*, i8** %119, i64 1
%121 = load i8*, i8** %120
%122 = call i64 @ioctl(i32 %117, i32 %118, i8* %121)
%123 = icmp slt i64 %122, 0
br i1 %123, label %124, label %142
124: ; preds = %115
br label %125
125: ; preds = %124
%126 = load i32, i32* @errno
store i32 %126, i32* %10
%127 = load i32, i32* @stderr
%128 = call i32 @fileno(i32 %127)
%129 = call i64 @isatty(i32 %128)
%130 = icmp ne i64 %129, 0
br i1 %130, label %136, label %131
131: ; preds = %125
%132 = load i32, i32* @LOG_ERR
%133 = load i32, i32* %10
%134 = call i32 @strerror(i32 %133)
%135 = call i32 @syslog(i32 %132, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %134)
br label %139
136: ; preds = %125
%137 = load i32, i32* %10
store i32 %137, i32* @errno
%138 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
br label %139
139: ; preds = %136, %131
br label %140
140: ; preds = %139
%141 = call i32 @exit(i32 1)
unreachable
142: ; preds = %115
br label %143
143: ; preds = %142, %99
%144 = load i32, i32* %6
%145 = load i32, i32* @stderr
%146 = call i32 @fileno(i32 %145)
%147 = icmp sgt i32 %144, %146
br i1 %147, label %148, label %151
148: ; preds = %143
%149 = load i32, i32* %6
%150 = call i32 @close(i32 %149)
br label %151
151: ; preds = %148, %143
%152 = load i8**, i8*** %5
%153 = getelementptr inbounds i8*, i8** %152, i64 2
%154 = load i8*, i8** %153
%155 = load i8**, i8*** %5
%156 = getelementptr inbounds i8*, i8** %155, i64 2
%157 = call i32 @execvp(i8* %154, i8** %156)
br label %158
158: ; preds = %151
%159 = load i32, i32* @errno
store i32 %159, i32* %11
%160 = load i32, i32* @stderr
%161 = call i32 @fileno(i32 %160)
%162 = call i64 @isatty(i32 %161)
%163 = icmp ne i64 %162, 0
br i1 %163, label %169, label %164
164: ; preds = %158
%165 = load i32, i32* @LOG_ERR
%166 = load i32, i32* %11
%167 = call i32 @strerror(i32 %166)
%168 = call i32 @syslog(i32 %165, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 %167)
br label %172
169: ; preds = %158
%170 = load i32, i32* %11
store i32 %170, i32* @errno
%171 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
br label %172
172: ; preds = %169, %164
br label %173
173: ; preds = %172
%174 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"-f\00"
@stderr = global i32 0
@.str.1 = [44 x i8] c"usage: setctsid [-f] tty program [arg ...]\0A\00"
@.str.2 = [28 x i8] c"%s: not a character device\0A\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [7 x i8] c"setsid\00"
@O_RDWR = global i32 0
@errno = global i32 0
@LOG_ERR = global i32 0
@.str.5 = [9 x i8] c"open: %s\00"
@.str.6 = [5 x i8] c"open\00"
@stdin = global i32 0
@stdout = global i32 0
@TIOCSCTTY = global i32 0
@.str.7 = [10 x i8] c"ioctl: %s\00"
@.str.8 = [6 x i8] c"ioctl\00"
@.str.9 = [11 x i8] c"execvp: %s\00"
@.str.10 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %15
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %8, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
%11 = select i1 %10, i8** %7, i8** %1
%12 = sext i1 %10 to i32
%13 = add nsw i32 %12, %0
%14 = icmp ult i32 %13, 3
br i1 %14, label %15, label %19
15: ; preds = %2, %6
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %6
%20 = getelementptr inbounds i8*, i8** %11, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @stat(i8* %21, %struct.stat* %3)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i8*, i8** %20
%26 = call i32 @perror(i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %19
%29 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @S_ISCHR(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %20
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %28
br i1 %10, label %39, label %46
39: ; preds = %38
%40 = call i32 (...) @fork()
switch i32 %40, label %44 [
i32 -1, label %41
i32 0, label %46
]
41: ; preds = %39
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %39
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %39, %38
%47 = call i64 (...) @setsid()
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %46
%53 = load i8*, i8** %20
%54 = load i32, i32* @O_RDWR
%55 = call i32 @open(i8* %53, i32 %54, i32 0)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %71
57: ; preds = %52
%58 = load i32, i32* @errno
%59 = load i32, i32* @stderr
%60 = call i32 @fileno(i32 %59)
%61 = call i64 @isatty(i32 %60)
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %57
%64 = load i32, i32* @LOG_ERR
%65 = call i32 @strerror(i32 %58)
%66 = call i32 @syslog(i32 %64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %65)
br label %69
67: ; preds = %57
store i32 %58, i32* @errno
%68 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
br label %69
69: ; preds = %67, %63
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %52
%72 = load i32, i32* @stdin
%73 = call i32 @fileno(i32 %72)
%74 = call i32 @dup2(i32 %55, i32 %73)
%75 = load i32, i32* @stdout
%76 = call i32 @fileno(i32 %75)
%77 = call i32 @dup2(i32 %55, i32 %76)
%78 = load i32, i32* @stderr
%79 = call i32 @fileno(i32 %78)
%80 = call i32 @dup2(i32 %55, i32 %79)
%81 = call i64 @isatty(i32 %55)
%82 = icmp eq i64 %81, 0
br i1 %82, label %104, label %83
83: ; preds = %71
%84 = load i32, i32* @stdin
%85 = call i32 @fileno(i32 %84)
%86 = load i32, i32* @TIOCSCTTY
%87 = load i8*, i8** %20
%88 = call i64 @ioctl(i32 %85, i32 %86, i8* %87)
%89 = icmp slt i64 %88, 0
br i1 %89, label %90, label %104
90: ; preds = %83
%91 = load i32, i32* @errno
%92 = load i32, i32* @stderr
%93 = call i32 @fileno(i32 %92)
%94 = call i64 @isatty(i32 %93)
%95 = icmp eq i64 %94, 0
br i1 %95, label %96, label %100
96: ; preds = %90
%97 = load i32, i32* @LOG_ERR
%98 = call i32 @strerror(i32 %91)
%99 = call i32 @syslog(i32 %97, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %98)
br label %102
100: ; preds = %90
store i32 %91, i32* @errno
%101 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
br label %102
102: ; preds = %100, %96
%103 = call i32 @exit(i32 1)
unreachable
104: ; preds = %83, %71
%105 = load i32, i32* @stderr
%106 = call i32 @fileno(i32 %105)
%107 = icmp sgt i32 %55, %106
br i1 %107, label %108, label %110
108: ; preds = %104
%109 = call i32 @close(i32 %55)
br label %110
110: ; preds = %108, %104
%111 = getelementptr inbounds i8*, i8** %11, i64 2
%112 = load i8*, i8** %111
%113 = call i32 @execvp(i8* %112, i8** %111)
%114 = load i32, i32* @errno
%115 = load i32, i32* @stderr
%116 = call i32 @fileno(i32 %115)
%117 = call i64 @isatty(i32 %116)
%118 = icmp eq i64 %117, 0
br i1 %118, label %119, label %123
119: ; preds = %110
%120 = load i32, i32* @LOG_ERR
%121 = call i32 @strerror(i32 %114)
%122 = call i32 @syslog(i32 %120, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i32 %121)
br label %125
123: ; preds = %110
store i32 %114, i32* @errno
%124 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
br label %125
125: ; preds = %123, %119
%126 = call i32 @exit(i32 1)
unreachable
}