#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 *);
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);
  errno = 0;
  ret = stat(NULL, &st);
  printf("stat 2: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  errno = 0;
  ret = stat("", &st);
  printf("stat 3: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  errno = 0;
  ret = stat("nofoo.fileio.test", &st);
  printf("stat 4: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
}


x86 assembly (clang, -O0):
<test_stat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2ea1(%rip),%rax
movl   $0x0,(%rax)
lea    0xf3a(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1199 <test_stat+0x69>
mov    -0x8(%rbp),%esi
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%edi
lea    0xec9(%rip),%rcx
lea    0xea2(%rip),%rax
cmp    $0xb,%edi
cmove  %rax,%rcx
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b3 <test_stat+0x83>
mov    -0x8(%rbp),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e26(%rip),%rax
movl   $0x0,(%rax)
xor    %eax,%eax
mov    %eax,%edi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dd2(%rip),%rax
movl   $0x0,(%rax)
lea    0xe26(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d7b(%rip),%rax
movl   $0x0,(%rax)
lea    0xe12(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   1030 <strerrno@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@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    0x2ea2(%rip),%r15
movl   $0x0,(%r15)
lea    0xf3a(%rip),%rdi
mov    %rsp,%rsi
call   1050 <stat@plt>
mov    (%r15),%edx
test   %eax,%eax
je     116d <test_stat+0x3d>
lea    0xec2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1193 <test_stat+0x63>
cmpl   $0xb,(%rsp)
lea    0xea9(%rip),%rax
lea    0xec2(%rip),%rcx
cmove  %rax,%rcx
lea    0xe76(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,(%r15)
mov    %rsp,%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <stat@plt>
mov    %eax,%ebp
mov    (%r15),%ebx
mov    %ebx,%edi
call   1030 <strerrno@plt>
lea    0xe88(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,(%r15)
lea    0xe6b(%rip),%rdi
mov    %r14,%rsi
call   1050 <stat@plt>
mov    %eax,%ebx
mov    (%r15),%ebp
mov    %ebp,%edi
call   1030 <strerrno@plt>
lea    0xe72(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,(%r15)
lea    0xe77(%rip),%rdi
mov    %r14,%rsi
call   1050 <stat@plt>
mov    %eax,%ebx
mov    (%r15),%ebp
mov    %ebp,%edi
call   1030 <strerrno@plt>
lea    0xe6e(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@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
store i32 0, i32* @errno
%21 = call i32 @stat(i8* null, %struct.stat* %3)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = load i32, i32* @errno
%24 = load i32, i32* @errno
%25 = call i8* @strerrno(i32 %24)
%26 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %22, i32 %23, i8* %25)
store i32 0, i32* @errno
%27 = call i32 @stat(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %struct.stat* %3)
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = load i32, i32* @errno
%30 = load i32, i32* @errno
%31 = call i8* @strerrno(i32 %30)
%32 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %28, i32 %29, i8* %31)
store i32 0, i32* @errno
%33 = call i32 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), %struct.stat* %3)
store i32 %33, i32* %2
%34 = load i32, i32* %2
%35 = load i32, i32* @errno
%36 = load i32, i32* @errno
%37 = call i8* @strerrno(i32 %36)
%38 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %34, i32 %35, i8* %37)
%39 = load i32, i32* %1
ret i32 %39
}

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
store i32 0, i32* @errno
%15 = call i32 @stat(i8* null, %struct.stat* %1)
%16 = load i32, i32* @errno
%17 = tail call i8* @strerrno(i32 %16)
%18 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %15, i32 %16, i8* %17)
store i32 0, i32* @errno
%19 = call i32 @stat(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %struct.stat* %1)
%20 = load i32, i32* @errno
%21 = tail call i8* @strerrno(i32 %20)
%22 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %19, i32 %20, i8* %21)
store i32 0, i32* @errno
%23 = call i32 @stat(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), %struct.stat* %1)
%24 = load i32, i32* @errno
%25 = tail call i8* @strerrno(i32 %24)
%26 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %23, i32 %24, i8* %25)
ret i32 undef
}