#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 dummy;
};

/* Variables and functions */
scalar_t__ EFAULT;
scalar_t__ errno;
int lstat(char *, struct stat *);
int /*<<< orphan*/ printf(char *);
int stat(char *, struct stat *);
int main(int argc, char **argv) {
  int r;
  struct stat buf;
  char *goodpath = argv[0];
  char *badpath = (void *)-1;
  struct stat *goodbuf = &buf;
  struct stat *badbuf = (void *)-1;
  r = stat(badpath, goodbuf);
  if (r == -1 && errno == EFAULT)
    printf("stat(invalid-path-ptr) correctly returned error\n");
  r = stat(goodpath, badbuf);
  if (r == -1 && errno == EFAULT)
    printf("stat(invalid-buf-ptr) correctly returned error\n");
  r = lstat(badpath, goodbuf);
  if (r == -1 && errno == EFAULT)
    printf("lstat(invalid-path-ptr) correctly returned error\n");
  r = lstat(goodpath, badbuf);
  if (r == -1 && errno == EFAULT)
    printf("lstat(invalid-buf-ptr) correctly returned error\n");
  return 0;
}


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)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11af <main+0x7f>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
jne    11af <main+0x7f>
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ef <main+0xbf>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11ef <main+0xbf>
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    122f <main+0xff>
mov    0x2dc8(%rip),%rax
mov    (%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    122f <main+0xff>
lea    0xdd6(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    126f <main+0x13f>
mov    0x2d88(%rip),%rax
mov    (%rax),%rax
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%rax
jne    126f <main+0x13f>
lea    0xdc8(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    (%rsi),%rbx
lea    0x8(%rsp),%rsi
mov    $0xffffffffffffffff,%rdi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    1170 <main+0x40>
mov    0x2e83(%rip),%rax
mov    (%rax),%rax
mov    0x2e99(%rip),%rcx
cmp    (%rcx),%rax
jne    1170 <main+0x40>
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0xffffffffffffffff,%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    11a6 <main+0x76>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jne    11a6 <main+0x76>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    $0xffffffffffffffff,%rdi
call   1040 <lstat@plt>
cmp    $0xffffffff,%eax
jne    11de <main+0xae>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <main+0xae>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0xffffffffffffffff,%rsi
call   1040 <lstat@plt>
cmp    $0xffffffff,%eax
jne    1214 <main+0xe4>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
jne    1214 <main+0xe4>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EFAULT = global i64 0
@.str = [49 x i8] c"stat(invalid-path-ptr) correctly returned error\0A\00"
@.str.1 = [48 x i8] c"stat(invalid-buf-ptr) correctly returned error\0A\00"
@.str.2 = [50 x i8] c"lstat(invalid-path-ptr) correctly returned error\0A\00"
@.str.3 = [49 x i8] c"lstat(invalid-buf-ptr) correctly returned error\0A\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 i8*
%9 = alloca i8*
%10 = alloca %struct.stat*
%11 = alloca %struct.stat*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
store i8* %14, i8** %8
store i8* inttoptr (i64 -1 to i8*), i8** %9
store %struct.stat* %7, %struct.stat** %10
store %struct.stat* inttoptr (i64 -1 to %struct.stat*), %struct.stat** %11
%15 = load i8*, i8** %9
%16 = load %struct.stat*, %struct.stat** %10
%17 = call i32 @stat(i8* %15, %struct.stat* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %2
%21 = load i64, i64* @errno
%22 = load i64, i64* @EFAULT
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %20, %2
%27 = load i8*, i8** %8
%28 = load %struct.stat*, %struct.stat** %11
%29 = call i32 @stat(i8* %27, %struct.stat* %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = load i64, i64* @errno
%34 = load i64, i64* @EFAULT
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %32, %26
%39 = load i8*, i8** %9
%40 = load %struct.stat*, %struct.stat** %10
%41 = call i32 @lstat(i8* %39, %struct.stat* %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %50
44: ; preds = %38
%45 = load i64, i64* @errno
%46 = load i64, i64* @EFAULT
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %44, %38
%51 = load i8*, i8** %8
%52 = load %struct.stat*, %struct.stat** %11
%53 = call i32 @lstat(i8* %51, %struct.stat* %52)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %62
56: ; preds = %50
%57 = load i64, i64* @errno
%58 = load i64, i64* @EFAULT
%59 = icmp eq i64 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %56, %50
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EFAULT = global i64 0
@str = [48 x i8] c"lstat(invalid-buf-ptr) correctly returned error\00"
@str.4 = [49 x i8] c"lstat(invalid-path-ptr) correctly returned error\00"
@str.5 = [47 x i8] c"stat(invalid-buf-ptr) correctly returned error\00"
@str.6 = [48 x i8] c"stat(invalid-path-ptr) correctly returned error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i8*, i8** %1
%6 = call i32 @stat(i8* inttoptr (i64 -1 to i8*), %struct.stat* %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load i64, i64* @errno
%10 = load i64, i64* @EFAULT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.6, i64 0, i64 0))
br label %14
14: ; preds = %12, %8, %2
%15 = tail call i32 @stat(i8* %5, %struct.stat* inttoptr (i64 -1 to %struct.stat*))
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EFAULT
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.5, i64 0, i64 0))
br label %23
23: ; preds = %21, %17, %14
%24 = call i32 @lstat(i8* inttoptr (i64 -1 to i8*), %struct.stat* %3)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EFAULT
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %26, %23
%33 = tail call i32 @lstat(i8* %5, %struct.stat* inttoptr (i64 -1 to %struct.stat*))
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i64, i64* @errno
%37 = load i64, i64* @EFAULT
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %41
41: ; preds = %39, %35, %32
ret i32 0
}