#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 uint32_t;
struct timeval {
  int /*<<< orphan*/ tv_sec;
};
struct stat {
  int /*<<< orphan*/ st_mtime;
  int /*<<< orphan*/ st_atime;
  int /*<<< orphan*/ st_mode;
  int /*<<< orphan*/ st_gid;
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
int fchmod(int, int /*<<< orphan*/);
int fchown(int, int /*<<< orphan*/, int /*<<< orphan*/);
int ftruncate(int, int /*<<< orphan*/);
int futimes(int, struct timeval *);
int fsetstat(int fd, uint32_t attr_flags, struct stat *st) {
  int ret;
  struct timeval amtimes[2] = {0};
  if (attr_flags & 0x00000001) {
    if ((ret = ftruncate(fd, st->st_size)) == -1)
      return ret;
  }
  if (attr_flags & 0x00000002) {
    if ((ret = fchown(fd, st->st_uid, st->st_gid)) == -1)
      return ret;
  }
  if (attr_flags & 0x00000004) {
    if ((ret = fchmod(fd, st->st_mode)) == -1)
      return ret;
  }
  if (attr_flags & 0x00000008) {
    amtimes[0].tv_sec = st->st_atime;
    amtimes[1].tv_sec = st->st_mtime;
    if ((ret = futimes(fd, amtimes)) == -1)
      return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fsetstat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11ac <fsetstat+0x5c>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x14(%rax),%esi
call   1040 <ftruncate@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11a7 <fsetstat+0x57>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1278 <fsetstat+0x128>
jmp    11ac <fsetstat+0x5c>
mov    -0xc(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11ed <fsetstat+0x9d>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%edx
call   1070 <fchown@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11e8 <fsetstat+0x98>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1278 <fsetstat+0x128>
jmp    11ed <fsetstat+0x9d>
mov    -0xc(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     1227 <fsetstat+0xd7>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%esi
call   1060 <fchmod@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1222 <fsetstat+0xd2>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1278 <fsetstat+0x128>
jmp    1227 <fsetstat+0xd7>
mov    -0xc(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     1271 <fsetstat+0x121>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <futimes@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    126c <fsetstat+0x11c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1278 <fsetstat+0x128>
jmp    1271 <fsetstat+0x121>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsetstat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r14d
test   $0x1,%bpl
je     1166 <fsetstat+0x26>
mov    0x14(%rbx),%esi
mov    %r14d,%edi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11be <fsetstat+0x7e>
test   $0x2,%bpl
je     117f <fsetstat+0x3f>
mov    0xc(%rbx),%edx
mov    0x10(%rbx),%esi
mov    %r14d,%edi
call   1060 <fchown@plt>
cmp    $0xffffffff,%eax
je     11be <fsetstat+0x7e>
test   $0x4,%bpl
je     1195 <fsetstat+0x55>
mov    0x8(%rbx),%esi
mov    %r14d,%edi
call   1050 <fchmod@plt>
cmp    $0xffffffff,%eax
je     11be <fsetstat+0x7e>
test   $0x8,%bpl
je     11ba <fsetstat+0x7a>
mov    0x4(%rbx),%eax
mov    %eax,0x8(%rsp)
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
call   1030 <futimes@plt>
cmp    $0xffffffff,%eax
je     11be <fsetstat+0x7e>
xor    %eax,%eax
jmp    11c3 <fsetstat+0x83>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32, i32, i32, i32 }
%struct.timeval = type { i32 }
define i32 @fsetstat(i32 %0, i32 %1, %struct.stat* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat*
%8 = alloca i32
%9 = alloca [2 x %struct.timeval]
store i32 %0, i32* %5
store i32 %1, i32* %6
store %struct.stat* %2, %struct.stat** %7
%10 = bitcast [2 x %struct.timeval]* %9 to i8*
%11 = load i32, i32* %6
%12 = and i32 %11, 1
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %3
%15 = load i32, i32* %5
%16 = load %struct.stat*, %struct.stat** %7
%17 = getelementptr inbounds %struct.stat, %struct.stat* %16, i32 0, i32 5
%18 = load i32, i32* %17
%19 = call i32 @ftruncate(i32 %15, i32 %18)
store i32 %19, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i32, i32* %8
store i32 %22, i32* %4
br label %78
23: ; preds = %14
br label %24
24: ; preds = %23, %3
%25 = load i32, i32* %6
%26 = and i32 %25, 2
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %41
28: ; preds = %24
%29 = load i32, i32* %5
%30 = load %struct.stat*, %struct.stat** %7
%31 = getelementptr inbounds %struct.stat, %struct.stat* %30, i32 0, i32 4
%32 = load i32, i32* %31
%33 = load %struct.stat*, %struct.stat** %7
%34 = getelementptr inbounds %struct.stat, %struct.stat* %33, i32 0, i32 3
%35 = load i32, i32* %34
%36 = call i32 @fchown(i32 %29, i32 %32, i32 %35)
store i32 %36, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = load i32, i32* %8
store i32 %39, i32* %4
br label %78
40: ; preds = %28
br label %41
41: ; preds = %40, %24
%42 = load i32, i32* %6
%43 = and i32 %42, 4
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %41
%46 = load i32, i32* %5
%47 = load %struct.stat*, %struct.stat** %7
%48 = getelementptr inbounds %struct.stat, %struct.stat* %47, i32 0, i32 2
%49 = load i32, i32* %48
%50 = call i32 @fchmod(i32 %46, i32 %49)
store i32 %50, i32* %8
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = load i32, i32* %8
store i32 %53, i32* %4
br label %78
54: ; preds = %45
br label %55
55: ; preds = %54, %41
%56 = load i32, i32* %6
%57 = and i32 %56, 8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %77
59: ; preds = %55
%60 = load %struct.stat*, %struct.stat** %7
%61 = getelementptr inbounds %struct.stat, %struct.stat* %60, i32 0, i32 1
%62 = load i32, i32* %61
%63 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %9, i64 0, i64 0
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %63, i32 0, i32 0
store i32 %62, i32* %64
%65 = load %struct.stat*, %struct.stat** %7
%66 = getelementptr inbounds %struct.stat, %struct.stat* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %9, i64 0, i64 1
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %68, i32 0, i32 0
store i32 %67, i32* %69
%70 = load i32, i32* %5
%71 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %9, i64 0, i64 0
%72 = call i32 @futimes(i32 %70, %struct.timeval* %71)
store i32 %72, i32* %8
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %59
%75 = load i32, i32* %8
store i32 %75, i32* %4
br label %78
76: ; preds = %59
br label %77
77: ; preds = %76, %55
store i32 0, i32* %4
br label %78
78: ; preds = %77, %74, %52, %38, %21
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32, i32, i32, i32 }
%struct.timeval = type { i32 }
define i32 @fsetstat(i32 %0, i32 %1, %struct.stat* nocapture %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to [2 x %struct.timeval]*
%6 = bitcast i64* %4 to i8*
%7 = and i32 %1, 1
%8 = icmp eq i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %3
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 5
%11 = load i32, i32* %10
%12 = tail call i32 @ftruncate(i32 %0, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %46, label %14
14: ; preds = %9, %3
%15 = and i32 %1, 2
%16 = icmp eq i32 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %14
%18 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 4
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 3
%21 = load i32, i32* %20
%22 = tail call i32 @fchown(i32 %0, i32 %19, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %46, label %24
24: ; preds = %17, %14
%25 = and i32 %1, 4
%26 = icmp eq i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %24
%28 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 2
%29 = load i32, i32* %28
%30 = tail call i32 @fchmod(i32 %0, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %46, label %32
32: ; preds = %27, %24
%33 = and i32 %1, 8
%34 = icmp eq i32 %33, 0
br i1 %34, label %45, label %35
35: ; preds = %32
%36 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%37 = load i32, i32* %36
%38 = bitcast i64* %4 to %struct.timeval*
%39 = bitcast i64* %4 to i32*
store i32 %37, i32* %39
%40 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%41 = load i32, i32* %40
%42 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %5, i64 0, i64 1, i32 0
store i32 %41, i32* %42
%43 = call i32 @futimes(i32 %0, %struct.timeval* %38)
%44 = icmp eq i32 %43, -1
br i1 %44, label %46, label %45
45: ; preds = %35, %32
br label %46
46: ; preds = %35, %27, %17, %9, %45
%47 = phi i32 [ 0, %45 ], [ -1, %9 ], [ -1, %17 ], [ -1, %27 ], [ -1, %35 ]
ret i32 %47
}