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

/* Variables and functions */
int errno;
int /*<<< orphan*/ printf(char *, int, int, ...);
int stat(char *, struct stat *);
int /*<<< orphan*/ stop();
char *strerrno(int);
int test_stat() {
  int ret;
  struct stat st;
  errno = 0;
  ret = stat("foo.fileio.test", &st);
  if (!ret)
    printf("stat 1: ret = %d, errno = %d %s\n", ret, errno,
           st.st_size == 11 ? "OK" : "");
  else
    printf("stat 1: ret = %d, errno = %d\n", ret, errno);
  stop();
  errno = 0;
  ret = stat(NULL, &st);
  printf("stat 2: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  stop();
  errno = 0;
  ret = stat("", &st);
  printf("stat 3: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  stop();
  errno = 0;
  ret = stat("nofoo.fileio.test", &st);
  printf("stat 4: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  stop();
}


x86 assembly (clang, -O0):
<test_stat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e91(%rip),%rax
movl   $0x0,(%rax)
lea    0xf2a(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11a9 <test_stat+0x69>
mov    -0x8(%rbp),%esi
mov    0x2e64(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%edi
lea    0xeb9(%rip),%rcx
lea    0xe92(%rip),%rax
cmp    $0xb,%edi
cmove  %rax,%rcx
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11c3 <test_stat+0x83>
mov    -0x8(%rbp),%esi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    0x2e0f(%rip),%rax
movl   $0x0,(%rax)
xor    %eax,%eax
mov    %eax,%edi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    0x2db4(%rip),%rax
movl   $0x0,(%rax)
lea    0xe08(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    0x2d56(%rip),%rax
movl   $0x0,(%rax)
lea    0xded(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_stat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e92(%rip),%r15
movl   $0x0,(%r15)
lea    0xf2a(%rip),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
mov    (%r15),%edx
test   %eax,%eax
je     117d <test_stat+0x3d>
lea    0xeb2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11a3 <test_stat+0x63>
cmpl   $0xb,(%rsp)
lea    0xe99(%rip),%rax
lea    0xeb2(%rip),%rcx
cmove  %rax,%rcx
lea    0xe66(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <stop@plt>
movl   $0x0,(%r15)
mov    %rsp,%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <stat@plt>
mov    %eax,%ebp
mov    (%r15),%ebx
mov    %ebx,%edi
call   1030 <strerrno@plt>
lea    0xe71(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <stop@plt>
movl   $0x0,(%r15)
lea    0xe4d(%rip),%rdi
mov    %r14,%rsi
call   1060 <stat@plt>
mov    %eax,%ebx
mov    (%r15),%ebp
mov    %ebp,%edi
call   1030 <strerrno@plt>
lea    0xe54(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <stop@plt>
movl   $0x0,(%r15)
lea    0xe52(%rip),%rdi
mov    %r14,%rsi
call   1060 <stat@plt>
mov    %eax,%ebx
mov    (%r15),%ebp
mov    %ebp,%edi
call   1030 <strerrno@plt>
lea    0xe49(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <stop@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@.str.1 = [33 x i8] c"stat 1: ret = %d, errno = %d %s\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [30 x i8] c"stat 1: ret = %d, errno = %d\0A\00"
@.str.5 = [33 x i8] c"stat 2: ret = %d, errno = %d %s\0A\00"
@.str.6 = [33 x i8] c"stat 3: ret = %d, errno = %d %s\0A\00"
@.str.7 = [18 x i8] c"nofoo.fileio.test\00"
@.str.8 = [33 x i8] c"stat 4: ret = %d, errno = %d %s\0A\00"
define i32 @test_stat() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
store i32 0, i32* @errno
%4 = call i32 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp ne i32 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %0
%8 = load i32, i32* %2
%9 = load i32, i32* @errno
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, 11
%13 = zext i1 %12 to i64
%14 = select i1 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%15 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %9, i8* %14)
br label %20
16: ; preds = %0
%17 = load i32, i32* %2
%18 = load i32, i32* @errno
%19 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %17, i32 %18)
br label %20
20: ; preds = %16, %7
%21 = call i32 (...) @stop()
store i32 0, i32* @errno
%22 = call i32 @stat(i8* null, %struct.stat* %3)
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = load i32, i32* @errno
%25 = load i32, i32* @errno
%26 = call i8* @strerrno(i32 %25)
%27 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %23, i32 %24, i8* %26)
%28 = call i32 (...) @stop()
store i32 0, i32* @errno
%29 = call i32 @stat(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %struct.stat* %3)
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = load i32, i32* @errno
%32 = load i32, i32* @errno
%33 = call i8* @strerrno(i32 %32)
%34 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %30, i32 %31, i8* %33)
%35 = call i32 (...) @stop()
store i32 0, i32* @errno
%36 = call i32 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), %struct.stat* %3)
store i32 %36, i32* %2
%37 = load i32, i32* %2
%38 = load i32, i32* @errno
%39 = load i32, i32* @errno
%40 = call i8* @strerrno(i32 %39)
%41 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %37, i32 %38, i8* %40)
%42 = call i32 (...) @stop()
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@.str.1 = [33 x i8] c"stat 1: ret = %d, errno = %d %s\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [1 x i8] zeroinitializer
@.str.4 = [30 x i8] c"stat 1: ret = %d, errno = %d\0A\00"
@.str.5 = [33 x i8] c"stat 2: ret = %d, errno = %d %s\0A\00"
@.str.6 = [33 x i8] c"stat 3: ret = %d, errno = %d %s\0A\00"
@.str.7 = [18 x i8] c"nofoo.fileio.test\00"
@.str.8 = [33 x i8] c"stat 4: ret = %d, errno = %d %s\0A\00"
define i32 @test_stat() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
store i32 0, i32* @errno
%3 = call i32 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp eq i32 %3, 0
%5 = load i32, i32* @errno
br i1 %4, label %6, label %12
6: ; preds = %0
%7 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = icmp eq i32 %8, 11
%10 = select i1 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%11 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %5, i8* %10)
br label %14
12: ; preds = %0
%13 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %3, i32 %5)
br label %14
14: ; preds = %12, %6
%15 = tail call i32 (...) @stop()
store i32 0, i32* @errno
%16 = call i32 @stat(i8* null, %struct.stat* %1)
%17 = load i32, i32* @errno
%18 = tail call i8* @strerrno(i32 %17)
%19 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %16, i32 %17, i8* %18)
%20 = tail call i32 (...) @stop()
store i32 0, i32* @errno
%21 = call i32 @stat(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %struct.stat* %1)
%22 = load i32, i32* @errno
%23 = tail call i8* @strerrno(i32 %22)
%24 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %21, i32 %22, i8* %23)
%25 = tail call i32 (...) @stop()
store i32 0, i32* @errno
%26 = call i32 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), %struct.stat* %1)
%27 = load i32, i32* @errno
%28 = tail call i8* @strerrno(i32 %27)
%29 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %26, i32 %27, i8* %28)
%30 = tail call i32 (...) @stop()
ret i32 undef
}