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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopendir(int);
int open(char *, int, ...);
int /*<<< orphan*/ *opendir(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void noleak(int x, int y, int z) {
  DIR *p1 = fdopendir(x);
  closedir(p1);
  DIR *p2 = opendir("abc");
  closedir(p2);
  int s = socket(AF_INET, SOCK_STREAM, 0);
  close(s);
  int fd1 = open("a", O_RDWR | O_CREAT);
  close(fd1);
  int fd2 = open("a", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  close(fd2);
}


x86 assembly (clang, -O0):
<noleak>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <fdopendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
lea    0xe73(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
or     (%rax),%esi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    0x2dc0(%rip),%rax
or     (%rax),%edx
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<noleak>:
push   %r15
push   %r14
push   %rbx
call   1070 <fdopendir@plt>
mov    %rax,%rdi
call   1050 <closedir@plt>
lea    0xe87(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%rdi
call   1050 <closedir@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%edi
call   1040 <close@plt>
mov    0x2e33(%rip),%r15
mov    0x2e44(%rip),%rbx
mov    (%rbx),%esi
or     (%r15),%esi
lea    0xe44(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%edi
call   1040 <close@plt>
mov    (%rbx),%esi
or     (%r15),%esi
mov    0x2ddb(%rip),%rax
mov    0x2dec(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <close@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"abc\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [2 x i8] c"a\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define void @noleak(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i32, i32* %4
%13 = call i32* @fdopendir(i32 %12)
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = call i32 @closedir(i32* %14)
%16 = call i32* @opendir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = call i32 @closedir(i32* %17)
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRUSR
%34 = load i32, i32* @S_IWUSR
%35 = or i32 %33, %34
%36 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %35)
store i32 %36, i32* %11
%37 = load i32, i32* %11
%38 = call i32 @close(i32 %37)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"abc\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [2 x i8] c"a\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define void @noleak(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32* @fdopendir(i32 %0)
%5 = tail call i32 @closedir(i32* %4)
%6 = tail call i32* @opendir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @closedir(i32* %6)
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = tail call i32 @close(i32 %10)
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = tail call i32 @close(i32 %15)
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %21, %20
%23 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %22)
%24 = tail call i32 @close(i32 %23)
ret void
}