#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 /*<<< orphan*/ uid_t;
struct passwd {
  int /*<<< orphan*/ pw_uid;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int chown(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strtol(char *, char **, int);
int main(int argc, char *argv[]) {
  uid_t uid;
  struct passwd *pwd;
  char *endptr;
  if (argc != 3 || argv[1][0] == '\0') {
    fprintf(stderr, "%s <owner> <file>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  uid = strtol(argv[1], &endptr, 10);
  if (*endptr != '\0') {
    pwd = getpwnam(argv[1]);
    if (pwd == NULL) {
      perror("getpwnam");
      exit(EXIT_FAILURE);
    }
    uid = pwd->pw_uid;
  }
  if (chown(argv[2], uid, -1) == -1) {
    perror("chown");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    1194 <main+0x34>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11be <main+0x5e>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe55(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1226 <main+0xc6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121d <main+0xbd>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x14(%rbp),%esi
mov    $0xffffffff,%edx
call   1060 <chown@plt>
cmp    $0xffffffff,%eax
jne    125e <main+0xfe>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    11bc <main+0x5c>
mov    0x8(%rbx),%rdi
cmpb   $0x0,(%rdi)
je     11bc <main+0x5c>
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     119f <main+0x3f>
mov    0x8(%rbx),%rdi
call   1030 <getpwnam@plt>
test   %rax,%rax
je     11df <main+0x7f>
mov    (%rax),%eax
mov    0x10(%rbx),%rdi
mov    %eax,%esi
mov    $0xffffffff,%edx
call   1060 <chown@plt>
cmp    $0xffffffff,%eax
je     11d6 <main+0x76>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe31(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11eb <main+0x8b>
lea    0xe3f(%rip),%rdi
jmp    11e6 <main+0x86>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"%s <owner> <file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"getpwnam\00"
@.str.2 = [6 x i8] c"chown\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.passwd*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %19, label %11
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %11, %2
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %11
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @strtol(i8* %30, i8** %8, i32 10)
store i32 %31, i32* %6
%32 = load i8*, i8** %8
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %27
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call %struct.passwd* @getpwnam(i8* %39)
store %struct.passwd* %40, %struct.passwd** %7
%41 = load %struct.passwd*, %struct.passwd** %7
%42 = icmp eq %struct.passwd* %41, null
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %36
%48 = load %struct.passwd*, %struct.passwd** %7
%49 = getelementptr inbounds %struct.passwd, %struct.passwd* %48, i32 0, i32 0
%50 = load i32, i32* %49
store i32 %50, i32* %6
br label %51
51: ; preds = %47, %27
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = load i32, i32* %6
%56 = call i32 @chown(i8* %54, i32 %55, i32 -1)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %62
58: ; preds = %51
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %51
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"%s <owner> <file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"getpwnam\00"
@.str.2 = [6 x i8] c"chown\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %6, %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %6
%18 = call i32 @strtol(i8* %8, i8** %3, i32 10)
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %33, label %22
22: ; preds = %17
%23 = load i8*, i8** %7
%24 = tail call %struct.passwd* @getpwnam(i8* %23)
%25 = icmp eq %struct.passwd* %24, null
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %22
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i64 0, i32 0
%32 = load i32, i32* %31
br label %33
33: ; preds = %30, %17
%34 = phi i32 [ %32, %30 ], [ %18, %17 ]
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = tail call i32 @chown(i8* %36, i32 %34, i32 -1)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = tail call i32 @exit(i32 %41)
unreachable
43: ; preds = %33
ret i32 0
}