#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 passwd {
  int /*<<< orphan*/ pw_gid;
  int /*<<< orphan*/ pw_uid;
};
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
scalar_t__ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fail_errno_except_eintr(char *);
int mkdir(char const *, int /*<<< orphan*/);
int mkdir_user_owned(const char *path, mode_t mode, struct passwd *user) {
  int result = mkdir(path, mode);
  if (result >= 0) {
    while ((int)(chown(path, user->pw_uid, user->pw_gid)) == -1)
      fail_errno_except_eintr("chown(path, user->pw_uid, user->pw_gid)");
    ;
  }
  return result;
}


x86 assembly (clang, -O0):
<mkdir_user_owned>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1196 <mkdir_user_owned+0x66>
jmp    1161 <mkdir_user_owned+0x31>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1050 <chown@plt>
cmp    $0xffffffff,%eax
jne    1191 <mkdir_user_owned+0x61>
lea    0xe79(%rip),%rdi
call   1030 <fail_errno_except_eintr@plt>
jmp    1161 <mkdir_user_owned+0x31>
jmp    1196 <mkdir_user_owned+0x66>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_user_owned>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rdi,%r14
call   1040 <mkdir@plt>
mov    %eax,%r15d
test   %eax,%eax
js     118a <mkdir_user_owned+0x5a>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   1050 <chown@plt>
cmp    $0xffffffff,%eax
jne    118a <mkdir_user_owned+0x5a>
lea    0xe9e(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1030 <fail_errno_except_eintr@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   1050 <chown@plt>
cmp    $0xffffffff,%eax
je     1170 <mkdir_user_owned+0x40>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@.str = [40 x i8] c"chown(path, user->pw_uid, user->pw_gid)\00"
define i32 @mkdir_user_owned(i8* %0, i32 %1, %struct.passwd* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.passwd*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store %struct.passwd* %2, %struct.passwd** %6
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = call i32 @mkdir(i8* %8, i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %3
br label %14
14: ; preds = %25, %13
%15 = load i8*, i8** %4
%16 = load %struct.passwd*, %struct.passwd** %6
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load %struct.passwd*, %struct.passwd** %6
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @chown(i8* %15, i32 %18, i32 %21)
%23 = trunc i64 %22 to i32
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @fail_errno_except_eintr(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %14
27: ; preds = %14
br label %28
28: ; preds = %27, %3
%29 = load i32, i32* %7
ret i32 %29
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@.str = [40 x i8] c"chown(path, user->pw_uid, user->pw_gid)\00"
define i32 @mkdir_user_owned(i8* nocapture %0, i32 %1, %struct.passwd* nocapture %2) {
%4 = tail call i32 @mkdir(i8* %0, i32 %1)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %21
6: ; preds = %3
%7 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 1
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%9 = load i32, i32* %7
%10 = load i32, i32* %8
%11 = tail call i64 @chown(i8* %0, i32 %9, i32 %10)
%12 = trunc i64 %11 to i32
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %21
14: ; preds = %6, %14
%15 = tail call i32 @fail_errno_except_eintr(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %7
%17 = load i32, i32* %8
%18 = tail call i64 @chown(i8* %0, i32 %16, i32 %17)
%19 = trunc i64 %18 to i32
%20 = icmp eq i32 %19, -1
br i1 %20, label %14, label %21
21: ; preds = %14, %6, %3
ret i32 %4
}