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

/* Variables and functions */
int chown(char *, int, int);
scalar_t__ errno;
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ strtol(char *, char **, int);
void cmd_chown(int argc, char *argv[]) {
  struct passwd *pwd;
  int uid;
  errno = 0;
  uid = (int)strtol(argv[1], (char **)NULL, 10);
  if (errno) {
    if ((pwd = getpwnam(argv[1])) == NULL) {
      perror("getpwnam");
      return;
    }
    uid = pwd->pw_uid;
  }
  if (chown(argv[2], uid, -1) == -1)
    perror("chown");
}


x86 assembly (clang, -O0):
<cmd_chown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e8a(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e63(%rip),%rax
cmpq   $0x0,(%rax)
je     11bc <cmd_chown+0x7c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b3 <cmd_chown+0x73>
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
jmp    11e6 <cmd_chown+0xa6>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x1c(%rbp),%esi
mov    $0xffffffff,%edx
call   1050 <chown@plt>
cmp    $0xffffffff,%eax
jne    11e6 <cmd_chown+0xa6>
lea    0xe28(%rip),%rdi
call   1060 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_chown>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e92(%rip),%r14
movq   $0x0,(%r14)
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
cmpq   $0x0,(%r14)
je     117b <cmd_chown+0x3b>
mov    0x8(%rbx),%rdi
call   1030 <getpwnam@plt>
test   %rax,%rax
je     11a1 <cmd_chown+0x61>
mov    (%rax),%eax
mov    0x10(%rbx),%rdi
mov    %eax,%esi
mov    $0xffffffff,%edx
call   1050 <chown@plt>
cmp    $0xffffffff,%eax
je     1198 <cmd_chown+0x58>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6a(%rip),%rdi
jmp    11a8 <cmd_chown+0x68>
lea    0xe58(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <perror@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@errno = global i64 0
@.str = [9 x i8] c"getpwnam\00"
@.str.1 = [6 x i8] c"chown\00"
define void @cmd_chown(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.passwd*
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i64 0, i64* @errno
%7 = load i8**, i8*** %4
%8 = getelementptr inbounds i8*, i8** %7, i64 1
%9 = load i8*, i8** %8
%10 = call i64 @strtol(i8* %9, i8** null, i32 10)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %6
%12 = load i64, i64* @errno
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %2
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call %struct.passwd* @getpwnam(i8* %17)
store %struct.passwd* %18, %struct.passwd** %5
%19 = icmp eq %struct.passwd* %18, null
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %35
22: ; preds = %14
%23 = load %struct.passwd*, %struct.passwd** %5
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %6
br label %26
26: ; preds = %22, %2
%27 = load i8**, i8*** %4
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = load i32, i32* %6
%31 = call i32 @chown(i8* %29, i32 %30, i32 -1)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %20, %33, %26
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@errno = global i64 0
@.str = [9 x i8] c"getpwnam\00"
@.str.1 = [6 x i8] c"chown\00"
define void @cmd_chown(i32 %0, i8** nocapture %1) {
store i64 0, i64* @errno
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i64 @strtol(i8* nocapture %4, i8** null, i32 10)
%6 = trunc i64 %5 to i32
%7 = load i64, i64* @errno
%8 = icmp eq i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %2
%10 = load i8*, i8** %3
%11 = tail call %struct.passwd* @getpwnam(i8* %10)
%12 = icmp eq %struct.passwd* %11, null
br i1 %12, label %22, label %13
13: ; preds = %9
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i64 0, i32 0
%15 = load i32, i32* %14
br label %16
16: ; preds = %13, %2
%17 = phi i32 [ %15, %13 ], [ %6, %2 ]
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @chown(i8* %19, i32 %17, i32 -1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16, %9
%23 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %16 ]
%24 = tail call i32 @perror(i8* %23)
br label %25
25: ; preds = %22, %16
ret void
}