#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_uid;
  int st_gid;
};

/* Variables and functions */
int chown(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int stat(char *, struct stat *);
int main() {
  struct stat sb, sb_new;
  int stat_st;
  int chown_st;
  stat_st = stat("./chown.c", &sb);
  printf("UID=%ld, GID=%ld\n", (long int)sb.st_uid, (long int)sb.st_gid);
  chown_st = chown("./chown.c", 1001, 1001);
  if (chown_st == -1) {
    printf("error\n");
    return chown_st;
  }
  stat_st = stat("./chown.c", &sb_new);
  printf("UID=%ld, GID=%ld\n", (long int)sb_new.st_uid,
         (long int)sb_new.st_gid);
  chown_st = chown("./chown.c", sb.st_uid, sb.st_gid);
  if (chown_st == -1) {
    printf("error\n");
    return chown_st;
  }
  stat_st = stat("./chown.c", &sb);
  printf("UID=%ld, GID=%ld\n", (long int)sb.st_uid, (long int)sb.st_gid);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x10(%rbp),%rsi
movslq -0xc(%rbp),%rdx
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe91(%rip),%rdi
mov    $0x3e9,%edx
mov    %edx,%esi
call   1050 <chown@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11a1 <main+0x71>
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1232 <main+0x102>
lea    0xe58(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x18(%rbp),%rsi
movslq -0x14(%rbp),%rdx
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe29(%rip),%rdi
call   1050 <chown@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1202 <main+0xd2>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1232 <main+0x102>
lea    0xdf7(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x10(%rbp),%rsi
movslq -0xc(%rbp),%rdx
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xeaf(%rip),%r14
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1050 <stat@plt>
mov    0x8(%rsp),%ebx
mov    0xc(%rsp),%ebp
movslq %ebx,%rsi
movslq %ebp,%rdx
lea    0xe97(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x3e9,%esi
mov    $0x3e9,%edx
call   1060 <chown@plt>
cmp    $0xffffffff,%eax
je     11fc <main+0xbc>
lea    0xe68(%rip),%r15
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1050 <stat@plt>
movslq 0x10(%rsp),%rsi
movslq 0x14(%rsp),%rdx
lea    0xe54(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1060 <chown@plt>
cmp    $0xffffffff,%eax
je     11fc <main+0xbc>
lea    0xe28(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
movslq 0x8(%rsp),%rsi
movslq 0xc(%rsp),%rdx
lea    0xe17(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    120e <main+0xce>
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [10 x i8] c"./chown.c\00"
@.str.1 = [18 x i8] c"UID=%ld, GID=%ld\0A\00"
@.str.2 = [7 x i8] c"error\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
store i32 %6, i32* %4
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%8 = load i32, i32* %7
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 1
%11 = load i32, i32* %10
%12 = sext i32 %11 to i64
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %9, i64 %12)
%14 = call i32 @chown(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 1001, i32 1001)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* %5
store i32 %19, i32* %1
br label %48
20: ; preds = %0
%21 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
store i32 %21, i32* %4
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%26 = load i32, i32* %25
%27 = sext i32 %26 to i64
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %24, i64 %27)
%29 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @chown(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %20
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %5
store i32 %38, i32* %1
br label %48
39: ; preds = %20
%40 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
store i32 %40, i32* %4
%41 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%42 = load i32, i32* %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 1
%45 = load i32, i32* %44
%46 = sext i32 %45 to i64
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %43, i64 %46)
store i32 0, i32* %1
br label %48
48: ; preds = %39, %36, %17
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [10 x i8] c"./chown.c\00"
@.str.1 = [18 x i8] c"UID=%ld, GID=%ld\0A\00"
@str.3 = [6 x i8] c"error\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %1 to i8*
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%6 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%7 = load i32, i32* %6
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%10 = load i32, i32* %9
%11 = sext i32 %10 to i64
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %8, i64 %11)
%13 = tail call i32 @chown(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 1001, i32 1001)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.3, i64 0, i64 0))
br label %37
17: ; preds = %0
%18 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%19 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%20 = load i32, i32* %19
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %21, i64 %24)
%26 = tail call i32 @chown(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.3, i64 0, i64 0))
br label %37
30: ; preds = %17
%31 = call i32 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %33, i64 %35)
br label %37
37: ; preds = %30, %28, %15
%38 = phi i32 [ -1, %15 ], [ -1, %28 ], [ 0, %30 ]
ret i32 %38
}