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

/* Variables and functions */
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
size_t STDIN_FILENO;
size_t STDOUT_FILENO;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, size_t);
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ strlen(char *);
char *check_diversion(char *diversion_name) {
  pid_t child;
  int pipedes[2];
  if (pipe(pipedes))
    return NULL;
  switch (child = fork()) {
  case -1:
    close(pipedes[0]);
    close(pipedes[1]);
    return NULL;
  case 0: {
    if (dup2(pipedes[STDOUT_FILENO], STDOUT_FILENO) < 0)
      _exit(127);
    close(pipedes[STDIN_FILENO]);
    close(pipedes[STDOUT_FILENO]);
    execl("/usr/sbin/dpkg-divert", "/usr/sbin/dpkg-divert", "--list",
          diversion_name, NULL);
    _exit(127);
  }
  default: {
    static char line[1024];
    FILE *fd;
    close(pipedes[STDOUT_FILENO]);
    fcntl(pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
    fd = fdopen(pipedes[STDIN_FILENO], "r");
    while (fgets(line, 1024, fd) != NULL) {
      line[strlen(line) - 1] = '\0';
      break;
    }
    fclose(fd);
    return line;
  }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<check_diversion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1090 <pipe@plt>
cmp    $0x0,%rax
je     11dc <check_diversion+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    134b <check_diversion+0x19b>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     1209 <check_diversion+0x59>
jmp    11f9 <check_diversion+0x49>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     1226 <check_diversion+0x76>
jmp    12a5 <check_diversion+0xf5>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    134b <check_diversion+0x19b>
mov    0x2dc3(%rip),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp,%rax,4),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%rsi
call   1070 <dup2@plt>
cmp    $0x0,%eax
jge    1256 <check_diversion+0xa6>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp,%rax,4),%edi
call   1080 <close@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp,%rax,4),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%rcx
lea    0xd79(%rip),%rsi
lea    0xd88(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   10c0 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp,%rax,4),%edi
call   1080 <close@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp,%rax,4),%edi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
mov    0x2cfa(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%rax
mov    -0x1c(%rbp,%rax,4),%edi
lea    0xd2b(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdx
lea    0x2d9a(%rip),%rdi
mov    $0x400,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     1337 <check_diversion+0x187>
lea    0x2d7f(%rip),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
movslq %eax,%rcx
lea    0x2d6d(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
lea    0x2d59(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_diversion>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1090 <pipe@plt>
test   %rax,%rax
jne    1271 <check_diversion+0xc1>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     1260 <check_diversion+0xb0>
test   %eax,%eax
je     127e <check_diversion+0xce>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    (%rsp,%rax,4),%edi
call   1080 <close@plt>
mov    0x2dcf(%rip),%rbx
mov    (%rbx),%rax
mov    (%rsp,%rax,4),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    (%rbx),%rax
mov    (%rsp,%rax,4),%edi
lea    0xdfa(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,%r14
lea    0x2e6e(%rip),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     1256 <check_diversion+0xa6>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    1273 <check_diversion+0xc3>
mov    (%rsp),%edi
call   1080 <close@plt>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d6b(%rip),%r14
mov    (%r14),%rsi
mov    (%rsp,%rsi,4),%edi
call   1070 <dup2@plt>
test   %eax,%eax
jns    129e <check_diversion+0xee>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%rax
mov    (%rsp,%rax,4),%edi
call   1080 <close@plt>
mov    (%r14),%rax
mov    (%rsp,%rax,4),%edi
call   1080 <close@plt>
lea    0xd3e(%rip),%rdi
lea    0xd4d(%rip),%rdx
mov    %rdi,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   10c0 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i64 0
@STDIN_FILENO = global i64 0
@.str = [22 x i8] c"/usr/sbin/dpkg-divert\00"
@.str.1 = [7 x i8] c"--list\00"
@check_diversion.line = internal global [1024 x i8] zeroinitializer
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.2 = [2 x i8] c"r\00"
define i8* @check_diversion(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca i32*
store i8* %0, i8** %3
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %68
11: ; preds = %1
%12 = call i32 (...) @fork()
store i32 %12, i32* %4
switch i32 %12, label %41 [
i32 -1, label %13
i32 0, label %20
]
13: ; preds = %11
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%15 = load i32, i32* %14
%16 = call i32 @close(i32 %15)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
store i8* null, i8** %2
br label %68
20: ; preds = %11
%21 = load i64, i64* @STDOUT_FILENO
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %21
%23 = load i32, i32* %22
%24 = load i64, i64* @STDOUT_FILENO
%25 = call i32 @dup2(i32 %23, i64 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @_exit(i32 127)
unreachable
29: ; preds = %20
%30 = load i64, i64* @STDIN_FILENO
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i64, i64* @STDOUT_FILENO
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = load i8*, i8** %3
%39 = call i32 @execl(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %38, i32* null)
%40 = call i32 @_exit(i32 127)
unreachable
41: ; preds = %11
%42 = load i64, i64* @STDOUT_FILENO
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %42
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = load i64, i64* @STDIN_FILENO
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %46
%48 = load i32, i32* %47
%49 = load i32, i32* @F_SETFD
%50 = load i32, i32* @FD_CLOEXEC
%51 = call i32 @fcntl(i32 %48, i32 %49, i32 %50)
%52 = load i64, i64* @STDIN_FILENO
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %52
%54 = load i32, i32* %53
%55 = call i32* @fdopen(i32 %54, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %55, i32** %6
br label %56
56: ; preds = %41
%57 = load i32*, i32** %6
%58 = call i32* @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @check_diversion.line, i64 0, i64 0), i32 1024, i32* %57)
%59 = icmp ne i32* %58, null
br i1 %59, label %60, label %65
60: ; preds = %56
%61 = call i32 @strlen(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @check_diversion.line, i64 0, i64 0))
%62 = sub nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* @check_diversion.line, i64 0, i64 %63
store i8 0, i8* %64
br label %65
65: ; preds = %60, %56
%66 = load i32*, i32** %6
%67 = call i32 @fclose(i32* %66)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @check_diversion.line, i64 0, i64 0), i8** %2
br label %68
68: ; preds = %65, %13, %10
%69 = load i8*, i8** %2
ret i8* %69
}

LLVM IR (-O2):
@STDOUT_FILENO = global i64 0
@STDIN_FILENO = global i64 0
@.str = [22 x i8] c"/usr/sbin/dpkg-divert\00"
@.str.1 = [7 x i8] c"--list\00"
@check_diversion.line = internal global [1024 x i8] zeroinitializer
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.2 = [2 x i8] c"r\00"
define i8* @check_diversion(i8* %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %58
7: ; preds = %1
%8 = call i32 (...) @fork()
switch i32 %8, label %34 [
i32 -1, label %9
i32 0, label %15
]
9: ; preds = %7
%10 = load i32, i32* %4
%11 = call i32 @close(i32 %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
br label %58
15: ; preds = %7
%16 = load i64, i64* @STDOUT_FILENO
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %16
%18 = load i32, i32* %17
%19 = call i32 @dup2(i32 %18, i64 %16)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @_exit(i32 127)
unreachable
23: ; preds = %15
%24 = load i64, i64* @STDIN_FILENO
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %24
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i64, i64* @STDOUT_FILENO
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %28
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = call i32 @execl(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32* null)
%33 = call i32 @_exit(i32 127)
unreachable
34: ; preds = %7
%35 = load i64, i64* @STDOUT_FILENO
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i64, i64* @STDIN_FILENO
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %39
%41 = load i32, i32* %40
%42 = load i32, i32* @F_SETFD
%43 = load i32, i32* @FD_CLOEXEC
%44 = call i32 @fcntl(i32 %41, i32 %42, i32 %43)
%45 = load i64, i64* @STDIN_FILENO
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %45
%47 = load i32, i32* %46
%48 = call i32* @fdopen(i32 %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32* @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @check_diversion.line, i64 0, i64 0), i32 1024, i32* %48)
%50 = icmp eq i32* %49, null
br i1 %50, label %56, label %51
51: ; preds = %34
%52 = call i32 @strlen(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @check_diversion.line, i64 0, i64 0))
%53 = add nsw i32 %52, -1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* @check_diversion.line, i64 0, i64 %54
store i8 0, i8* %55
br label %56
56: ; preds = %51, %34
%57 = call i32 @fclose(i32* %48)
br label %58
58: ; preds = %1, %56, %9
%59 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @check_diversion.line, i64 0, i64 0), %56 ], [ null, %9 ], [ null, %1 ]
ret i8* %59
}